【问题标题】:How to create a custom tab bar controller with custom tab bar shape programattically如何以编程方式创建具有自定义标签栏形状的自定义标签栏控制器
【发布时间】: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


【解决方案1】:

如果您使用情节提要,则使用自定义标签栏是微不足道的。

  1. 创建您的自定义 UITabBar 子类。
  2. 将标签栏控制器拖到情节提要上。
  3. 在情节提要中选择标签栏对象,点击“身份检查器”,将标签栏的类更改为您自定义的标签栏类。

【讨论】:

    【解决方案2】:

    请尝试以这种方式运行,然后告诉我。

    class MyTab: UITabBar {
    
    }
    class MyTabBarController: UITabBarController {
        override var tabBar: UITabBar {
            return MyTab()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 2015-11-01
      • 2019-10-08
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多