.每个文件夹粘贴.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:"发现", 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)
//设置选中状态
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 监听按钮点击事件(对外抛出点击事件,声明一个闭包)
importUIKit
classHMTabBar:UITabBar{
//
varcomposeClouse: (() -> ())?
overrideinit(frame:CGRect) {
super.init(frame: frame)
addSubview(composeBtn)
//监听按钮的点击事件
self.composeBtn.addTarget(self, action:#selector(composeBtnDidClick), for: .touchUpInside)
}
//不希望外部访问按钮的点击事件私有的
//一旦添加了private私有方法对OC的运行循环不可见
//@objc是一个OC的标示swift会对继承自NSObject的对象的方法
//添加@objc是告诉系统这个方法或者属性具备OC特性消息机制
@objcprivatefunccomposeBtnDidClick() {
//对外抛出点击事件(闭包)不能在View里直接处理点击事件
print(#function)
composeClouse?()
}
//默认实现报错
//重写了构造方法系统需要程序员手动实现该方法
//一旦该视图通过xib加载程序就会崩溃你写好了这个视图,别人就会使用该视图
requiredinit?(coder aDecoder:NSCoder) {
// fatalError("init(coder:) has not been implemented")
//该控件就支持手写代码xib来创建
super.init(coder: aDecoder)
self.addSubview(composeBtn)
}
//修改内部视图的位置重写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 实现点击按钮的操作(闭包)
hmtabBar.composeClouse= { [weakself]in
print("撰写按钮被点击了")
// print(self)
}
5.Main 里面 新建一个类 HMBaseNavController 自定义返回按钮
importUIKit
classHMBaseNavController:UINavigationController{
overridefuncviewDidLoad() {
super.viewDidLoad()
}
//重写push方法
//说明uinavigationcontroller(rootviewcontroller:)内部执行了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)
}
}