I have embedded two navigation controllers in a tab bar controller. The two navigation controllers both have a view controller. The two view controllers both have a right bar button item, and the two bar button items have been set customView with a static var sharedView, which conforms to a UIView inheritance. The sharedView has been set to yellow color.
I expect the two bar button items always show yellow color. However, when switching between the two tabs, the navigation bar button item will disappear once in every four switches.
I have made a sample project replicating this issue: https://github.com/lenameow/TestCustomView
Relevant code:
import Foundation import UIKit
class SharedView: UIView {
static var sharedView = SharedView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
func setupUI(button: UIBarButtonItem) {
SharedView.sharedView.backgroundColor = UIColor.yellow
button.customView = SharedView.sharedView
}
}
class ViewController: UIViewController {
@IBOutlet weak var testBarButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
SharedView.sharedView.setupUI(button: testBarButton)
}
}
class SecondViewController: UIViewController {
@IBOutlet weak var testBarButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
SharedView.sharedView.setupUI(button: testBarButton)
}
}
Thanks for any inputs.
Finally I found a way to bypass this issue by replacing the static var with separate variables in each VC. However, it is still unknown why using a static var would cause that phenomenon. I have submitted a report to Apple regarding this issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.