App底部的tab标签页可以方便的把功能模块划分清楚,只需点击相应的标签页就可以展示完全独立的视图页面,同时各标签页间的视图也可以进行数据交换。

 
TabBarItem系统自带图标样式(System)介绍:
Custom:自定义方式,配合Selected Image来自定义图标
More:三个点的图标,表示更多意思
Favorites:星形图标
Featured:星形图标
Top Tated:星形图标
Recents:时钟图标
Contacts:一个圆形一个人头像的图标,代表联系方式
History:时钟图标
Bookmarks:书本图标
Search:放大镜图标,代表搜索的意思
Downloads:正方形,加一个向下的箭头,代表下载的意思
Most Recent:时钟图标
Most Viewed:三条杠的笔记本纸片图标
 
下面演示了两种创建标签页的方法。

   
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即可。
2,使用代码实现标签条(TabBar)
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的名称数组
    var tabs = ["公开课","全栈课","设置"]
    //Tab Bar上方的容器
    var contentView:UIView!
                             
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
         
        //在底部创建Tab Bar
        tabBar = UITabBar(frame:
            CGRectMake(0,CGRectGetHeight(self.view.bounds)-64,CGRectGetWidth(self.view.bounds),44))
        var items:[UITabBarItem] = []
        for tab in self.tabs {
            var tabItem = UITabBarItem()
            tabItem.title = tab
            items.append(tabItem)
        }
        //设置Tab Bar的标签页
        tabBar.setItems(items, animated: true)
        //本类实现UITabBarDelegate代理,切换标签页时能响应事件
        tabBar.delegate = self
        //代码添加到界面上来
        self.view.addSubview(tabBar);
         
        //上方的容器
        contentView = UIView(frame:
            CGRectMake(0,0,CGRectGetWidth(self.view.bounds),CGRectGetHeight(self.view.bounds)-44))
        self.view.addSubview(contentView)
        var lbl = UILabel(frame:CGRectMake(100,200,100,20))
        //定义tag,在用户切换tab时能查询到label控件
        lbl.tag = 1
        contentView.addSubview(lbl)
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
     
    // UITabBarDelegate协议的方法,在用户选择不同的标签页时调用
    func tabBar(tabBar: UITabBar!, didSelectItem item: UITabBarItem!) {
        //通过tag查询到上方容器的label,并设置为当前选择的标签页的名称
        (contentView.viewWithTag(1) as UILabel).text = item.title
    }
}

3,使用代码实现标签页控制器(TabBarController)

  

--- ViewController.swift ---

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import UIKit
 
class ViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
         
        let button:UIButton = UIButton(type: UIButtonType.System)
        button.frame=CGRectMake(100, 150, 100, 30)
        button.setTitle("开始游戏", forState:UIControlState.Normal)
        button.addTarget(self,action:Selector("tapped"),forControlEvents:UIControlEvents.TouchUpInside)
        self.view.addSubview(button);
    }
     
    func tapped(){
        self.presentViewController(MainTabViewController(), animated:true, completion:nil)
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

--- MainTabViewController.swift ---

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import UIKit
 
class MainTabViewController:UITabBarController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        //一共包含了两个视图
        let viewMain = MainViewController()
        viewMain.title = "2048"
        let viewSetting = SettingViewController()
        viewSetting.title = "设置"
         
        //分别声明两个视图控制器
        var main = UINavigationController(rootViewController:viewMain)
        main.tabBarItem.image = UIImage(named:"first")
        var setting = UINavigationController(rootViewController:viewSetting)
        setting.tabBarItem.image = UIImage(named:"second")
        self.viewControllers = [main,setting]
         
        //默认选中的是游戏主界面视图
        self.selectedIndex = 0
    }
}

--- MainViewController.swift ---

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import UIKit
 
class MainViewController: UIViewController {
 
    override func viewDidLoad() {
      
        super.viewDidLoad()
        //改成主视图背景白色背景
        self.view.backgroundColor = UIColor.whiteColor()
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

--- SettingViewController.swift ---

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import UIKit
 
class SettingViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(red:109/255, green:218/255, blue:255/255, alpha:1)
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

源码下载:TabBarControllerTest.zip

Swift - 标签条(UITabBar)标签页控制器(UITabBarController)用法的更多相关文章

  1. 标签视图控制器UITabBarController

    标签视图控制器 UITabBarController FirstViewController*first = [[FirstViewController alloc] init]; //创建一个UIT ...

  2. UITabBarController — 标签视图控制器

    UITabBarController - 标签视图控制器 UITabBarController 分为三层结构: (1).tab bar (2.)Custom Content (3.). Tab bar ...

  3. UITabBarController ---- 标签视图控制器

    直接上代码: // // AppDelegate.m // // #import "AppDelegate.h" #import "RootViewController. ...

  4. Bootstrap标签页(Tab)插件

    标签页(Tab)在Bootstrap导航元素一章中简介过,通过结合一些data属性,您可以轻松地创建一些标签页界面.通过这个插件您可以把内容放置在标签页或胶囊式标签页甚至是下拉菜单标签页中. 用法 您 ...

  5. swift:简单使用翻页控制器UIPageViewController

    一.小叙 UIPageViewController是一个实现图书阅读的控制器,使用它可以设置书脊位置.单双页.过渡效果等,它是通过代理的方式来实现翻页,也即上一页.下一页.最终这个UIPageView ...

  6. 用标签页TitleSwitch切换不通的控制器

    用标签页TitleSwitch切换不通的控制器 教程效果: 项目开发中效果: 各种源码: TitleSwitch.h 与 TitleSwitch.m (这个是修改过的升级版本) // // Title ...

  7. Bootstrap 标签页和工具提示插件

    一.标签页 标签页也就是通常所说的选项卡功能. //基本用法 <ul class="nav nav-tabs"> <li class="active&q ...

  8. JavaScript插件——标签页

    JavaScript插件——标签页 前言 阅读之前您也可以到Bootstrap3.0入门学习系列导航中进行查看http://www.cnblogs.com/aehyok/p/3404867.html ...

  9. js基础--浏览器标签页隐藏或显示状态 visibility详解

    欢迎访问我的个人博客:http://www.xiaolongwu.cn 前言 在工作中我们可能会遇到这样的需求,当浏览器切换到别的标签页或着最小化时,我们需要暂停页面上正在播放的视频或者音乐,这个需求 ...

随机推荐

  1. opencv鼠标绘制直线 C++版

    因为需要在图片上标记直线,所以从网上找了相应的参考资料.但大多都是c风格的,于是自己就写了一个c++风格的. opencv2.4.11,win8.1,vs2013 #include <cv.h& ...

  2. vs2016 创建 vsto excel 文件项目的一个问题

    新工作需要些一个基于Excel开发一个工具,vs的 vsto 功能很好用,封装了基于开发office 开的一些工具.但是在实际使用时,创建项目总是报错,提示打开excel文件失败.项目是需要创建一个e ...

  3. WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]

    原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...

  4. AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3參数含义

    arg0:是指父Vjew arg1就是你点击的那个Item的View arg2是position,position是你适配器里面的position arg3是id,通常是第几个项.id是哪个项View ...

  5. 谁是Docker的开发人员

    由CHRIS DAWSON发表在thenewstack/DATA RESEARCH qianhen123/CHB译 我们分析了Docker的容器库并提出两个问题: 1.Docker的贡献者们感兴趣的其 ...

  6. 利用T4模版生成EF实体

    直接上代码,只需要修改EF实体的地址就可以了. <#@ template language="C#" debug="false" hostspecific ...

  7. WCF技术剖析之十六:数据契约的等效性和版本控制

    原文:WCF技术剖析之十六:数据契约的等效性和版本控制 数据契约是对用于交换的数据结构的描述,是数据序列化和反序列化的依据.在一个WCF应用中,客户端和服务端必须通过等效的数据契约方能进行有效的数据交 ...

  8. debian网易163更新服务器 源

    sudo vi /etc/apt/sources.list 加入如下内容即可: deb http://mirrors.163.com/debian/ jessie main non-free cont ...

  9. tcp接收xml数据解析

    避免tcp接收xml数据时加上xml数据长度,根据xml数据特点来解析recv到的xml数据 int nPos1 = 0; int nPos2 = 0; int nTempPos = 0; int n ...

  10. Jenkins快速搭建持续集成

    基于Jenkins快速搭建持续集成环境 Jenkins+tortoisesvn+MSBuild做到持续集成 附Jenkins的使用:http://www.infoq.com/cn/articles/M ...