How to change the size of a popover

前端 未结 6 1165
庸人自扰
庸人自扰 2020-12-29 20:17

I\'m having trouble changing the size of my popover presentation. Here is what I have so far

 override func prepareForSegue(segue: UIStoryboardSegue, sender:         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 21:01

    I'm not using storyboards. I just present a UINavigationController in the popover:

        self.present(popoverNavigationController!, animated: true) {}
    

    The way to resize the popover size when a new view controller is pushed, it is just change the preferredContentSize before pushing it. For example:

        let newViewController = NewViewController()
        popoverNavigationController!.preferredContentSize = CGSize(width: 348, height: 400)
        popoverNavigationController!.pushViewController(newViewController, animated: true)
    

    The problem is when we try to resize the popover when we pop a view controller.

    If you use viewWillDisappear of the current view controller to change the preferredContentSize of the popover, the popover will resize but after the view controller is popped. That means that the animation has a delay.

    You have to change the preferredContentSize before executing popViewController. That's mean you have to create a custom back button in the navigation bar like it is explained here. This is the implementation updated for Swift 4:

            self.navigationItem.hidesBackButton = true
            let newBackButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(CurrentViewController.backButtonTapped(sender:)))       
            self.navigationItem.leftBarButtonItem = newBackButton
    

    And run the next code when the new Back button is pressed:

       @objc func backButtonTapped(sender: UIBarButtonItem) {
    
            self.navigationController?.preferredContentSize = CGSize(width: 348, height: 200)
    
            self.navigationController?.popViewController(animated: true)
       }
    

    Basically, the preferredContentSize has to be changed before pushing and popping the view controller.

提交回复
热议问题