How do you present a UIViewController from the top of the screen instead of the bottom?

后端 未结 5 2185
不思量自难忘°
不思量自难忘° 2021-01-03 13:10

I am using a UIViewController and I use presentModalViewController:controllerr animated:YES to present it but I would like if it would slide down from the top of the screen

5条回答
  •  一向
    一向 (楼主)
    2021-01-03 13:12

    public extension UINavigationController {
    
        func pushViewControllerFromTop(viewController vc: UIViewController) {
            vc.view.alpha = 0
            self.present(vc, animated: false) { () -> Void in
                vc.view.frame = CGRect(x: 0, y: -vc.view.frame.height, width: vc.view.frame.width, height: vc.view.frame.height)
                vc.view.alpha = 1
                UIView.animate(withDuration: 1, 
                               animations: { () -> Void in
                                   vc.view.frame = CGRect(x: 0, y: 0, width: vc.view.frame.width, height: vc.view.frame.height)
                               }, 
                               completion: nil)
            }
        }
    
        func dismissViewControllerToTop() {
            if let vc = self.presentedViewController {
                UIView.animate(withDuration: 1, 
                               animations: { () -> Void in
                                   vc.view.frame = CGRect(x: 0, y: -vc.view.frame.height, width: vc.view.frame.width, height: vc.view.frame.height)
                               }, 
                               completion: { complete -> Void in
                                   if complete {
                                       self.dismiss(animated: false, completion: nil)
                                   }
                               })
            }
        }
    }
    

提交回复
热议问题