I've been experimenting and practicing building apps programmatically and just can't understand why my UIButton from the root VC remains in the next view after a modal segue. When I did it through the storyboard, all of the elements in the view would disappear.
My rootVC is made of one layer and a UIButton over it:
pressing the button modally presents the next view controller, but the UIButton remains:
There are no references to the UIButton in the second VC file. Here's the code from the rootVC:
var imInChinaButton : UIButton {
var button = UIButton( frame: CGRect(
x: 0,
y: 0,
width: 300,
height: 300))
button.setImage(#imageLiteral(resourceName: "button"), for: .normal)
button.setTitle("hello", for: .normal)
button.contentMode = UIViewContentMode.scaleAspectFit
button.center = view.center
button.backgroundColor = .clear
button.isUserInteractionEnabled = true
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
return button
}
self.view.addSubview(imInChinaButton)
}
@objc func buttonTapped(_ sender: UIButton) {
sender.setImage(#imageLiteral(resourceName: "pressedButton"), for: .normal)
let resultsViewController = ResultsViewController()
self.navigationController!.present(resultsViewController, animated: true, completion: nil)
}
Any help would be awesome! Thank you so much for your time.
You need to change background of modal , in viewDidLoad
self.view.backgroundColor = .red
//
If you no longer need the backVC , you can
UIApplication.shared.keyWindow?.rootViewController = ResultsViewController()
but hiding temporarily is not possible you can do this to hide them before present but it's not a good practise
self.view.subviews.forEach { $0.isHidden = true }
the best thing is to change the backgroundColor or completely clear the old rootVC
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.