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)
}
相关推荐
赵谨言10 分钟前
基于python二手车价值评估系统的设计与实现
大数据·开发语言·经验分享·python
Ghost Face...15 分钟前
深入解析U-Boot命令系统
linux·运维·服务器
心灵宝贝17 分钟前
申威服务器安装Nacos 2.0.3 RPM包详细步骤(Kylin V10 sw_64架构)附安装包
服务器·架构·kylin
ajax_beijing19 分钟前
当同一个弹性云服务器所在子网同时设置了snat和弹性公网IP时,会优先使用哪个
linux·运维·服务器
随风语21 分钟前
云计算与服务器
运维·服务器·云计算
wanhengidc22 分钟前
服务器受到网络攻击该怎么办
服务器·arm开发·智能手机·玩游戏
wanhengidc22 分钟前
服务器会遭受到哪些网络攻击
运维·服务器
安当加密23 分钟前
基于ASP身份认证服务器实现远程办公VPN双因素认证的架构与实践
java·服务器·架构
轮子大叔23 分钟前
如何自建内网穿透(FRP)服务器
运维·服务器
Hali_Botebie29 分钟前
服务器上用Slurm 管理训练bash 脚本任务,申明使用GPU
服务器·chrome·bash