Ios UITextField的左空间问题
下面的代码可以在我的Ios UITextField的左空间问题,ios,objective-c,uitextfield,uisearchbar,Ios,Objective C,Uitextfield,Uisearchbar,下面的代码可以在我的UITextField中插入一个左空格: @implementation UITextField (custom) - (CGRect)textRectForBounds:(CGRect)bounds { return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 8, bounds.size.width - 20, bounds.size.height - 16);
UITextField
中插入一个左空格:
@implementation UITextField (custom)
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 8,
bounds.size.width - 20, bounds.size.height - 16);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
@end
无论我插入哪个类的代码,我的项目中存在的所有UITextField都将生效(奇怪…)。但问题是这段代码也影响了所有的UISearchBar
(文本和占位符的工作方式不正确,破坏了布局)
如何解决此问题?您可以自定义文本字段:
UITextField *textFiled = [[UITextField alloc]init];
textFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
textFiled.leftViewMode = UITextFieldViewModeAlways;
也许您可以尝试创建UITextField的子类,而不是UITextField的类别
@interface MyTextField : UITextField
@end
@implementation MyTextField
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 8,
bounds.size.width - 20, bounds.size.height - 16);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
@end
我有144个文本字段和2个搜索栏,我有两个解决方案:编辑我所有144个文本字段的所有框架(重复144x)或将此搜索栏转换为UITextField(并继续我的代码)。