1、创建三个视图控制器 FirstViewController、SecondViewController、ThirdViewController
2、在FirstViewController.swift中viewDidLoad添加
self.title = “Item #1”
self.tabBarItem.image = UIImage(named:“Tab1”)
let label = UILabel(frame:CGRect(x:40, y:150,width:240, height:44))
label.text = “第一页”
label.textAlignment = NSTextAlignment.center
label.font = UIFont(name:“Arial”, size:36)
self.view.addSubview(label)
3 AppDelegate.swift的didFinishLaunchingWithOptions方法:
let firstViewController = FirstViewController()
let secondViewController = SecondViewController()
let thirdViewController = ThirdViewController()
let tabViewController = UITabBarController()
tabViewController.viewControllers =[firstViewController, secondViewController,thirdViewController]
self.window?.rootViewController = tabViewController
![9967595-091415da6b475a01.png](https://upload-images.jianshu.io/upload_images/9967595-091415da6b475a01.png)
加右上角提醒:self.tabBarItem.badgeValue="8"
![9967595-e28b75a20301d200.png](https://upload-images.jianshu.io/upload_images/9967595-e28b75a20301d200.png)
手动改变tab
1 let button = UIButton(frame:CGRect(x:40, y:220,
width:240, height:44))
2 button.setTitle(“进入第二页”, for:UIControlState())
3 button.backgroundColor = UIColor.black
4 button.addTarget(self, action:
selector(FirstViewController.enterPage2),
for:.touchUpInside)
5 self.view.addSubview(button)
1 func enterPage2()
2 {
3 self.tabBarController?.selectedIndex = 1
4 }
可以改变导航栏的位置:
let screenWidth = UIScreen.main.bounds.width
tabViewController.tabBar.frame = CGRect(x:0, y:0,width:screenWidth, height:44)
隐藏顶部状态栏
info.plist中Status bar is initially hidden=YES
View controller-based status bar appearance=NO
![9967595-0166038dd61ee064.png](https://upload-images.jianshu.io/upload_images/9967595-0166038dd61ee064.png)