How create modalVC with navBar and custom size?

ε祈祈猫儿з 提交于 2020-01-03 02:21:11

问题


I need to create custom modalVC with custom size ( ex: CGSize(width: 100, height: 100) and coordinates). ModalVC may be NOT like popover( popover has rounded corners).

There will be navigation bar with item "Done" for dismiss this modalVC.

VC will create for UIButton pressing in other VC.

There is my code(created of other code, only popover present):

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    @IBOutlet var button: UIButton!
    @IBAction func buttonPressed(sender: UIButton) {
        showModalVC()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func showModalVC() {
        let modalVC = CustomModalViewController()
        modalVC.modalPresentationStyle = .popover
        if let modal = modalVC.popoverPresentationController {
            modal.delegate = self
            modal.sourceView = self.view
        }
        self.present(modalVC, animated: true, completion: nil)
    }
}

class CustomModalViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.preferredContentSize = CGSize(width: UIScreen.main.bounds.width,
                                           height: UIScreen.main.bounds.height/3)
    }
}

P.S: I know how to do this like PopOver, but not like custom modal.


回答1:


Try this

let VC = self.storyboard!.instantiateViewController(withIdentifier: "Identifier") as! YourViewController
let navController = UINavigationController(rootViewController: VC)
navController.preferredContentSize = CGSize(width: 500, height: 400)
navController.modalPresentationStyle = .formSheet
self.present(navController, animated:true, completion: nil)



来源:https://stackoverflow.com/questions/49067471/how-create-modalvc-with-navbar-and-custom-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!