rightView iOS 7的UITextField属性错误
问题描述:
我将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设备
但在搭载iOS 7的设备
的任何解决方案呢?
在此先感谢
答
只是在做这个我和使用此解决方案:
- (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中,点击文本字段要继承和改变类属性到这个新的子类
答
还有一个更快的选项的名称,没有必要继承你的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,然后把它作为左/右视图在你的文本框中。