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)
}
相关推荐
Python×CATIA工业智造1 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm
我叫小白菜2 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
狐凄2 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python
weixin_446122463 小时前
JAVA内存区域划分
java·开发语言·redis
悦悦子a啊3 小时前
Python之--基本知识
开发语言·前端·python
QuantumStack4 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
whoarethenext4 小时前
使用 C++/OpenCV 和 MFCC 构建双重认证智能门禁系统
开发语言·c++·opencv·mfcc
(:满天星:)5 小时前
第31篇:块设备与字符设备管理深度解析(基于OpenEuler 24.03)
linux·运维·服务器·网络·centos
小陶来咯5 小时前
【仿muduo库实现并发服务器】Acceptor模块
运维·服务器
爱莉希雅&&&5 小时前
shell编程之awk命令详解
linux·服务器·git