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)
}
相关推荐
闲猫32 分钟前
LangChain / Core components / Models
开发语言·python·langchain
-银雾鸢尾-2 小时前
C#中的索引器
开发语言·c#
Haku Coder2 小时前
Netlify 部署静态网站——0~1保姆级操作教程
服务器·netlify·静态网页托管·免费部署
早点睡啊Y2 小时前
深入学 LangChain 官方文档(一):总览、安装与快速开始
linux·服务器·langchain
Qlittleboy3 小时前
PHP的接口参数传递的过多,max_input_vars = 5000
开发语言·php
Aurorar0rua3 小时前
CS50 x 2024 Notes Algorithms - 02
c语言·开发语言·学习方法
liulilittle3 小时前
论双机热备:分布式
服务器·网络·分布式·通信·竞态
en.en..3 小时前
冒泡排序与选择排序完整对比解析
开发语言·数据结构·算法·c#·排序算法
兰令水3 小时前
hot100【acm版】【2026.7.18打卡-java版本】
java·开发语言·算法
优橙教育3 小时前
5G网优工程师证书怎么选:HCSP/HCIA/5G认证全解析
运维·服务器