How to present a view controller with smaller size

可紊 提交于 2020-01-01 13:19:32

问题


I want presented view controller vc2 in smaller size than the screen, how to do that in Swift 3 ?? Thanks for help. This is my code:

@IBAction func leftButtonPressed(_ sender: UIButton) {
    let vc2 = self.storyboard?.instantiateViewController(withIdentifier: "Controller2") as! ViewController2
    vc2.modalPresentationStyle = .currentContext

    present(vc2, animated: true, completion: nil)
}

回答1:


Try this..

@IBAction func leftButtonPressed(_ sender: UIButton) {
    let vc2 = self.storyboard?.instantiateViewController(withIdentifier: "Controller2") as! ViewController
    vc2.providesPresentationContextTransitionStyle = true
    vc2.definesPresentationContext = true
    vc2.modalPresentationStyle=UIModalPresentationStyle.overCurrentContext
    self.present(vc2, animated: true, completion: nil)

    // Make sure your vc2 background color is transparent
    vc2.view.backgroundColor = UIColor.clear
}



回答2:


You can simply use a container view to display the smaller view controller. Here is a great tutorial on container views: https://cocoacasts.com/managing-view-controllers-with-container-view-controllers/

Hope this was helpful :)



来源:https://stackoverflow.com/questions/44663002/how-to-present-a-view-controller-with-smaller-size

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