I’ve a button with 2 icons and a label. The button has a special background shade for .chosen state. After I click on the button, the button is chosen and a brand new ViewController is pushed. I am making an attempt to imitate the behaviour of UITableViewCell.setSelected(animated:), so when the person backs out from the brand new ViewController I need the background shade to animate. I take advantage of this:
UIView.transition(with: button, period: 0.3, choices: .transitionCrossDissolve) { button.isSelected = false }
It really works fantastic once I put it in viewDidAppear however it’s kind of too late so I need it in viewWillAppear. Drawback is once I do it from viewDidAppear, the label on my button begins as clear. The icons are fantastic and static however the label will animate from clear to its shade. Why? How I can stop 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(shade: .white, forState: .regular)
setBackgroundColor(shade: .grey, forState: .highlighted)
setBackgroundColor(shade: .grey, forState: .chosen)
addSubview(label)
label.body.origin = .init(x: 40, y: 40)
}
}
extension UIButton {
func setBackgroundColor(shade: UIColor, forState: UIControl.State) {
self.clipsToBounds = true
UIGraphicsBeginImageContext(CGSize(width: 1, top: 1))
if let context = UIGraphicsGetCurrentContext() {
context.setFillColor(shade.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
}
}
Attempt backing out from ViewController2 , see label begins invisible. Now remark out the viewWillAppear and use viewDidAppear as a substitute, the label begins black.