Push Up View Controller from Bottom using Swift

后端 未结 6 1288
一生所求
一生所求 2021-01-31 11:46

I would like to push a view controller using Swift and animate it so it appears from the bottom and moves up. I have the following code to push my view controller:



        
6条回答
  •  醉梦人生
    2021-01-31 12:43

    Swift3:

    for push :

        // push view controller but animate modally
        let storyBoard: UIStoryboard = UIStoryboard(name: "myStoryBoard", bundle: nil)
        let vc = storyBoard.instantiateViewController(withIdentifier: "myViewControllerIdentifier") as! MyViewController
    
        let navigationController = self.navigationController
    
    
        vc.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: vc, action: #selector(vc.closeView))
        vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: vc, action: nil)
    
        let transition = CATransition()
        transition.duration = 0.5
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        transition.type = kCATransitionMoveIn
        transition.subtype = kCATransitionFromTop
        navigationController?.view.layer.add(transition, forKey: nil)
        navigationController?.pushViewController(vc, animated: false)
    

    and in vc for pop:

    func closeView() {
        let transition = CATransition()
        transition.duration = 0.5
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        transition.type = kCATransitionReveal
        transition.subtype = kCATransitionFromBottom
        navigationController?.view.layer.add(transition, forKey: nil)
        _ = navigationController?.popViewController(animated: false)
    }
    

提交回复
热议问题