标签栏控制器问题

时间:2018-11-25 07:33:40

标签: ios swift uiviewcontroller uitabbarcontroller tabbar

我正在使用xib的选项卡栏控制器,但是当我包含一个vc时,该vc末尾仍有空间。 Here is video of my problem

Here is my viewcontroller xib and code

请帮助我

这是我的标签栏控制器代码

import UIKit

class TabbarControllerVC: UITabBarController, UITabBarControllerDelegate{

    let homeVC = HomeVC(nibName: "HomeVC", bundle: nil)
    let listVC = HomeVC(nibName: "HomeVC", bundle: nil)
    let notificationVC = HomeVC(nibName: "HomeVC", bundle: nil)
    let settingVC = MyProfileVC(nibName: "MyProfileVC", bundle: nil)

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self

        tabBar.tintColor = ColorConstants.ThemeColor

        var tabbarControllers = [UIViewController]()
        tabbarControllers.append(homeVC)
        tabbarControllers.append(listVC)
        tabbarControllers.append(notificationVC)
        tabbarControllers.append(settingVC)

        self.setViewControllers(tabbarControllers, animated: true)

        homeVC.tabBarItem = UITabBarItem(title: "", image: UIImage(named : "Home"), selectedImage: UIImage(named : "Home"))
        listVC.tabBarItem = UITabBarItem(title: "", image: UIImage(named : "List"), selectedImage: UIImage(named : "List"))
        notificationVC.tabBarItem = UITabBarItem(title: "", image: UIImage(named : "Notification"), selectedImage: UIImage(named : "Notification"))
        settingVC.tabBarItem = UITabBarItem(title: "", image: UIImage(named : "Setting"), selectedImage: UIImage(named : "Setting"))

        UITabBar.appearance().tintColor = ColorConstants.ThemeColor

        homeVC.tabBarItem.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0)
        listVC.tabBarItem.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0)
        notificationVC.tabBarItem.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0)
        settingVC.tabBarItem.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0)

        if #available(iOS 10.0, *) {
            UITabBar.appearance().unselectedItemTintColor = ColorConstants.BlackColor
        } else {
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: ColorConstants.BlackColor], for: .normal)

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func setupTabBarSeparators() {
        let itemWidth = floor(self.tabBar.frame.size.width / CGFloat(self.tabBar.items!.count))

        // this is the separator width.  0.5px matches the line at the top of the tab bar
        let separatorWidth: CGFloat = 0.5

        // iterate through the items in the Tab Bar, except the last one
        for i in 0...(self.tabBar.items!.count - 1) {
            // make a new separator at the end of each tab bar item
            let separator = UIView(frame: CGRect(x: itemWidth * CGFloat(i + 1) - CGFloat(separatorWidth / 2) , y: 15, width: CGFloat(separatorWidth), height: self.tabBar.frame.size.height - 30))

            // set the color to light gray (default line color for tab bar)
            separator.backgroundColor = UIColor.lightGray

            self.tabBar.addSubview(separator)
        }
    }

    override func viewWillAppear(_ animated : Bool){
        super.viewWillAppear(true)
        setupTabBarSeparators()
    }
}

0 个答案:

没有答案