简体   繁体   English

使用自定义标签栏以编程方式选择标签时,标签栏项目未突出显示

[英]Tab bar item not highlighted when a tab is selected programmatically with custom tab bar

I have a custom tab bar that has a bigger tab bar item at the center. 我有一个自定义标签栏,中间有一个较大的标签栏项目。

If I use the custom tab bar and programmatically select a tab, tab bar item wont be highlighted. 如果我使用自定义标签栏并以编程方式选择一个标签,则标签栏项目将不会突出显示。 It will only be highlighted when I press the tab manually: 仅当我手动按下选项卡时,它才会突出显示:

    let controller1 = ....
    controller1.tabBarItem = UITabBarItem(..)

    let controller2 = ...
    controller2.tabBarItem = UITabBarItem(..)

    let controller3 = ...
    controller3.tabBarItem = UITabBarItem(..)

    let controller4 = ...
    controller4.tabBarItem = UITabBarItem(..)

    let tabBarController = UITabBarController()
    tabBarController.viewControllers = [controller1, controller2, controller3, controller4];

    let myTabBar = MainTabBar()
    tabBarController.setValue(myTabBar, forKey: "tabBar")
    tabBarController.selectedIndex = 0 //tab bar item wont highlight!

    self.window?.rootViewController = tabBarController;
    self.window?.makeKeyAndVisible()

However, if I remove the custom tab bar, it works: 但是,如果我删除了自定义标签栏,它将起作用:

    let controller1 = ....
    controller1.tabBarItem = UITabBarItem(..)

    let controller2 = ...
    controller2.tabBarItem = UITabBarItem(..)

    let controller3 = ...
    controller3.tabBarItem = UITabBarItem(..)

    let controller4 = ...
    controller4.tabBarItem = UITabBarItem(..)

    let tabBarController = UITabBarController()
    tabBarController.viewControllers = [controller1, controller2, controller3, controller4];

    //let myTabBar = MainTabBar()
    //tabBarController.setValue(myTabBar, forKey: "tabBar")
    tabBarController.selectedIndex = 0 //tab bar item WILL highlight!

    self.window?.rootViewController = tabBarController;
    self.window?.makeKeyAndVisible()

This is my custom tab bar, to implement a bigger center button: 这是我的自定义标签栏,用于实现更大的中心按钮:

import UIKit

class MainTabBar: UITabBar {

private var middleButton : UIButton?
var middleButtonPressed: ()->() = {}

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    guard let middleButton = middleButton else {
        return super.hitTest(point, with: event)
    }
    if self.isHidden {
        return super.hitTest(point, with: event)
    }

    let from = point
    let to = middleButton.center

    return sqrt((from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y)) <= 39 ? middleButton : super.hitTest(point, with: event)
}

func showMiddleButton() {
    if middleButton == nil {
        let size : CGFloat = 48.0
        middleButton = UIButton(frame: CGRect(x: 0.5 * (self.bounds.size.width - size), y: 10.0 - size, width: size, height: size))
        if let middleButton = middleButton {
            middleButton.backgroundColor = ThemeManager.currentTheme().mainColor
            middleButton.layer.shadowOffset = CGSize(width: 0, height: 5)
            middleButton.layer.shadowOpacity = 1
            if middleButton.backgroundColor == UIColor.white {
                middleButton.layer.shadowColor = UIColor(red:0.14, green:0.06, blue:0.21, alpha:0.86).cgColor
                middleButton.layer.shadowRadius = 28
            }else{
                middleButton.layer.shadowColor = UIColor(red:0.14, green:0.06, blue:0.21, alpha:0.29).cgColor
                middleButton.layer.shadowRadius = 20
            }
            middleButton.layer.cornerRadius = 0.5 * size
            middleButton.setTitle("+", for: .normal)
            middleButton.setTitleColor(ThemeManager.currentTheme().tabBarMiddleButtonTitleColor, for: .normal)
            middleButton.titleLabel?.font = UIFont.systemFont(ofSize: 36.0, weight: .light)
            middleButton.titleEdgeInsets.top = -5
            middleButton.addTarget(self, action: #selector(MainTabBar.handlePressed), for: UIControlEvents.touchUpInside)
            self.addSubview(middleButton)
        }
    }
    if let middleButton = middleButton {
        middleButton.isHidden = false
    }
}

func hideMiddleButton(){
    if let middleButton = middleButton {
        middleButton.isHidden = true
    }
}

@objc func handlePressed() {
    middleButtonPressed()
}

} }

What could be wrong? 有什么事吗 I only want the programmatically selected tab to be highlighted. 我只希望突出显示以编程方式选择的选项卡。

At the end I realise it will work if I set view controller AFTER setting the custom tab bar. 最后,我意识到如果在设置自定义选项卡栏之后设置视图控制器,它将可以正常工作。

    //this will work
    let tabBarController = UITabBarController()
    let myTabBar = MainTabBar()
    tabBarController.setValue(myTabBar, forKey: "tabBar")
    tabBarController.viewControllers = [controller1, controller2, controller3, controller4];
    tabBarController.selectedIndex = 0

    //this will not work
    let tabBarController = UITabBarController()
    tabBarController.viewControllers = [controller1, controller2, controller3, controller4];
    let myTabBar = MainTabBar()
    tabBarController.setValue(myTabBar, forKey: "tabBar")
    tabBarController.selectedIndex = 0

Your code having custom tab bar works fine if selected index is other than 0. Try this quick hack: Instead of 如果选择的索引不是0,则具有自定义标签栏的代码可以正常工作。请尝试以下快速技巧:

let myTabBar = MainTabBar()
tabBarController.setValue(myTabBar, forKey: "tabBar")
tabBarController.selectedIndex = 0

try 尝试

let myTabBar = MainTabBar()
tabBarController.setValue(myTabBar, forKey: "tabBar")
tabBarController.selectedIndex = 2 // any index other than 0
tabBarController.selectedIndex = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM