iOS7上的UITextfield leftView / rightView填充
iOS7上的UITextField的leftView和rightView视图非常接近文本域边框。
The leftView and rightView views of an UITextField on iOS7 are really close to the textfield border.
如何为这些项添加一些(水平)填充?
How may I add some (horizontal) padding to those items?
我尝试修改框架,但没有工作
I tried modifying the frame, but did not work
uint padding = 10;//padding for iOS7
UIImageView * iconImageView = [[UIImageView alloc] initWithImage:iconImage];
iconImageView.frame = CGRectMake(0 + padding, 0, 16, 16);
textField.leftView = iconImageView;
请注意,我没有兴趣在textfield的文本中添加填充,就像这样使用UITextBorderStyleNone设置UITextField的填充
Please, note that I'm not interested in adding padding to the textfield's text, like this Set padding for UITextField with UITextBorderStyleNone
我自己正在研究并使用这个解决方案:
Was just working on this myself and used this solution:
- (CGRect) rightViewRectForBounds:(CGRect)bounds {
CGRect textRect = [super rightViewRectForBounds:bounds];
textRect.origin.x -= 10;
return textRect;
}
这会将图像从右边移动10而不是图像挤在iOS 7中的边缘。
This will move the image over from the right by 10 instead of having the image squeezed up against the edge in iOS 7.
此外,这是在 UITextField
的子类中,可以是创建者:
Additionally, this was in a subclass of UITextField
, which can be created by:
- 创建一个新文件,该文件是
UITextField
的子类,而不是defaultNSObject
-
添加名为
的新方法 - (id)initWithCoder:(NSCoder *)编码器
设置图像
- Create a new file that's a subclass of
UITextField
instead of the defaultNSObject
Add a new method named
- (id)initWithCoder:(NSCoder*)coder
to set the image
- (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;
}
您可能需要导入 #import < QuartzCore / QuartzCore.h>
添加 rightViewRectForBounds
方法上面
在Interface Builder中,单击要子类化的TextField并将class属性更改为此新子类的名称
In Interface Builder, click on the TextField you would like to subclass and change the class attribute to the name of this new subclass