我制作了Uitabar控制器,并在故事板上制作了5个tabbar按钮。
但我无法控制选项卡栏项中的图像大小和颜色。
我想自定义按钮的颜色和形状使用PNG文件,而不是使用默认颜色。
但是下面的代码除了tabbar tintcolor之外,效果不太好。

class MainView: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()

    tabBar.barTintColor = UIColor.white

}

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    switch item.tag{
    case 0:
        print(item.tag)
        UITabBar.setItems(item[0]) = UITabBarItem(title: "Magzine", image: UIImage(named: "exhibiter.png"), selectedImage: UIImage(named: "exhibiterselected.png"))
    case 1:
        print("")
    case 2:
        print("")
    case 3:
        print("")
    case 4:
        print("")
    default:
        break
    }
}

override func viewWillAppear(_ animated: Bool) {
    UIApplication.shared.isStatusBarHidden = false
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

最佳答案

你可以这样用

class TabBarViewController: UITabBarController {

    required init(coder aDecoder: NSCoder) {

    super.init(coder: aDecoder)!

override func viewDidLoad() {
    super.viewDidLoad()

    UITabBar.appearance().tintColor = UIColor(red: 254/255.0, green: 105/255.0, blue: 51/255.0, alpha: 100.0)

    for i in 0 ..< self.tabBar.items!.count {

        switch i {

        case 0:

            tabBar.items?[0].title = "Home"
            let firstTab = self.tabBar.items![i] as UITabBarItem
            learnTab.image = UIImage(named: "Homedeselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            firstTab.selectedImage = UIImage(named: "Homedeselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            firstTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
            firstTab.titlePositionAdjustment = UIOffsetMake(0, -4)


        case 1:

            tabBar.items?[1].title = "Request"
            let secondTab = self.tabBar.items![i] as UITabBarItem
            secondTab.image = UIImage(named: "RequestDeselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            secondTab.selectedImage = UIImage(named: "Requestselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            secondTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
            secondTab.titlePositionAdjustment = UIOffsetMake(0, -4)

        case 2:

            tabBar.items?[2].title = "Renew"
            let thirdTab = self.tabBar.items![i] as UITabBarItem
             thirdTab.image = UIImage(named: "RenewDeSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            thirdTab.selectedImage = UIImage(named: "RenewSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            thirdTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
            thirdTab.titlePositionAdjustment = UIOffsetMake(0, -4)

        case 3:

            tabBar.items?[3].title = "ReSell"
            let forthTab = self.tabBar.items![i] as UITabBarItem
            forthTab.image = UIImage(named: "ReSellDeSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            forthTab.selectedImage = UIImage(named: "ReSellSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            forthTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
            forthTab.titlePositionAdjustment = UIOffsetMake(0, -4)

        case 4:

            tabBar.items?[4].title = "ReCycle"
            let fifthTab = self.tabBar.items![i] as UITabBarItem
            fifthTab.image = UIImage(named: "ReCycleDeSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            fifthTab.selectedImage = UIImage(named: "ReCycleSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            fifthTab.imageInsets = UIEdgeInsetsMake(-2, 0, 2, 0)
            fifthTab.titlePositionAdjustment = UIOffsetMake(0, -5)

        default:
            break
        }
    }
}

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {




    }

}

关于ios - 如何自定义添加在 Storyboard 上的标签栏项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42036944/

10-09 16:34