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)
}
相关推荐
yaoxin5211232 小时前
384. Java IO API - Java 文件复制工具:Copy 示例完整解析
java·开发语言·python
开压路机2 小时前
进程控制
linux·服务器
NotFound4863 小时前
实战指南如何实现Java Web 拦截机制:Filter 与 Interceptor 深度分享
java·开发语言·前端
2401_873479403 小时前
如何从零搭建私有化IP查询平台?数据采集、清洗、建库到API发布全流程
服务器·网络·tcp/ip
Ava的硅谷新视界3 小时前
用了一天 Claude Opus 4.7,聊几点真实感受
开发语言·后端·编程
rabbit_pro3 小时前
Python调用onnx模型
开发语言·python
浪客川4 小时前
【百例RUST - 010】字符串
开发语言·后端·rust
代码中介商5 小时前
Linux 帮助手册与用户管理完全指南
linux·运维·服务器
赵侃侃爱分享5 小时前
学完Python第一次写程序写了这个简单的计算器
开发语言·python
断眉的派大星5 小时前
# Python 魔术方法(魔法方法)超详细讲解
开发语言·python