3.Swift导航栏的使用

Swift 导航栏的使用

一、基本使用

1.1 创建导航栏

在AppDelegate 如下方法中添加创建导航栏的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
           
    self.window?.backgroundColor = UIColor.white
    let main = ControlMainController()
    let navigation = UINavigationController(rootViewController: main)
    self.window?.rootViewController = navigation
    return true
}
1.2 导航栏上添加按钮
//添加导航栏左边按钮
let item1 = UIBarButtonItem(title: "我的", style: UIBarButtonItem.Style.plain, target: self, action: #selector(buttonClick))
self.navigationItem.leftBarButtonItem = item1

//添加导航栏右边按钮
let item2 = UIBarButtonItem(title: "设置", style: UIBarButtonItem.Style.plain, target: self, action: #selector(buttonClick))
self.navigationItem.rightBarButtonItem = item2

//添加多个按钮
let item1 = UIBarButtonItem(title: "我的", style: UIBarButtonItem.Style.plain, target: self, action: #selector(buttonClick))
let item2 = UIBarButtonItem(title: "设置", style: UIBarButtonItem.Style.plain, target: self, action: #selector(buttonClick))
self.navigationItem.rightBarButtonItems = [item1, item2]
1.3 页面跳转
//跳转到下个页面
func buttonClick(button:UIButton) {
   
  let secondVC = SecondViewController()
  self.navigationController?.pushViewController(secondVC, animated:true)
}
//返回上一个页面
func back() {
   
  self.navigationController?.popViewControllerAnimated(true)
}

相关推荐

  1. 3.Swift导航使用

    2024-02-23 04:08:06       53 阅读
  2. css折叠导航

    2024-02-23 04:08:06       27 阅读
  3. uniapp导航组件如何使用

    2024-02-23 04:08:06       56 阅读
  4. uniapp——自定义导航封装

    2024-02-23 04:08:06       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-23 04:08:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-23 04:08:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-23 04:08:06       87 阅读
  4. Python语言-面向对象

    2024-02-23 04:08:06       96 阅读

热门阅读

  1. 【Swift】NSPopUpButton用法和示例

    2024-02-23 04:08:06       57 阅读
  2. JBOSS EPA 7.X 接入Oracle数据源

    2024-02-23 04:08:06       49 阅读
  3. Leetcode | 231. 2 的幂 C语言

    2024-02-23 04:08:06       59 阅读
  4. QT 如何让多语言翻译变得简单,提高效率?

    2024-02-23 04:08:06       51 阅读
  5. Springcloud OpenFeign 的实现(二)

    2024-02-23 04:08:06       56 阅读