dperk dperk - 10 months ago 64
Objective-C Question

UITextField has black insets when width constraint animated

So I'm changing a

's width constraint by calling
[self viewLayoutIfNeeded]
[UIView animateWithDuration:]
. The text field width constraint's starting width is 20, then I reset it to 250, and call
. 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
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;

@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

enter image description here


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.