titleLabel frame isn't updated correctly when setTitle: forState: is called when button uses Auto Layout

Originator:inbox
Number:rdar://27246380 Date Originated:08/07/2016
Status:Open Resolved:
Product:iOS SDK Product Version:iOS 8.1 & 9.3
Classification: Reproducible:Always
 
Summary:
The frame of a buttons `titleLabel` reports an incorrect value after `setTitle: forState:` is called.

This is only an issue when using Auto Layout to position the button within a view.

Steps to Reproduce:
- Open & run UIButton-Issue sample project.
- See the button layout is correct with a correct value printed to the console.
- Tap the only button on the view.
- See the incorrect width of the `titleLabel` logged to the console.
- Tap the button again and see an incorrect layout matched with incorrect `titleLabel` width printed in the console.
- Now toggle on the "Apply fix" switch.
- Tap the button again and see the correct layout with correct width of the `titleLabel`.
- Tap the button again so the button's title is set to the shorter text and see the correct widths again.

Expected Results:
The frame of the `titleLabel` of the button should report the correct frame after a title is set.

Actual Results:
The frame of the `titleLabel`reports incorrect values when queried after setting the title of the button.

Version:
iOS 8.1 & 9.3

Notes:


Configuration:
iPhone simulator

Attachments:
'UIButton-Issue.zip', 'UIButton-issue.gif' and 'UIButton-issue.mov' were successfully uploaded.

Potential fix:
Remove the animation of setting the title and force a layout before redrawing view.

```
override func setTitle(title: String?, forState state: UIControlState) {
    UIView.performWithoutAnimation { () -> Void in
        super.setTitle(title, forState: state)
        self.layoutIfNeeded()
        self.setNeedsDisplay()
    }
}
```

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!