iOS几种转场的方法

1.按钮跳转
storyboard上直接control拖拽

2.present

1.设置好Storyboard ID
在这里插入图片描述

2.在需要跳转的地方用如下代码即可

 let sb = UIStoryboard(name: "Main", bundle: Bundle.main)
 let vc = sb.instantiateViewController(identifier: "test")
 self.present(vc, animated: true, completion: nil)

在iOS13以及Xcode11中,viewcontroller的modalPresentationStyle属性由之前延续了很久的fullScreen方式变成了automatic

所以如果在使用Xcode11开发时遇到设计或者动画上的bug的话,可以考虑强制把转场的展示方式变为fullScreen:

 let sb = UIStoryboard(name: "Main", bundle: Bundle.main)
 let vc = sb.instantiateViewController(identifier: "test")
 vc.modalPresentationStyle = .fullScreen
 self.present(vc, animated: true, completion: nil)

需要传值的话直接用vc来传值

let sb = UIStoryboard(name: "Main", bundle: Bundle.main)
let vc = sb.instantiateViewController(identifier: "test") as! ViewController2
vc.text = "hello"
self.present(vc, animated: true, completion: nil)

3.performSegue

1.在storyboard上control拖拽当前页面(下图左)的黄色圆圈,拖到想跳转的页面,并按项目需求选择一个跳转方式(比如:show)

在这里插入图片描述

2.选中segue(下图蓝色箭头),在右边attributes inspector面板给他取个名字

在这里插入图片描述

3.回到当前页面的代码区域,在需要跳转的地方用如下代码即可

performSegue(withIdentifier: "showDetail", sender: nil)

在iOS中,所有的segue(包括页面跳转,页面嵌入等)在跳转或嵌入之前都会先触发viewcontroller的这个方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 

如果要传值,可以利用这里的sender

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetail" {
        let vc = segue.destination as! ViewController2
        vc.text = (sender as! String)
    }
}
发布了19 篇原创文章 · 获赞 8 · 访问量 1456

猜你喜欢

转载自blog.csdn.net/qq_44864362/article/details/104096015
今日推荐