TabBarItem系统自带图标样式(System)介绍:
More:三个点的图标,表示更多意思
Favorites:星形图标
Featured:星形图标
Top Tated:星形图标
Recents:时钟图标
Contacts:一个圆形一个人头像的图标,代表联系方式
History:时钟图标
Bookmarks:书本图标
Search:放大镜图标,代表搜索的意思
Downloads:正方形,加一个向下的箭头,代表下载的意思
Most Recent:时钟图标
Most Viewed:三条杠的笔记本纸片图标
下面演示了两种创建标签页的方法。
3,使用代码实现标签页控制器(TabBarController) --- ViewController.swift ---
24
--- MainTabViewController.swift ---
UITabBarController
--- MainViewController.swift ---
16
--- SettingViewController.swift ---
14
1,使用storyboard设计标签页
(1)新建一个Simple View Application,然后删除原来的View Controller并拖入一个Tab Bar Controller,默认就带有两个标签页,每个标签页都在一个View Controller里。
(2)项目新建为Tabbed Application模板也可实现上面的效果。
(3)如果想要添加新的标签页,可以在storyboard里拖入更多的View Controller,每个View Controller放入一个Tab Bar Item。然后建立Tab Bar Controller和新建的View Controller之间的segue关联。即按住Ctrl键,拖动Tab Bar Controller到View Controller,在弹出的上下文菜单中选择View Controller即可。
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
52
|
import
UIKit
class
ViewController
:
UIViewController
,
UITabBarDelegate
{
//添加Tab Bar控件
var
tabBar:
UITabBar
!
//Tab Bar Item的名称数组
tabs = [
"公开课"
"全栈课"
"设置"
]
//Tab Bar上方的容器
contentView:
UIView
!
override
func
viewDidLoad() {
super
.viewDidLoad()
// Do any additional setup after loading the view,typically from a nib.
//在底部创建Tab Bar
tabBar =
(frame:
CGRectMake
(0,147)!important">CGRectGetHeight
(
self
.view.bounds)-64,147)!important">CGRectGetWidth
.view.bounds),44))
items:[
UITabBarItem
] = []
for
tab
in
.tabs {
tabItem =
()
tabItem.title = tab
items.append(tabItem)
}
//设置Tab Bar的标签页
tabBar.setItems(items,animated:
true
)
//本类实现UITabBarDelegate代理,切换标签页时能响应事件
tabBar.delegate =
self
.view.addSubview(tabBar);
//上方的容器
contentView =
(frame:
.view.bounds)-44))
.view.addSubview(contentView)
lbl =
UILabel
(frame:
(100,200,100,20))
lbl.tag = 1
contentView.addSubview(lbl)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
@H_353_403@// Dispose of any resources that can be recreated.
}
tabBar(tabBar:
!,didSelectItem item:
!) {
(contentView.viewWithTag(1)
as
).text = item.title
}
}
|
3,使用代码实现标签页控制器(TabBarController) --- ViewController.swift ---
viewDidLoad() {
.viewDidLoad()
let
button:
UIButton
=
(type:
UIButtonType
.
System
)
button.frame=
button.setTitle(
"开始游戏"
UIControlState
Normal
)
button.addTarget(
Selector
(
"tapped"
),forControlEvents:
UIControlEvents
TouchUpInside
)
.view.addSubview(button);
}
tapped(){
.presentViewController(
MainTabViewController
(),completion:
nil
)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
|
--- MainTabViewController.swift ---
--- MainViewController.swift ---
//改成主视图背景白色背景
.view.backgroundColor =
UIColor
.whiteColor()
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
|
--- SettingViewController.swift ---
(red:109/255,green:218/255,blue:255/255,alpha:1)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
|
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_592.html 原文链接:https://www.f2er.com/swift/324794.html