如何在leftBarButtonItem的pushViewController上更改动画方向?

时间:2018-12-11 12:46:39

标签: swift navigation uinavigationbar uibarbuttonitem swift4.2

我已将leftBarButtonItem添加到NavigationBar(与NavigationController结合使用)。 leftBarButtonItem在顶部的ViewController上。

当我使用pushViewController函数去下一个ViewController时,动画是从右到左,与按下导航栏右侧的按钮一样。

如何像在“ Tinder”中按下导航栏上的向左按钮一样,将动画方向从左向右移动?

代码如下:

let profileButton = UIBarButtonItem(image: #imageLiteral(resourceName: "profile"), style: .plain, target: self, action: #selector(self.showProfile))
    self.navigationItem.leftBarButtonItem = profileButton

@objc func showProfile() {
    let profileViewController = storyboard?.instantiateViewController(withIdentifier: "profile") as! ProfileViewController
    self.navigationController?.pushViewController(profileViewController, animated: true)

2 个答案:

答案 0 :(得分:0)

您不能更改UINavigationController的方向。 为了实现这一点,您必须自己创建它。 如果要实现菜单,可以签出SideMenu

答案 1 :(得分:0)

要在推送到另一个viewController时进行动画处理,可以使用 CATransition

@objc func showProfile() {
    let profileViewController = storyboard?.instantiateViewController(withIdentifier: "profile") as! ProfileViewController
    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    transition.type = CATransitionType.push
    transition.subtype = CATransitionSubtype.fromLeft       
    navigationController?.view.layer.add(transition, forKey: kCATransition)
    self.navigationController?.pushViewController(profileViewController, animated: true)
}

您可以将动画方向更改为 fromRight,fromTopfromBottom