I’ve a button with 2 icons and a label. The button has a distinct background colour for .chosen state. After I click on the button, the button is chosen and a brand new ViewController is pushed. I am attempting to imitate the behaviour of UITableViewCell.setSelected(animated:), so when the consumer backs out from the brand new ViewController I would like the background colour to animate. I take advantage of this:
UIView.transition(with: button, period: 0.3, choices: .transitionCrossDissolve) { button.isSelected = false }
It really works positive once I put it in viewDidAppear however it is a bit too late so I would like it in viewWillAppear. Downside is once I do it from viewDidAppear, the label on my button begins as clear. The icons are positive and static however the label will animate from clear to its colour. Why? How I can forestall this?
Here’s a small demo:
import UIKit
class ViewController: UIViewController {
var myButton: MyButton?
override func viewDidLoad() {
tremendous.viewDidLoad()
myButton = MyButton()
view.addSubview(myButton!)
myButton?.body.origin.x = 200
myButton?.body.origin.y = 200
myButton?.addAction(.init { [weak self] _ in
self?.myButton?.isSelected = true
self?.navigationController?.pushViewController(ViewController2(), animated: true)
}, for: .touchUpInside)
}
override func viewWillAppear(_ animated: Bool) {
tremendous.viewWillAppear(animated)
UIView.transition(with: myButton!, period: 3, choices: .transitionCrossDissolve) { self.myButton!.isSelected = false }
}
// override func viewDidAppear(_ animated: Bool) {
// tremendous.viewDidAppear(animated)
// UIView.transition(with: myButton!, period: 3, choices: .transitionCrossDissolve) { self.myButton!.isSelected = false }
//
// }
}
class ViewController2: UIViewController {
override func viewDidLoad() {
tremendous.viewDidLoad()
view.backgroundColor = .white
}
}
class MyButton: UIButton {
let label = UILabel("Hahahaha", font: .systemFont(ofSize: 14), textColor: .black)
comfort init() {
self.init(body: .init(x: 0, y: 0, width: 200, top: 200))
setBackgroundColor(colour: .white, forState: .regular)
setBackgroundColor(colour: .grey, forState: .highlighted)
setBackgroundColor(colour: .grey, forState: .chosen)
addSubview(label)
label.body.origin = .init(x: 40, y: 40)
}
}
extension UIButton {
func setBackgroundColor(colour: UIColor, forState: UIControl.State) {
self.clipsToBounds = true
UIGraphicsBeginImageContext(CGSize(width: 1, top: 1))
if let context = UIGraphicsGetCurrentContext() {
context.setFillColor(colour.cgColor)
context.fill(CGRect(x: 0, y: 0, width: 1, top: 1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.setBackgroundImage(colorImage, for: forState)
}
}
}
extension UILabel {
comfort init(_ textual content: String = "", font: UIFont? = nil, textColor: UIColor? = .black) {
self.init()
self.textual content = textual content
self.font = font
sizeToFit()
self.textColor = textColor
}
}
Strive backing out from ViewController2 , see label begins invisible. Now remark out the viewWillAppear and use viewDidAppear as a substitute, the label begins black.