dperk dperk - 6 months ago 26
Objective-C Question

UITextField has black insets when width constraint animated

So I'm changing a

UITextField
's width constraint by calling
[self viewLayoutIfNeeded]
in
[UIView animateWithDuration:]
. The text field width constraint's starting width is 20, then I reset it to 250, and call
viewLayoutIfNeeded
. When that happens, it resizes appropriately, but with strange black insets on the sides. I'm not working with shadows or anything. Has anyone dealt with this or have a solution?

The views are set in my storyboard, and I dragged an
NSLayoutConstraint
for the width constraint, which is the only property being mutated.

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textFieldWidthConst;
@end

@implementation ViewController
-(void)viewDidAppear:(BOOL)animated {

[self.view layoutIfNeeded];

_textFieldWidthConst.constant = 275;

[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{

[self.view layoutIfNeeded];

} completion: nil
}];
}
@end


enter image description here

Answer

Set the text field's content mode to “Redraw”:

View content mode in Interface Builder

If you want to do it in code, set the text field's contentMode property to UIViewContentModeRedraw.

Comments