Swift 中 NavigationController 的切换改变

最近在一个用 Swift 写的项目中,用到了 NavigationController 来进行界面的切换,即用到了 pushViewControllerpopViewController,众所周知,pushViewController 是从右向左载入新界面,而 popViewController 是从左向右退回返回到旧界面。那么是否有可能让它们的方向是相反的呢?

答案是可以,这里用到了 CATransition,直接上代码,pushViewController 部分

let newVC = NewVC()
let transition = CATransition()
transition.duration = 0.3
transition.type = .push
transition.subtype = .fromLeft
self.navigationController?.view.layer.add(transition, forKey: kCATransition)
self.navigationController?.pushViewController(newVC, animated: false)

从上面代码即可以看出将 profileVC 从左向右载入,那么 popViewController 用反方向即可

let transition = CATransition()
transition.duration = 0.3
transition.type = .push
transition.subtype = .fromRight
self.navigationController?.view.layer.add(transition, forKey: kCATransition)
self.navigationController?.popViewController(animated: false)

请注意:这里的内容都是原创,你可以免费阅读,但拒绝所有其他任何形式的转载,谢谢配合。如果你发现文章中有错误,请发邮件给我,非常感谢。

The Copyright of all posts & photos posted in tourcoder.com is belong to tourcoder.com. You can read them for free, but you may not reuse anything therein without my permission.