开发环境:Xcode 7 Beta 4
1.1: 部署初步架构
1.2 设置根控制器(AppDelegate.swift)
<span style="font-family:Microsoft YaHei;"> func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. window = UIWindow (frame: UIScreen.mainScreen().bounds ) window?.backgroundColor = UIColor.whiteColor() window?.rootViewController = MainUITabBarController() window?.makeKeyAndVisible() return true } </span>
1.3 自定义TarbBar
<span style="font-family:Microsoft YaHei;">import UIKit class MainUITabBarController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. addChildViewControllers() setComposeButton() } ///设置Button private func setComposeButton() { //计算Button 高度 let w = tabBar.bounds.width / CGFloat (viewControllers!.count) // Button 已有宽和高的Rect let rect = CGRect(x: 0,y: 0,width: w,height: tabBar.bounds.height) //使用偏移 设置Button位置 composeButton.frame = CGRectOffset(rect,2 * w,0) } /// 添加所有子控制器 private func addChildViewControllers() { //Start添加所有子控制器-------------------------- addChildViewController(HoMetableViewController(),title:"首页",imageName:"tabbar_home" ) addChildViewController(MessageTableViewController(),title:"消息",imageName:"tabbar_message_center" ) addChildViewController(UIViewController()) addChildViewController(DiscoverTableViewController(),title:"发现",imageName:"tabbar_discover" ) addChildViewController(profileTableViewController(),title:"我",imageName:"tabbar_profile" ) //end----------------------------------------- } ///懒加载创建Button lazy private var composeButton: UIButton = { let button = UIButton () //Start设置Button素材--------------------------------- button.setImage(UIImage(named: "tabbar_compose_icon_add"),forState: UIControlState.Normal) button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"),forState: UIControlState.Highlighted) button.setBackgroundImage(UIImage(named: "tabbar_compose_button"),forState: UIControlState.Normal ) button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"),forState: UIControlState.Highlighted ) //end----------------------------------------------- //此处不提示得手敲 self.tabBar.addSubview(button) button.addTarget(self,action: "ClickComposeButton",forControlEvents: UIControlEvents.TouchUpInside) return button }() /// 添加控制器方法 /// /// - parameter vc : 视图控制器 /// - parameter title : 标题 /// - parameter imageName: 图像名称 private func addChildViewController(vc:UIViewController,title: String,imageName: String) { tabBar.tintColor = UIColor.orangeColor() vc.title = title vc.tabBarItem.image = UIImage(named: imageName) let nav = UINavigationController(rootViewController: vc) addChildViewController(nav) } </span>
1.4.1自定义一个继承UITableViewController的控制器
1.4.2 修改所有控制器的继承关系,继承BaseTableViewController
1.4.3 设置BaseTableViewController的View
<span style="font-family:Microsoft YaHei;"> ///设置自定义登录View private func setVistorView() { //创建空View,测试使用 view = UIView () //设置颜色 view.backgroundColor = UIColor.orangeColor() }</span>
1.4.4 重写BaseTableViewController 的loadView()方法
<span style="font-family:Microsoft YaHei;">//定义一个用户是否登录的开关 var userLogin = true</span>
<span style="font-family:Microsoft YaHei;"> override func loadView() { //判断用户登录是否成功切换View userLogin ? super.loadView() : setVistorView() }</span>原文链接:https://www.f2er.com/swift/326456.html