3

I am trying to set the title of a UIButton using [button setTitle:changedTitle forState:UIControlStateNormal] The title is changing correctly but internally this appears to animated by UIButton and when changing from a long title to a short title there seems to be a layout pass on the UIButton label with an intrinsicContentSize based on the final string length but before the text in the UIButton's label has been changed. The result is that I can see a momentary contraction of the long title in the UIButton before the short title is set. eg.

To start:

LooooongTiiiiiiitle

during the fading animation becomes for a fraction of a second:

Lon...tle

before landing at:

Short

I am not doing any other animations on the view and have been able to recreate this effect in a new project with nothing but a view with one UIButton.

Is this a UIKit bug or am I doing something wrong?

Dallas Johnson
  • 1,546
  • 10
  • 13

1 Answers1

0

The phenomenon is actually easier to see if you add a delay so that you are not changing the button label during the tapping of the button:

@interface ViewController ()
@property (nonatomic,assign) BOOL longString;
@end

@implementation ViewController

- (IBAction)changeLabel:(UIButton *)sender {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSString *text = self.longString ? @"aaaaaaaaaaaaaaaa": @"bbbbb";
        [sender setTitle:text forState:UIControlStateNormal];
        self.longString = !self.longString;
    });
}

I would say go ahead and file a bug report.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    Thanks. I know it's a pretty subtle bug that's not going to ruin anyone's day but I thought I'd put it out there in case I was doing something stupid. I'll file a bug report but won't hold my breath waiting for a fix. Apple have plenty of more important bugs to fix first. – Dallas Johnson Aug 19 '15 at 08:35