我有一个自定义标签栏,其中心有一个较大的标签栏项目。
如果我使用自定义标签栏并以编程方式选择一个标签,则标签栏项目将不会突出显示。仅当我手动按下选项卡时,它才会突出显示:
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()
但是,如果我删除了自定义标签栏,它将起作用:
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()
这是我的自定义标签栏,用于实现更大的中心按钮:
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()
}
}
可能是什么问题?我只希望突出显示以编程方式选择的选项卡。
答案 0 :(得分:1)
最后,我意识到如果在设置自定义选项卡栏之后设置视图控制器,它将可以正常工作。
//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
答案 1 :(得分:0)
如果所选索引不是0,则具有自定义标签栏的代码可以正常工作。 试试这个快速技巧: 代替
let myTabBar = MainTabBar()
tabBarController.setValue(myTabBar, forKey: "tabBar")
tabBarController.selectedIndex = 0
尝试
let myTabBar = MainTabBar()
tabBarController.setValue(myTabBar, forKey: "tabBar")
tabBarController.selectedIndex = 2 // any index other than 0
tabBarController.selectedIndex = 0