搭建项目框架
1.创建一个根视图 (window)
2 根视图上添加根控制器 tabbarController
3.创建4个tableviewController (nav)添加到tabbarController的childController中
-
设置appdelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. setupAppearance() window = UIWindow(frame: UIScreen.mainScreen().bounds) window?.backgroundColor = UIColor.orangeColor() window?.rootViewController = mainViewController() window?.makeKeyAndVisible() return true }
增加子控制器
func addvc(title:String!,imageName:String!,selecterImage:String!)
{
var vc = UITableViewController()
vc.title = title
//修改默认渲染的颜色
UITabBar.appearance().tintColor = UIColor.orangeColor()
//修改图片默认渲染的颜色
vc.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
vc.tabBarItem.selectedImage = UIImage(named: selecterImage)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let nav = UINavigationController(rootViewController: vc)
addChildViewController(nav)
}修改全局渲染
private func setupAppearance(){
UITabBar.appearance().tintColor = UIColor.orangeColor()
UINavigationBar.appearance().tintColor = UIColor.orangeColor()
}-
增加发布按钮
func addChildViewControllers() { addChildViewController(HomeTableViewController(), title: "首页", image: "tabbar_home", selectedImage: "tabbar_home_highlighted") addChildViewController(MessageTableViewController(), title: "消息", image: "tabbar_message_center", selectedImage: "tabbar_message_center_highlighted") //占个坑位 addChildViewController(UIViewController()) addChildViewController(DiscoverTableViewController(), title: "发现", image: "tabbar_discover", selectedImage: "tabbar_discover_highlighted") addChildViewController(ProfileTableViewController(), title: "个人", image: "tabbar_profile", selectedImage: "tabbar_profile_highlighted") }
//创建按钮
private lazy var composeBTN : UIButton = {
let btn = UIButton(type: UIButtonType.Custom)
btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
btn.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
self.tabBar.addSubview(btn)
return btn
}()
func setupComposeBtn(){
let count = childViewControllers.count
//计算每个tabbaritem的宽度
let with = tabBar.bounds.width / CGFloat(count)
//偏移2个单位
let rect = CGRect(x: 0, y: 0, width: with, height: tabBar.bounds.height)
composeBTN.frame = CGRectOffset(rect, with*2, 0)}
-
在viewWillAppear中创建按钮懒加载
override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) setupComposeBtn() }
按钮点击怎么触发的?
运行循环(runloop) 当没有事件时runloop休眠状态(省电),等待用户输入
一旦用户输入,UIApplicationHandleEventQueue
响应者链条:UIApplication发送事件找到keyWindow->UIcontrol(button)->UIApplication(按钮被点击了,告诉APP,并且按钮注册了监听)->target(告诉target被点击了,快点执行方法)->
@objc 能保证运行循环可以调用,走OC的消息机制,调用之前不再进行判断方法是否存在,和private联用,就能做到对方法的保护
@objc private func composeClick(){
print(__FUNCTION__)
}
- 利用基类判断用户登录界面逻辑
新需求:增加用户登录界面
loadview 创建实例化根视图,如果根视图不存在系统再次调用loadview
class BaseTableViewController: UITableViewController {
var islogin = false
override func loadView() {
islogin ? super.loadView() : setupUI()
}
private func setupUI(){
view = UIView()
view.backgroundColor = UIColor.orangeColor()
}
}