首页 技术 正文
技术 2022年11月20日
0 收藏 966 点赞 4,227 浏览 4838 个字

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

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

 Swift – 标签条(UITabBar)标签页控制器(UITabBarController)用法  Swift – 标签条(UITabBar)标签页控制器(UITabBarController)用法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)

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 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)

Swift – 标签条(UITabBar)标签页控制器(UITabBarController)用法 Swift – 标签条(UITabBar)标签页控制器(UITabBarController)用法 Swift – 标签条(UITabBar)标签页控制器(UITabBarController)用法

— ViewController.swift —

123456789101112131415161718192021222324 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 —

123456789101112131415161718192021222324 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 —

12345678910111213141516 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 —

1234567891011121314 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.    }}

源码下载:Swift – 标签条(UITabBar)标签页控制器(UITabBarController)用法TabBarControllerTest.zip

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781