我从 UITextField 继承了我的“MFTextField”类。我想使用 Interface Builder 中的 IBInspectable 属性自定义 UITextField 的“rightView”属性的框架。要做到这一点 :
IB_DESIGNABLE
@interface MFTextField : UITextField <UITextFieldDelegate, MFComponentValidationDelegate>
@property (nonatomic) IBInspectable CGRect errorViewFrame;
@end
在我的 MFtextField.m 中:
-(CGRect)rightViewRectForBounds:(CGRect)bounds {
if(CGRectEqualToRect(CGRectZero, self.errorViewFrame)) {
//default position
CGRect rightViewRect = self.errorView.bounds;
rightViewRect.size.height = bounds.size.height - 4;
rightViewRect.origin.y = 2;
rightViewRect.origin.x = bounds.size.width - rightViewRect.size.width - 2;
return rightViewRect;
}
else {
//Custom position
return self.errorViewFrame;
}
}
我声明我的 rightView :
self.errorView = [[[NSBundle bundleForClass:[self class]] loadNibNamed:@"MFErrorView" owner:nil options:nil] firstObject];
self.rightView = self.errorView;
self.rightView = UITextFieldViewModeAlways;
好的。现在我在 IB 上看到了我的 rightView 默认位置,我可以自定义它的框架:
但是当我想更改我的“错误视图框架”IBinspectable 属性的值时,XCode 开始一个无限循环然后崩溃......似乎方法rightViewRectForBounds:现在被 iOS 无限调用......
任何想法 ?