I have a problem when I try to customize the Tabbar in my UITabController. When I Used the solution below to override the default Tabbar with my custom Tabbar, the tabbar is showing without any items and no click can be fire. this is the code of my custom 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
}
}
this the code of my 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.
}
}
I think that Apple doesn't allow to override the tabBar when create UITabbarController programmatically. Finally I checked in stack about this issue Link without find any solution. Any help please