UITextField的iOS7中存在rightView属性错误

UITextField的iOS7中存在rightView属性错误,ios,iphone,ipad,Ios,Iphone,Ipad,我正在将imageView设置为文本字段的右视图。这是我的代码: UITextField *textField = [[UITextField alloc]initWithFrame:controlFrame]; [textField setBorderStyle:UITextBorderStyleRoundedRect]; [textField setBackgroundColor:[UIColor whiteColor]]; CGRect imageViewFrame

我正在将imageView设置为文本字段的右视图。这是我的代码:

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*)coder的新方法来设置图像

- (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中,单击要子类化的文本字段,并将class属性更改为这个新子类的名称

还有一个更快的选项,无需子类化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,然后将其作为左/右视图放在文本字段中