- 前言 在iOS的项目中,我们很多时候都需要去自定义tabbar的导航栏,因为这样能更好的个性化设置界面
- 自定义tabbar
import UIKit
class QLLMainViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setupChildController()
setupComposeButton()
}
func composeStatus() {
print("---center");
}
//撰写按钮
lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//注意 和OC的分类一样 extension 中不能定义属性
extension QLLMainViewController {
func setupComposeButton() {
tabBar.addSubview(composeButton)
//设置按钮的位置
let count = CGFloat(childViewControllers.count)
//将宽度减少 能够让按钮的宽度变大 ,防止穿帮,避免点击到容错点
let width = tabBar.bounds.width / count - 1
composeButton.frame = tabBar.bounds.insetBy(dx: 2 * width, dy: 0)
composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
}
// 设置所有的子控制器
func setupChildController() {
let array = [
["clsName": "QLHomeLViewController", "title": "首页", "imageName": "home"],
["clsName": "QLLMessageViewController", "title": "消息", "imageName": "message_center"],
["clsName": "UIViewController"],
["clsName": "QLLDiscoverViewController", "title": "发现", "imageName": "discover"],
["clsName": "QLLProfileViewController", "title": "我的", "imageName": "profile"],
]
var arratM = [UIViewController]()
for dict in array {
arratM.append(controller(dict: dict))
}
viewControllers = arratM
}
private func controller(dict: [String: String]) -> UIViewController {
// 1、取得字典内容
guard let clsName = dict["clsName"],
let title = dict["title"],
let imageName = dict["imageName"],
let cls = NSClassFromString(Bundle.main.namespece + "." + clsName) as? UIViewController.Type else {
return UIViewController()
}
//2. 创建视图控制器
let vc = cls.init()
vc.title = title
//3. 设置图片
vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
// 4.设置标题字体
vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted);
//设置字体大小 只能设置normal状态下
vc.tabBarItem.setTitleTextAttributes(([NSFontAttributeName: UIFont.systemFont(ofSize: 12)]), for: .normal);
let nav = QLLNavigationController(rootViewController: vc)
return nav
}
}
复制代码
- 自定义导航栏 1.封装导航栏控制器
import UIKit
class QLLNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
//影藏默认的导航栏
navigationBar.isHidden = true
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
print(viewController)
if childViewControllers.count > 0 {
//如果不是栈底控制器才需要影藏 根控制器不需要处理
viewController.hidesBottomBarWhenPushed = true
// 影藏底部的tabbar
if let vc = viewController as? QLLBaseViewController {
var title = "返回"
if childViewControllers.count == 1 {
title = childViewControllers.first?.title ?? "返回"
}
vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, fontSize: 16, target: self, action: #selector(popToParent), isBack: true)
}
}
super.pushViewController(viewController, animated: true)
}
@objc private func popToParent() {
self.popViewController(animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
复制代码
2.在基类控制器中进行自定义导航栏的设置
import UIKit
class QLLBaseViewController: UIViewController {
lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
// 自定义系统的导航条目
lazy var navItem = UINavigationItem()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override var title: String? {
didSet {
navItem.title = title;
}
}
}
extension QLLBaseViewController {
func setupUI() {
view.backgroundColor = UIColor.cz_random()
navigationBar.backgroundColor = UIColor.cz_random()
view.addSubview(navigationBar)
//将item 设置给bar
navigationBar.items = [navItem]
navigationBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray]
}
}
复制代码
3.UIBarButtonItem+Extensions
import UIKit
extension UIBarButtonItem {
/// 创建 UIBarButtonItem
///
/// - Parameters:
/// - title: 标题title
/// - fontSize: 字体大小 默认是16
/// - target: targer
/// - action: action
/// - isBack: 是否有返回按钮
convenience init(title: String, fontSize: CGFloat = 16, target: AnyObject?, action: Selector, isBack: Bool = false) {
let btn: UIButton = UIButton.cz_textButton(title, fontSize: fontSize, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
if isBack {
let imageName: String = "navigationbar_back_withtext"
btn.setImage(UIImage(named: imageName), for: .normal)
btn.setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
btn.sizeToFit()
}
btn.addTarget(target, action: action, for: .touchUpInside)
//实例化
self.init(customView: btn)
}
}
复制代码
4.在非基类控制器中的用法
import UIKit
class QLHomeLViewController: QLLBaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// 显示好友
@objc func showFriends(){
print("显示好友")
let vc = QLLDemoViewController()
vc.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(vc, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension QLHomeLViewController {
override func setupUI() {
super.setupUI()
navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
}
}
复制代码