问题
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