Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift - 导航条(UINavigationBar)的使用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

与导航控制器(UINavigationController)同时实现导航条和页面切换功能不同。 导航条(UINavgationBar)可以单独使用,添加至任何的UIView中。UINavigationBar比较重要的属性为,左侧按钮,中间的标题,以及右侧按钮。 下面是一个使用样例,点击左侧加号会添加一个新的导航项,点击右侧Cancel会移除当前最上层导航项。 1 2 3 4 5 6 7 8 9 1
与导航控制器(UINavigationController)同时实现导航条和页面切换功能不同。
导航条(UINavgationBar)可以单独使用,添加至任何的UIView中。UINavigationBar比较重要的属性为,左侧按钮,中间的标题,以及右侧按钮。

下面是一个使用样例,点击左侧加号会添加一个新的导航项,点击右侧Cancel会移除当前最上层导航项。

1
2
3
4
5
6
@H_675_46@ 7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import UIKit
class ViewController : UIViewController {
var count = 0
@H_673_179@ //声明导航条
navigationBar: UINavigationBar ?
@H_403_181@override func viewDidLoad() {
super .viewDidLoad()
// Do any additional setup after loading the view,typically from a nib.
//实例化导航条
navigationBar = (frame: CGRectMake (0,320,44))
self .view.addSubview(navigationBar!)
onAdd()
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@H_618_232@
//增加导航项函数
onAdd(){
count++
//给导航条增加导航项
navigationBar?.pushNavigationItem(onMakeNavitem(),animated: true )
}
//删除导航项函数
onRemove(){
if count > 1{
//减少导航项数量
@H_70_262@count--
//从导航条中移除最后一个导航项
navigationBar?.popNavigationItemAnimated( )
}
}
//创建一个导航项
onMakeNavitem()-> UINavigationItem {
navigationItem = ()
//创建左边按钮
leftBtn = UIBarButtonItem (barButtonSystemItem: UIBarButtonSystemItem . Add ,
target: :1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,action: "onAdd" )
//创建右边按钮
rightBtn = Cancel :1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"onRemove" )
//设置导航栏标题
@H_944_325@navigationItem.title = "第\(count)个导航项"
//设置导航项左边的按钮
navigationItem.setLeftBarButtonItem(leftBtn,monospace!important; min-height:inherit!important">)
//设置导航项右边的按钮
navigationItem.setrightBarButtonItem(rightBtn,monospace!important; min-height:inherit!important">)
return navigationItem
}
}

大佬总结

以上是大佬教程为你收集整理的Swift - 导航条(UINavigationBar)的使用全部内容,希望文章能够帮你解决Swift - 导航条(UINavigationBar)的使用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。