swift tabbar 自定义+号 底部34导航栏适配

2020/10/07 11:23
阅读数 2.7K

接上篇文章

iphoneX底部34的高度 也适配了。。

//
//  Deivices.swift
//  News
//
//  Created by liuan on 2020/10/6.
//

import Foundation
import UIKit
extension UIDevice {
    //刘海屏, 获取底部高度
    public func getBottomHeight() -> CGFloat {
        
        var tabHeight = UIApplication.shared.statusBarFrame.size.height > 20 ? 34.0 : 0.0
       
        
   
        return CGFloat(tabHeight)
        
    }
    
}

主要就是自己定义tabBar类

//
//  MyTabBar.swift
//  News
//
//  Created by liuan on 2020/10/6.
//

import UIKit

class MyTabBar: UITabBar {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(publishButton)
    }
    //private 绝对私有,除了在当前类种可以访问之外,其他任何类或者这个类的扩展都不能访问
    //fileprivate 文件私有。。可以在当前类文件种访问。其他文件种不能访问,
    //open 在任何类文件种都能访问
    //internal 默认 可以不写
    
    
    private lazy var publishButton:UIButton =  {
        let publishButton = UIButton(type: .custom)
        
        publishButton.setBackgroundImage(UIImage(named: "feed_publish_44x44_"), for: .normal)
        publishButton.setBackgroundImage(UIImage(named: "feed_publish_press_44x44_"), for: .selected)
        publishButton.sizeToFit()
        return publishButton
        
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder)")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        //当前tabbai的宽度和高度
        
        let width = frame.width
        var height = frame.height - UIDevice.init().getBottomHeight()
     
      
        publishButton.center = CGPoint(x:width*0.5,y:height*0.5 - 7)
        //设置其他按钮的frame
        let buttonW:CGFloat = width * 0.2
        let buttonH:CGFloat = height
        let buttonY:CGFloat = 0
        
        var index = 0
        for button in subviews{
            
            if !button.isKind(of: NSClassFromString("UITabBarButton")!){
                continue
            }
            
            let buttonX = buttonW * CGFloat(index > 1 ?(index+1):index)
            button.frame = CGRect(x: buttonX, y: buttonY, width: buttonW, height: buttonH)
            
            index+=1
        }
        
    }
    
}

然后设置给主的UITabBarController

  setValue(MyTabBar(), forKey: "tabBar")

就是上篇文章的

//
//  MyTabBarController.swift
//  News
//
//  Created by 杨蒙 on 2017/9/6.
//  Copyright © 2017年 hrscy. All rights reserved.
//

import UIKit

class MyTabBarController: UITabBarController {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print(tabBar.subviews)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let tabbar = UITabBar.appearance()
        tabbar.tintColor = UIColor(red: 245 / 255.0, green: 90 / 255.0, blue: 93 / 255.0, alpha: 1.0)
        
        // 添加子控制器
        addChildViewControllers()
    }
    
    /// 添加子控制器
    func addChildViewControllers() {
        setChildViewController(HomeViewController(), title: "首页", imageName: "home_tabbar_32x32_", selectedImageName: "home_tabbar_press_32x32_")
        setChildViewController(VideoViewController(), title: "视频", imageName: "video_tabbar_32x32_", selectedImageName: "video_tabbar_press_32x32_")
        setChildViewController(HuoshanViewController(), title: "小视频", imageName: "huoshan_tabbar_32x32_", selectedImageName: "huoshan_tabbar_press_32x32_")
        setChildViewController(MineViewController(), title: "我的", imageName: "mine_tabbar_32x32_", selectedImageName: "mine_tabbar_press_32x32_")
        // tabBar 是 readonly 属性,不能直接修改,利用 KVC 把 readonly 属性的权限改过来
        setValue(MyTabBar(), forKey: "tabBar")
    }
    
    /// 初始化子控制器
    func setChildViewController(_ childController: UIViewController, title: String, imageName: String, selectedImageName: String) {
        // 设置 tabbar 文字和图片
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: selectedImageName)
        childController.title = title
        // 添加导航控制器为 TabBarController 的子控制器
        let navVc = MyNavigationController(rootViewController: childController)

        addChild(navVc)
    }
    
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部