Swift仿今日头条的多页面滑动切换

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 应用开发中,通常使用 UINavigationController 来管理多个页面的转换。在使用 UINavigationController 的情况下,你可以通过调用它的 pushViewController(_:animated:) 方法来转换到另一个页面。 举个例子,假设你有一个名为 FirstViewController 的视图控制器,并且要通过按钮点击事件来转换到另一个名为 SecondViewController 的视图控制器。 首先,需要在你的故事板中创建两个场景。并将场景关联到对应的视图控制器。 其次,在 FirstViewController 中添加一个按钮,并在它的点击事件中,调用 UINavigationController 的 pushViewController(_:animated:) 方法,将 SecondViewController 压入导航栈中 ``` swift import UIKit class FirstViewController: UIViewController { @IBOutlet weak var transitionButton: UIButton! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func transitionButtonDidTapped(_ sender: Any) { let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController self.navigationController?.pushViewController(secondViewController, animated: true) } } ``` 这样你就可以通过按钮点击来在两个场景间转换了。 需要注意的是,需要在你的 Appdelegate 里面配置 Navigation 并且设置第一个页面是哪一个. ```swift let firstViewController = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController let navigationController = UINavigationController(rootViewController: firstViewController) self.window?.rootViewController = navigationController
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值