Swift - 工具条UIToolbar的用法

(本文代码升级至Swift3)

1,UIBarButtonItem是工具条按钮,有如下5种init初始化方法
(1)初始化为普通图片按钮
1
2
3
4
5
6
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
import UIKit
class ViewController : UIViewController {
override func viewDidLoad() {
super .viewDidLoad()
// 创建一个浏览器工具条,并设置它的大小和位置
let browserToolbar = UIToolbar (frame: CGRect (x:0,y:20,width:320,height:44))
// 将工具条添加到当前应用的界面中
self .view.addSubview(browserToolbar)
//创建后退按钮
btnback = UIBarButtonItem (image: UIImage (named: "back.png" ),
style: .plain,target: ,
action:#selector( .backClick(_:)))
//第一个分隔按钮
btngap1 = UIBarButtonItem (barButtonSystemItem:.flexibleSpace,
target: nil ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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: )
// 创建前进按钮 UIBarButtonItem
btnforward = (image: UIImage "forward.png" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none rgb(249,
.forwardClick(_:)))
// 第二个分隔按钮,创建一个可伸缩的UIBarButtonItem
btngap2 = ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none rgb(249,
)
//添加按钮
browserToolbar.setItems([btnback,btngap1,btnforward,btngap2],animated: false )
}
backClick(_ sender: ) {
//后退
print ( "后退按钮点击" )
}
forwardClick(_ sender: ) {
//前进
"前进按钮点击" )
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

(2)普通图片按钮,但提供另一个备选图像供横屏使用
1
2
4
"back1.png" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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,
landscapeImagePhone: "back2.png" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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,
style:.plain,
.backClick(_:)))

(3)普通文本按钮
2
(title: "返回" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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,style:.plain,
.backClick(_:)))

(4)使用预置按钮:如撤销,重做,编辑等(总共有25个样式)
Custom认样式
Flexible Space自动伸缩的空白占位符
Fixed Space:固定宽度的占位元素
Add:“+”加号样式
Edit:Edit文字样式
Done:Done文字样式
Cancel:Cancel文字样式
Save:Save文字样式
Undo:Undo文字样式
Redo:Redo文字样式
Compose:背景矩形插支笔的图标样式
Reply:返回箭头的图标样式
Action:背景矩形加上一个向上箭头的图标样式
Organize文件夹的图标样式
Trash垃圾桶的图标样式
Bookmarks:书的图标样式
Search:放大镜的图标样式
Refresh:顺时针圆形箭头的图标样式
Stop一个×的图标样式
Camera一个照相机的图标样式
Play:向右三角形代表播放的图标样式
Pause:两条竖线代表播放的图标样式
Rewind:向左两个三角代表回退的图标样式
Fast Forward:向右两个三角代表快进的图标样式
Page Curl页面旋转的图标样式。在Xcode 6中似乎不显示
示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import UIKit
class ViewController : UIViewController {
override func viewDidLoad() {
super .viewDidLoad()
// 创建一个浏览器工具条,并设置它的大小和位置
let browserToolbar = UIToolbar (frame: CGRect ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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,height:44))
// 将工具条添加到当前应用的界面中
self .view.addSubview(browserToolbar)
btn1 = UIBarButtonItem (barButtonSystemItem:.compose,
target: nil ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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: );
btn2 = (barButtonSystemItem:.add,
);
btn3 = UIBarButtonItem ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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,
);
btn4 = (barButtonSystemItem:.reply,
);
browserToolbar.setItems([btn1,btn2,btn3,btn4],animated: false )
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

(5)使按钮变成任意一个UIView

2,使用UIBarButtonItem还可以创建间隔项
(1)弹性间隔(会尽可能往两端伸长)
2
btngap1 = (barButtonSystemItem: .flexibleSpace,
)
(2)固定间隔(自定义宽度)
3
(barButtonSystemItem: .fixedspace,monospace!important; min-height:auto!important; background:none!important">)
btngap1.width = 50

原文出自: www.hangge.com 转载请保留原文链接 http://www.hangge.com/blog/cache/detail_551.html

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...