3.Swift导航栏的使用

Swift 导航栏的使用

一、基本使用

1.1 创建导航栏

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

swift 复制代码
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 导航栏上添加按钮
swift 复制代码
//添加导航栏左边按钮
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 页面跳转
swift 复制代码
//跳转到下个页面
func buttonClick(button:UIButton) {
  let secondVC = SecondViewController()
  self.navigationController?.pushViewController(secondVC, animated:true)
}
//返回上一个页面
func back() {
  self.navigationController?.popViewControllerAnimated(true)
}
相关推荐
H_老邪1 分钟前
Linux 与 Docker 常用命令
linux·运维·服务器·docker
大尚来也1 分钟前
PHP 反序列化漏洞深度解析:从原理利用到 allowed_classes 防御实战
android·开发语言·php
雕刻刀4 分钟前
ERROR: Failed to build ‘natten‘ when getting requirements to build wheel
开发语言·python
qq_416018724 分钟前
高性能密码学库
开发语言·c++·算法
博语小屋8 分钟前
I/O 多路转接之epoll
运维·服务器·数据库
小碗羊肉13 分钟前
【从零开始学Java | 第十八篇】BigInteger
java·开发语言·新手入门
宵时待雨16 分钟前
C++笔记归纳14:AVL树
开发语言·数据结构·c++·笔记·算法
执笔画流年呀30 分钟前
PriorityQueue(堆)续集
java·开发语言
山川行33 分钟前
关于《项目C语言》专栏的总结
c语言·开发语言·数据结构·vscode·python·算法·visual studio code
呜喵王阿尔萨斯35 分钟前
C and C++ code
c语言·开发语言·c++