2011-12-02 123 views
5

我添加一個文本框我的看法是這樣的:的UITextField文字重疊清除按鈕

UITextField* tf_email = [[UITextField alloc] initWithFrame:CGRectMake((320-btnImage1.size.width)/2, 170, 175, 35)]; 
    [tf_email setBackgroundColor:[UIColor clearColor]]; 
    [tf_email setBorderStyle:UITextBorderStyleRoundedRect]; 
    [tf_email setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    [tf_email setReturnKeyType:UIReturnKeyDone]; 
    [tf_email setAutocapitalizationType:UITextAutocapitalizationTypeNone]; 
    [tf_email setEnablesReturnKeyAutomatically:NO]; 
    [tf_email setDelegate:self];  
    [tf_email setOpaque:YES]; 
    tf_email.tag=1; 
    tf_email.font = TTSTYLEVAR(font); 
    tf_email.layer.cornerRadius = 10; 
    tf_email.keyboardType = UIKeyboardTypeEmailAddress; 
    [tf_email setAutocorrectionType:UITextAutocorrectionTypeNo]; 
    tf_email.placeholder = @"[email protected]"; 
    [self.view addSubview:tf_email]; 

當我在這個字段中輸入長文本,文本和清除按鈕重疊。有誰知道如何解決這一問題?

+0

請儘量使用標準字體和看到的結果。 – beryllium

+0

沒有區別。 –

回答

3

我想通了什麼問題了。在另一個文件中,我有一個爲UITextField定義的類別。該類別指定文本區域非常接近左右邊界。這是導致重疊。

獲得的經驗:定義類別時,我們應該使用單獨的文件,以便修改很容易檢測到。

+0

你拯救我的一天... –

1

[button bringSubviewToFront:self.view];

afert添加文本框..

+0

我需要把哪個視圖帶到前面? –

+0

clearButton View意味着被Textfield重疊的視圖... – Developer

+0

請參閱iPad上的App Store應用程序。它有一個搜索按鈕,文本和清除按鈕不會相互衝突。在這種情況下,就不需要把一個視圖放在另一個視圖上。 –

12

創建的UITextField子類,並覆蓋以下提供的方法,

/*< Place holder position >*/ 
- (CGRect)textRectForBounds:(CGRect)bounds { 

    bounds.size.width = bounds.size.width - 20.0f; 
    return bounds; 
} 

/*< Text Posiotion >*/ 
- (CGRect)editingRectForBounds:(CGRect)bounds { 

    bounds.size.width = bounds.size.width - 20.0f; 
    return bounds; 
} 

乾杯!

3

@Augustine P A,我認爲更好的方法是調用超和修改的結果是這樣的:

-(CGRect) textRectForBounds:(CGRect)bounds 
{ 
    CGRect rect = [super textRectForBounds:bounds]; 
    rect.size.width = rect.size.width - 20.0f; 
    return rect; 
} 

-(CGRect) editingRectForBounds:(CGRect)bounds 
{ 
    CGRect rect = [super editingRectForBounds:bounds]; 
    rect.size.width = rect.size.width - 20.0f; 
    return rect; 
}