2014-02-28 35 views
2

我將imageView設置爲文本框的右側視圖。這是我的代碼:rightView iOS 7的UITextField屬性錯誤

UITextField *textField = [[UITextField alloc]initWithFrame:controlFrame]; 
    [textField setBorderStyle:UITextBorderStyleRoundedRect]; 
    [textField setBackgroundColor:[UIColor whiteColor]]; 
    CGRect imageViewFrame = CGRectMake(controlFrame.origin.x + controlFrame.size.width - 20,controlFrame.origin.y, 15.0,controlFrame.size.height-10); 
    UIImage *image = [UIImage imageNamed:@"arrow.png"];; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    [imageView setBackgroundColor:[UIColor clearColor]]; 
    [imageView setFrame:imageViewFrame]; 
    [textField setRightViewMode:UITextFieldViewModeAlways]; 
    textField.rightView = imageView; 

它的工作完美在iOS 6.1設備enter image description here

但在搭載iOS 7的設備enter image description here

的任何解決方案呢?

在此先感謝

回答

3

只是在做這個我和使用此解決方案:

- (CGRect) rightViewRectForBounds:(CGRect)bounds { 

CGRect textRect = [super rightViewRectForBounds:bounds]; 
textRect.origin.x -= 10; 
return textRect; 
} 

照片將在從右邊10移動,而不必擠靠着邊緣圖像在iOS的7

此外,這是也是UITextField的一個子類,可以通過創建:

創建一個新的文件,這是一個的UITextField,而不是默認的NSObject 的子類,添加一個名爲的新方法 - (ID)的initWithCoder:(NSCoder *)編碼器來設置圖像

- (id)initWithCoder:(NSCoder*)coder { 
self = [super initWithCoder:coder]; 

if (self) { 

    self.clipsToBounds = YES; 
    [self setRightViewMode:UITextFieldViewModeUnlessEditing]; 

    self.leftView = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"textfield_edit_icon.png"]]; 
} 

return self; 
} 

您可能不得不進口#進口

添加rightViewRectForBounds上述方法

在Interface Builder中,點擊文本字段要繼承和改變類屬性到這個新的子類

0

還有一個更快的選項的名稱,沒有必要繼承你的uitextfield。有一個例子

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 6)]; 
[imageView setImage:[UIImage imageNamed:@"grey_filter.png"]]; 

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 6)]; 
[paddingView addSubview:imageView]; 

只需創建一個視圖比您的圖像更大的填充。添加你的UIImageView,然後把它作爲左/右視圖在你的文本框中。