PopupDialogCustomExample
PopupDialogCustomExample copied to clipboard
View Controller Doesn't Appear
A view controller doesn't appear when the popup is presented with the following code - just a blur view. What am I missing here?
View Controller A:
func showPopup() {
let vc = ViewControllerB()
let popup = PopupDialog(viewController: vc)
vc.popup = popup
self.present(popup, animated: true, completion: nil)
}
View Controller B:
class ViewControllerB: UIViewController {
var popup: PopupDialog?
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
lazy var card: UIView = {
let view = UIView()
view.backgroundColor = .white
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
func setupView() {
view.addSubview(card)
card.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
card.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
card.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
card.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
Hey there! As you don't have any elements inside your view, the height ends up being 0. If you want to test an empty view, you would have to set a specific height constraint. Make sure to have a seamless chain of constraints from top to bottom, once you add elements. This way, the dialog height will adapt to the height of the elements inside the view.
By the way, make sure to have a weak reference to PopupDialog in your view controller, otherwise it will never be deallocated.
weak var popup: PopupDialog?
Adding this to the CustomViewController made it show up for me:
override func viewDidLoad() {
super.viewDidLoad()
// Setup constraints
self.view.heightAnchor.constraint(equalToConstant: 220).isActive = true
}
A seamless chain of constraints from top to bottom! Thank god!