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
image.png
加右上角提醒:self.tabBarItem.badgeValue="8"
image.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
image.png