Search code examples
iosswift3

modalPresentationStyle works strangly


I've a simple question,

Why this works perfectly :

class HomeController: UIViewController,UITableViewDataSource,UITableViewDelegate, AddingDisciplineDelegate, LogoutUserDelegate {
        override func viewDidLoad() {
               super.viewDidLoad()
               let controller = SeePlugAlertModalView()
               controller.modalPresentationStyle = .overFullScreen       
               self.present(controller, animated: true, completion: nil)
        }
        (...)
}

class SeePlugAlertModalView : UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.isOpaque = false
        self.modalTransitionStyle = .crossDissolve
        view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
    }

enter image description here

But if i put the modalPresentationStyle in SeePlugAlertView, my view is presented but hides the parent view controller. ( Even if the background color has an alpha of 0.5).

 class HomeController: UIViewController,UITableViewDataSource,UITableViewDelegate, AddingDisciplineDelegate, LogoutUserDelegate {
                override func viewDidLoad() {
                       super.viewDidLoad()
                       let controller = SeePlugAlertModalView()       
                       self.present(controller, animated: true, completion: nil)
                }
                (...)
        }

class SeePlugAlertModalView : UIViewController {

            override func viewDidLoad() {
                super.viewDidLoad()
                self.view.isOpaque = false
                self.modalPresentationStyle = .overFullScreen
                self.modalTransitionStyle = .crossDissolve
                view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
      }

enter image description here


Solution

  • The problem is that, in your second code, by the time SeePlugAlertModalView's viewDidLoad is called, it's too late; the presentation of SeePlugAlertModalView has already started. So, since you've removed the earlier configuration from HomeController, there is no setting of the presentation style at presentation time, and you're getting .fullScreen (the default).

    The solution is to set SeePlugAlertModalView's presentation style earlier, such as in its initializer or awakeFromNib.