我试图完全用代码创建我的iOS移动应用程序。这样做会导致我的自定义标签栏控制器出现问题,并带有自定义标签栏形状。
My custom tab bar shape looks something along the lines of this
AppDelegate.swift:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window.rootViewController = CustomTabBarController()
window?.makeKeyAndVisible()
return true
}
在我的自定义TabBarController中:
import UIKit
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setupCenterButton()
}
func setupCenterButton() {
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
var menuButtonFrame = menuButton.frame
menuButtonFrame.origin.y = view.bounds.height - menuButtonFrame.height - 48
menuButtonFrame.origin.x = view.bounds.width/2 - menuButtonFrame.size.width/2
menuButton.frame = menuButtonFrame
menuButton.clipsToBounds = true
menuButton.layer.cornerRadius = menuButtonFrame.height/2
view.addSubview(menuButton)
}
}
extension UITabBar {
open override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: size.width, height: 64)
}
}
而我的自定义标签栏形状
import UIKit
class CustomizedTabBar: UITabBar {
private var shapeLayer: CALayer?
private func addShape() {
let shapeLayer = CAShapeLayer()
shapeLayer.path = createPath()
if let oldShapeLayer = self.shapeLayer {
self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
} else {
self.layer.insertSublayer(shapeLayer, at: 0)
}
self.shapeLayer = shapeLayer
}
func createPath() -> CGPath {
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
//..path moving around..
path.close()
return path.cgPath
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
//..stuff
}
override func draw(_ rect: CGRect) {
self.addShape()
}
func createBezierPath() -> UIBezierPath {
//..creating a path
}
我该如何解决这个问题?使用当前的代码设置,我的tabbar就像一个普通的tabbar ...而不是我的自定义形状see here
我试图在我的UITabBarController中覆盖我的标签栏属性,以返回我的CustomizedBarBar类的实例,没有运气。任何帮助将非常感激!
请尝试以这种方式运行,并告诉我。
class MyTab: UITabBar {
}
class MyTabBarController: UITabBarController {
override var tabBar: UITabBar {
return MyTab()
}
}