UIBarButtonItem's titleTextAttributes cannot be changed when the item is on screen
Originator: | VW.Dolph | ||
Number: | rdar://35644990 | Date Originated: | Nov 20, 2017 |
Status: | Open | Resolved: | |
Product: | iOS + SDK | Product Version: | iOS 11.1 |
Classification: | Bug | Reproducible: | always |
Summary: UIBarButtonItem.setTitleTextAttributes() has no effect if the item has already appeared on screen. Steps to Reproduce: 1.) put a ViewController inside a NavigationController 2.) in viewDidLoad() add a UIBarButtonItem as the navigationItem.rightBarButtonItem 3.) write a method that sets the titleTextAttributes of the rightBarButtonItem (e.g. different foregroundColor) and can be triggered while the rightBarButtonItem is visible on screen 4.) trigger the method Expected Results: The rightBarButtonItem's titleTextAttributes change. Actual Results: The rightBarButtonItem's titleTextAttributes do not change. At all. Workaround for the bug: Change the rightBarButtonItem's title before or after changing the titleTextAttributes. Sample Code: class ViewController: UIViewController { private var blue = true override func viewDidLoad() { super.viewDidLoad() title = "Test" view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(changeBarButtonColor))) navigationItem.rightBarButtonItem = UIBarButtonItem(title: "GreenButton", style: .plain, target: nil, action: nil) navigationItem.rightBarButtonItem?.setTitleTextAttributes(titleTextAttributes(color: .green), for: .normal) } @objc private func changeBarButtonColor() { navigationItem.rightBarButtonItem?.setTitleTextAttributes(titleTextAttributes(color: blue ? .blue : .green), for: .normal) // include this line and it works again: // navigationItem.rightBarButtonItem?.title = blue ? "BlueButton" : "GreenButton" blue = !blue } private func titleTextAttributes(color: UIColor) -> [NSAttributedStringKey: Any] { return [ .foregroundColor: color ] } }
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!