.每個文件夾粘貼.gitkeep? ? ? 添加.gitigonre
2.Classes ? ? Model
Other
Tools
ViewModel
View ? ? ?: ? ? ? ?Discover ?: ? Controller
View
Home ? ?: ? ? Controller
View
Main ?: ? ? ? Controller
View
Message ?: ? Controller
View
Profile ? : ? ? Controller
View
3.在Main里面的Controller ? 新建 ? HMTabBarController
importUIKit
classHMTabBarController:UITabBarController{
overridefuncviewDidLoad() {
super.viewDidLoad()
addChildViewControllers()
}
//添加子視圖控制器
privatefuncaddChildViewControllers(){
addChildViewController(vc:HMHomeTableViewController(), title:"首頁", imageName:"tabbar_home")
addChildViewController(vc:HMMessageTableViewController(), title:"消息", imageName:"tabbar_message_center")
addChildViewController(vc:HMDiscoverTableViewController(), title:"發(fā)現(xiàn)", imageName:"tabbar_discover")
addChildViewController(vc:HMProfileTableViewController(), title:"我", imageName:"tabbar_profile")
}
//mark:添加子視圖控制器
privatefuncaddChildViewController(vc:UIViewController, title:String, imageName:String){
//設置title
vc.tabBarItem.image=UIImage(named: imageName)
//設置選中狀態(tài)
vc.tabBarItem.selectedImage=UIImage(named: imageName+"_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
//設置文字顏色
vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
//設置文字大小
vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize:10)], for: .normal)
//設置文字的位置
vc.tabBarItem.titlePositionAdjustment=UIOffset(horizontal:0, vertical:-3)
vc.tabBarItem.badgeValue="10"
vc.tabBarItem.badgeColor= #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
//??????? vc.tabBarItem.title = title
vc.tabBarItem.title= title
vc.navigationItem.title= title
//2.將tableVC包裝到導航控制器
letnav =HMBaseNavController(rootViewController: vc)
//3.將導航控制器添加到UITabBarController對象的子視圖控制器
self.addChildViewController(nav)
}
}
4.Mian里面的View ? 自定義撰寫按鈕 ? ?新建 HMTabBar ? ? 懶加載按鈕 重寫layoutsubviews ?監(jiān)聽按鈕點擊事件(對外拋出點擊事件,聲明一個閉包)
importUIKit
classHMTabBar:UITabBar{
//
varcomposeClouse: (() -> ())?
overrideinit(frame:CGRect) {
super.init(frame: frame)
addSubview(composeBtn)
//監(jiān)聽按鈕的點擊事件
self.composeBtn.addTarget(self, action:#selector(composeBtnDidClick), for: .touchUpInside)
}
//不希望外部訪問按鈕的點擊事件私有的
//一旦添加了private私有方法對OC的運行循環(huán)不可見
//@objc是一個OC的標示swift會對繼承自NSObject的對象的方法
//添加@objc是告訴系統(tǒng)這個方法或者屬性具備OC特性消息機制
@objcprivatefunccomposeBtnDidClick() {
//對外拋出點擊事件(閉包)不能在View里直接處理點擊事件
print(#function)
composeClouse?()
}
//默認實現(xiàn)報錯
//重寫了構(gòu)造方法系統(tǒng)需要程序員手動實現(xiàn)該方法
//一旦該視圖通過xib加載程序就會崩潰你寫好了這個視圖,別人就會使用該視圖
requiredinit?(coder aDecoder:NSCoder) {
//??????? fatalError("init(coder:) has not been implemented")
//該控件就支持手寫代碼xib來創(chuàng)建
super.init(coder: aDecoder)
self.addSubview(composeBtn)
}
//修改內(nèi)部視圖的位置重寫layoutsubviews
overridefunclayoutSubviews() {
//super
super.layoutSubviews()
//獲取uitabbarbutton
letw =UIScreen.main.bounds.width/5
leth =self.bounds.height
varindex =0
forsubviewinsubviews{
ifsubview.isKind(of:NSClassFromString("UITabBarButton")!) {
//修改frame
subview.frame=CGRect(x:CGFloat(index) * w, y:0, width: w, height: h)
index += (index ==1?2:1)
}
}
//設置frame
composeBtn.bounds.size=CGSize(width: w, height: h)
composeBtn.center=CGPoint(x:self.center.x, y: h *0.5)
}
//懶加載按鈕
lazyvarcomposeBtn:UIButton= {
letbtn =UIButton()
//設置圖片
btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
//設置背景圖片
btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
returnbtn
}()
}
5.Main里面的HMTabBarController ?實現(xiàn)點擊按鈕的操作(閉包)
hmtabBar.composeClouse= { [weakself]in
print("撰寫按鈕被點擊了")
//??????????? print(self)
}
5.Main 里面 新建一個類 ?HMBaseNavController ? ?自定義返回按鈕
importUIKit
classHMBaseNavController:UINavigationController{
overridefuncviewDidLoad() {
super.viewDidLoad()
}
//重寫push方法
//說明uinavigationcontroller(rootviewcontroller:)內(nèi)部執(zhí)行了push
overridefuncpushViewController(_viewController:UIViewController, animated:Bool) {
letcount =childViewControllers.count
ifcount >0{
viewController.navigationItem.leftBarButtonItem=UIBarButtonItem(title:"返回", imageName:"navigationbar_back_withtext", target:self, action:#selector(back))
//隱藏tabbaritem
viewController.hidesBottomBarWhenPushed=true
}
super.pushViewController(viewController, animated: animated)
}
@objcprivatefuncback() {
popViewController(animated:true)
}
}