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)
}
相关推荐
编织幻境的妖12 小时前
Python代码性能优化工具与方法
开发语言·python·性能优化
Fcy64812 小时前
二叉搜索树(C++实现)
开发语言·数据结构·c++·二叉搜索树
程序员-周李斌12 小时前
ArrayBlockingQueue 源码解析
java·开发语言·后端·哈希算法·散列表
Tim_1012 小时前
【C++入门】02、C++程序初识
开发语言·c++
lkbhua莱克瓦2413 小时前
项目知识——Next.js App Router体系
开发语言·javascript·项目知识
Cricyta Sevina13 小时前
Java 语言多线程核心概念全解析
java·开发语言
爱打代码的小林13 小时前
python基础(pandas库)
服务器·python·pandas
缘三水13 小时前
【C语言】15.指针(5)
c语言·开发语言·指针·语法
爱吃大芒果13 小时前
从零开始学 Flutter:状态管理入门之 setState 与 Provider
开发语言·javascript·flutter