代码旅行

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)

--- EOF ---

除特别注明外,所有文章均采用Creative Commons BY-NC-ND 4.0(自由转载-保持署名-非商用-禁止演绎)协议发布。