微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift - 工具条UIToolbar的用法

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
class ViewController : UIViewController {
override func viewDidLoad() {
super .viewDidLoad()
// Do any additional setup after loading the view,typically from a nib.
// 创建一个浏览器工具条,并设置它的大小和位置
var browserToolbar = UIToolbar (frame: CGRectMake (0,320,44))
// 将工具条添加到当前应用的界面中
self .view.addSubview(browserToolbar)
//创建后退
btnback = UIBarButtonItem (image: UIImage (named: "back.png" ),
style: UIBarButtonItemStyle . Bordered ,target: 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: Selector ( "backClicked:" ));
//第一个分隔按钮
btngap1 = (barButtonSystemItem: UIBarButtonSystemItem FlexibleSpace 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,
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 = "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,
Plain "forwardClicked:" ));
// 第二个分隔按钮,创建一个可伸缩的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,
);
}
backClicked(sender: )
{
//后退
}
forwardClicked(sender: )
{
//前进
}
}

(2)普通图片按钮,但提供另一个备选图像供横屏使用
2
"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,image: "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,
));
(3)普通文本按钮
(title:"确定""confrim:"(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
import UIKit
class ViewController : UIViewController {
var pickerView: UIPickerView !
override func viewDidLoad() {
super .viewDidLoad()
// 创建一个浏览器工具条,并设置它的大小和位置
browserToolbar = UIToolbar (frame: CGRectMake 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,44))
// 将工具条添加到当前应用的界面中
self .view.addSubview(browserToolbar)
btn1 = UIBarButtonItem (barButtonSystemItem: UIBarButtonSystemItem . Compose 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,
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 = Add 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,
);
btn3 = FlexibleSpace 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 = Reply 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,
);
browserToolbar.setItems([btn1,btn2,btn3,btn4],animated: false )
}
}

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

2,使用UIBarButtonItem还可以创建间隔项
(1)弹性间隔(会尽可能往两端伸长)
3
btngap1 = 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: );
(2)固定间隔(自定义宽度)
4
Fixedspace );
btngap1.width = 50

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐