2014-06-13 40 views
0

我正在使用以下代碼創建帶有清除按鈕模式的文本字段。UITextField「clearButtonRectForBounds」方法不調用

self.TextField =[[UITextField alloc]initWithFrame:CGRectMake(30, 0, 200, 30)]; 
    self.TextField.tintColor =[UIColor lightGrayColor]; 
    self.TextField.returnKeyType =UIReturnKeyGo; 
    self.TextField.clearButtonMode =UITextFieldViewModeWhileEditing; 
    self.TextField.delegate=self; 
    [self.view addSubview:self.TextField]; 

- (CGRect)clearButtonRectForBounds:(CGRect)bounds 
{ 
    return CGRectMake(bounds.origin.x - 10, bounds.origin.y, bounds.size.width, bounds.size.height+30); 
} 

我想爲清除按鈕設置自定義框架,爲此我試圖調用「clearButtonRectForBounds」。但「clearButtonRectForBounds」方法不能調用。我如何設置清晰的按鈕框架?

回答

0

你需要子類UITextField有需要覆蓋-(CGRect)clearButtonRectForBounds:(CGRect)bounds方法,在這個答案由@Luda描述,https://stackoverflow.com/a/14870633/1603234

+0

我有一個UIViewController subclass.In該類我有一個文本字段 – lazyCoder

+0

歐凱,但你已經創建了一個UITextField的子類,然後在你的UIViewController中使用。 – Hemang