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)
}
相关推荐
cui_ruicheng14 小时前
Linux网络编程(二):网络数据传输基本流程
linux·服务器·网络
微风欲寻竹影14 小时前
Java数据结构——栈(Stack)详解
java·开发语言·数据结构
TechWayfarer14 小时前
网络安全视角:利用IP定位API接口识别机房与基站流量(合规风控篇)
开发语言·网络·数据库·python·安全·网络安全
Makoto_Kimur14 小时前
Java 后端面试场景题:页面刷新后一直转圈,应该怎么排查?
java·开发语言·面试
小陶来咯14 小时前
aimrt中间件的使用
开发语言·qt·中间件
神仙别闹14 小时前
基于C语言实现(控制台)学生信息管理系统
c语言·开发语言
ch.ju15 小时前
Java Programming Chapter 3——Default value of array
java·开发语言
aini_lovee15 小时前
STM32 上实现 SD 卡读取 JPEG 解码 TFT 显示
开发语言·stm32
谙弆悕博士15 小时前
【附C语言源码】C语言 栈结构 实现及其扩展操作
c语言·开发语言·数据结构·算法·链表·指针·
njsgcs15 小时前
c# solidworks GetPartBox无法获得正确实体边界框原因
开发语言·c#·solidworks