0

I inherits my "MFTextField" class from UITextField. I want to customize the frame of the "rightView" property of the UITextField with an IBInspectable property from Interface Builder. To do that :

IB_DESIGNABLE
@interface MFTextField : UITextField <UITextFieldDelegate, MFComponentValidationDelegate>

@property (nonatomic) IBInspectable CGRect errorViewFrame;

@end

And in my 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;
    }
}

I Declare my rightView :

self.errorView = [[[NSBundle bundleForClass:[self class]] loadNibNamed:@"MFErrorView" owner:nil options:nil] firstObject];
self.rightView = self.errorView;
self.rightView = UITextFieldViewModeAlways;

Okay. Now i see the my rightView with de default position on IB, and i'm allowed to customize its frame :

In InterfaceBuilder before to customize rightView frame

But when i want to change the value of my "Error view frame" IBinspectable property, XCode begins an infinite loop then crashes... It seems that the method rightViewRectForBounds: is infinitely called by iOS now...

Any idea ?

Community
  • 1
  • 1
QLag
  • 823
  • 1
  • 13
  • 33
  • You have put no reference at where or why the method `rightViewRectForBounds` is used, from your code alone I can tell that it is never called neither by you or by IB. – A-Live Apr 14 '15 at 14:15
  • I do, but i forget to put it in my question. edited. – QLag Apr 14 '15 at 14:41
  • The question still doesn't show how `rightViewRectForBounds` is *supposed* to be called. – A-Live Apr 15 '15 at 06:03
  • Oh yes it does. rightViewRectForBounds is a method of UITextField that is automatically called when a rightView is defined. – QLag Apr 15 '15 at 07:50

1 Answers1

0

Finally i found where was the problem. In my opinion it's an iOS bug : the constraints i defined in my XIB file specify that a "Info Detail Button" must stick the XIB rootView (defining top, left, bottom and right margin between my button and the xib root view).

When i change the first value of my IBInspectable property (i set 1 instead of 0 for X value, for example), the size of my view is to small compared to the constraints defined in the XIB loaded in the rightView : it crashes.

However, when i change the XIB size (for example 2x2 instead of 35x35), it causes no problem to render it.

I decided to change my constraints with : fixed height and fixed width for my Add Button (35x35) and center it horizontally and vertically.

No crash anymore.

QLag
  • 823
  • 1
  • 13
  • 33