【发布时间】:2019-07-31 01:26:42
【问题描述】:
我正在尝试完全用代码创建我的 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
}
我该如何解决这个问题?使用当前的代码设置,我的标签栏就像普通的标签栏一样......不是我的自定义形状 see here
我试图在我的 UITabBarController 中覆盖我的标签栏属性以返回我的 CustomizedBarBar 类的实例,但没有成功。任何帮助将不胜感激!
【问题讨论】:
-
我不确定,但尝试添加图层作为蒙版。
-
请向我们展示您在哪里使用带有默认标签栏的
CustomizedBarBar的代码。
标签: ios swift uitabbarcontroller uitabbar