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)
}
相关推荐
aramae2 小时前
模拟实现strcmp()(C语言)
c语言·开发语言·后端
Madison-No72 小时前
搭建项目测试环境
linux·运维·服务器·python
泡海椒3 小时前
PDF 表格样式优化:jquick-pdf 边框、圆角、背景色
java·开发语言·pdf
悠悠121384 小时前
微服务通信别只看性能:REST 与 gRPC 的 6 个选型判断和一次 502 故障复盘
运维·服务器
Beyond_System|系统之外4 小时前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
景熙55234 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
云老大-阿里云国际站代理商5 小时前
华为云国际站服务代理商:NAT网关怎么配?多台ECS共用公网IP要注意什么
服务器·华为云·云计算
Bruce_Liuxiaowei7 小时前
Python 实例赋值遮蔽类属性:一个从不报错的静默陷阱
开发语言·python·语法糖
浅念-8 小时前
Redis基础详解:单线程模型、String与Hash数据结构
服务器·数据库·redis·sql·mysql·nosql数据库·nosql
aramae8 小时前
MySQL内置函数(7)
开发语言·笔记·后端·mysql·其他