(本文代码已升级至Swift3)
1,UIBarButtonItem是工具条按钮,有如下5种init初始化方法:
(2)普通图片按钮,但提供另一个备选图像供横屏使用
1
(3)普通文本按钮
2
(4)使用预置按钮:如撤销,重做,编辑等(总共有25个样式)
(5)使按钮变成任意一个UIView
2,使用UIBarButtonItem还可以创建间隔项
(1)弹性间隔(会尽可能往两端伸长)
2
(2)固定间隔(自定义宽度)
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()
// 创建一个浏览器工具条,并设置它的大小和位置
// 将工具条添加到当前应用的界面中
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,
)
//添加按钮
}
backClick(_ sender:
) {
//后退
print
(
"后退按钮点击"
)
}
forwardClick(_ sender:
) {
//前进
"前进按钮点击"
)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}
|
"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(_:)))
|
(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(_:)))
|
Custom:默认样式
Flexible Space:自动伸缩的空白占位符
Fixed Space:固定宽度的占位元素
Add:“+”加号样式
Edit:Edit文字样式
Done:Done文字样式
Cancel:Cancel文字样式
Save:Save文字样式
Undo:Undo文字样式
Redo:Redo文字样式
Compose:背景矩形插支笔的图标样式
Reply:返回箭头的图标样式
Organize:文件夹的图标样式
Trash:垃圾桶的图标样式
Bookmarks:书的图标样式
Search:放大镜的图标样式
Refresh:顺时针圆形箭头的图标样式
Stop:一个×的图标样式
Camera:一个照相机的图标样式
Play:向右三角形代表播放的图标样式
Pause:两条竖线代表播放的图标样式
Rewind:向左两个三角代表回退的图标样式
Fast Forward:向右两个三角代表快进的图标样式
示例如下:
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,
);
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}
|
(5)使按钮变成任意一个UIView
2,使用UIBarButtonItem还可以创建间隔项
(1)弹性间隔(会尽可能往两端伸长)
btngap1 =
(barButtonSystemItem: .flexibleSpace,
)
|
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_551.html