Which is the best approach for creating custom tab bar controller?

后端 未结 4 1721
Happy的楠姐
Happy的楠姐 2021-02-03 11:25

I am working on application that is more likely a tabBarController application. But I can not use the tabBarController because I need a custom ta

4条回答
  •  时光取名叫无心
    2021-02-03 12:24

    For ios 9.0+ use UIStackView in Horizontal direction and create a xib and named it CustomTabBarView

    CustomTabBarView.Xib

    Create Another View CustomTabBarItem.

    CustomTabBarItem.xib

    create CustomTabBarItem.swift file for implement functionality of TabItem

    class CustomTabItem: UIView {
    
    //MARK:- IBOutlets
    @IBOutlet weak var itemImage: UIImageView!
    @IBOutlet weak var itemTitle: SelectableLabel!
    @IBOutlet weak var topButton: UIButton!
    
    //MARK:- Variables
    var isSelected: Bool = false {
        didSet {
            self.itemImage.image = CommonFunctions.getTabItemImage(isSelected: isSelected, tag: topButton.tag)
            itemTitle.isSelected = isSelected
            if isSelected {
                self.backgroundColor = UIColor.appDarkBlueColor
            }
            else {
                self.backgroundColor = UIColor.appWhiteColor
            }
        }
    }
    
    //MARK:- IBActions
    @IBAction func onClickButton(_ sender: Any) {
    
    }
    

    }

    In TabBarViewController Implement This Code

    func createCustomTabBarView(tabItemCount: Int){
    
        customTabBarView = Bundle.main.loadNibNamed("CustomTabBarView", owner: nil, options: nil)?.last as! UIView
        customTabBarView.frame = CGRect(x: 0, y: self.view.frame.height - self.tabBar.frame.height, width: self.tabBar.frame.width, height: self.tabBar.frame.size.height)
        self.view.addSubview(customTabBarView)
    
        var stackView = UIStackView()
        for subView in customTabBarView.subviews{
            if subView is UIStackView {
                stackView = subView as! UIStackView
            }
        }
    
        for i in 0..

提交回复
热议问题