我在ViewController类中创建了一个自定义方法,如下所示:
- (void) modifyTextField:(UITextField *)textField
{
// Prepare the imageView with the required image
uint padding = 10;//padding for iOS7
UIImageView * iconImageView = [[UIImageView alloc] initWithImage:iconImage];
iconImageView.frame = CGRectMake(0 + padding, 0, 16, 16);
// Set the imageView to the left of the given text field.
textField.leftView = iconImageView;
textField.leftViewMode = UITextFieldViewModeAlways;
}
现在,我可以在(viewDidLoadmethod)内调用该方法,并将我的任何方法发送TextFields到该方法,并为左右添加填充,并通过仅编写一行代码来给文本和背景色,如下所示:
[self modifyTextField:self.firstNameTxtFld];
这在iOS 7上完美运行!希望这也可以在iOS 8和9上使用!
我知道添加过多的View可能会使此对象的加载重一些。但是,当担心其他解决方案中的困难时,我发现自己更偏向于这种方法,并且使用这种方式更加灵活。;)
希望这个答案可能对寻找其他人的解决方案有所帮助或有用。
干杯!