在 UITabBarController 中覆盖 UITabBar

Posted

技术标签:

【中文标题】在 UITabBarController 中覆盖 UITabBar【英文标题】:Override UITabBar in UITabBarController 【发布时间】:2020-01-25 16:30:10 【问题描述】:

当我尝试在我的 UITabController 中自定义 Tabbar 时遇到问题。当我使用下面的解决方案用我的自定义标签栏覆盖默认标签栏时,标签栏显示没有任何项目,并且不会触发任何点击。 这是我的自定义标签栏的代码:

import UIKit
@IBDesignable
class MyTabBar: UITabBar 
    private var shapeLayer: CALayer?
override var frame: CGRect 
    get 
        return super.frame
    
    set 
        var tmp = newValue
        if let superview = superview, tmp.maxY !=
            superview.frame.height 
            tmp.origin.y = superview.frame.height - tmp.height
        

        super.frame = tmp
    

    private func addShape() 
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = createPath()
        shapeLayer.strokeColor = UIColor.lightGray.cgColor
        shapeLayer.fillColor = UIColor.white.cgColor
        shapeLayer.lineWidth = 1.0
        //The below 4 lines are for shadow above the bar. you can skip them if you do not want a shadow
        shapeLayer.shadowOffset = CGSize(width: 0, height: 0)
        shapeLayer.shadowRadius = 10
        shapeLayer.shadowColor = UIColor.gray.cgColor
        shapeLayer.shadowOpacity = 0.3

        if let oldShapeLayer = self.shapeLayer 
            self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
         else 
            self.layer.insertSublayer(shapeLayer, at: 0)
        
        self.shapeLayer = shapeLayer
    
    override func draw(_ rect: CGRect) 
        self.addShape()
    
    func createPath() -> CGPath 
        let height: CGFloat = 30.0
        let path = UIBezierPath()
        let centerWidth = self.frame.width / 2
        path.move(to: CGPoint(x: 0, y: 0)) // start top left
        path.addLine(to: CGPoint(x: (centerWidth - height * 2), y: 0)) // the beginning of the trough

        path.addCurve(to: CGPoint(x: centerWidth, y: -height),
        controlPoint1: CGPoint(x: centerWidth - height * 2, y: 0), controlPoint2: CGPoint(x: centerWidth - height, y: -height))

        path.addCurve(to: CGPoint(x: centerWidth + height * 2, y: 0),
        controlPoint1: CGPoint(x: centerWidth, y: -height), controlPoint2: CGPoint(x: (centerWidth + height), y: -height))

        path.addLine(to: CGPoint(x: self.frame.width, y: 0))
        path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
        path.addLine(to: CGPoint(x: 0, y: self.frame.height))
        path.close()

        return path.cgPath
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? 
           guard !clipsToBounds && !isHidden && alpha > 0 else  return nil 
           for member in subviews.reversed() 
               let subPoint = member.convert(point, from: self)
               guard let result = member.hitTest(subPoint, with: event) else  continue 
               return result
           
           return nil
       

这是我的 TabbarController 的代码:

@objc class MyTabBarController: UITabBarController 
override var tabBar: UITabBar 
    return MyTabBar()

override func viewDidLoad() 
    super.viewDidLoad()
    setupMiddleButton()

func setupMiddleButton() 
    let addLostBtn = UIButton(frame: CGRect(x: (self.view.bounds.width / 2)-25, y: -20, width: 50, height: 50))
    //STYLE THE BUTTON YOUR OWN WAY
    addLostBtn.setBackgroundImage(UIImage(named: "addLostIcon"), for: .normal)
    //add to the tabbar and add click event
    self.tabBar.addSubview(addLostBtn)
    addLostBtn.addTarget(self, action: #selector(self.menuButtonAction), for: .touchUpInside)
    self.view.layoutIfNeeded()


// Menu Button Touch Action
@objc func menuButtonAction(sender: UIButton) 
    self.selectedIndex = 2   //to select the middle tab. use "1" if you have only 3 tabs.

我认为 Apple 不允许在以编程方式创建 UITabbarController 时覆盖 tabBar。最后我在堆栈中检查了这个问题Link,但没有找到任何解决方案。 请帮忙

【问题讨论】:

【参考方案1】:

我认为使用上面的代码无法覆盖 UITabBar。所以我使用自定义 UIViewController 和基于此链接的自定义 UIView Tabbar 解决了这个问题

Link

【讨论】:

以上是关于在 UITabBarController 中覆盖 UITabBar的主要内容,如果未能解决你的问题,请参考以下文章

将 UITabBar 放置在 UITabBarController 中的屏幕顶部

为啥 UITabBarController 返回“..应该支持至少一个方向”消息?

如何识别从UITabBarController中的更多选项卡或单独选项卡单击视图控制器?

双击UITabBarController时防止自动popToRootViewController

TabBar 覆盖导航堆栈中的视图控制器内容

Swift 4 - 如何覆盖标签栏将打开视图控制器