如何使用navBar和自定义大小创建模态?

问题描述 投票:1回答:1

我需要创建具有自定义大小的自定义modalVC(例如:CGSize(width: 100, height: 100)和坐标)。 ModalVC可能不像popover(popover有圆角)。

将出现带有“Done”项的导航栏,用于关闭此modalVC。

VC将为其他VC中的UIButton创建。

有我的代码(由其他代码创建,只有popover存在):

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:我知道如何像PopOver那样做,但不像自定义模式。

ios swift viewcontroller modalviewcontroller
1个回答
2
投票

试试这个

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)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.