Как создать пользовательский контроллер панели вкладок с пользовательской формой панели вкладок программно - PullRequest
0 голосов
/ 09 марта 2019

Я пытаюсь создать мобильное приложение для iOS полностью в коде. Это вызывает проблемы с моим пользовательским контроллером панели вкладок с пользовательской формой панели вкладок.

Моя пользовательская форма панели вкладок выглядит примерно так:

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
}

Как мне обойти это? При текущей настройке кода моя панель вкладок выглядит как обычная панель вкладок ... не моя пользовательская форма см. Здесь

и я попытался переопределить свойство панели вкладок в моем UITabBarController, чтобы вернуть экземпляр моего класса CustomizedBarBar, но безуспешно. Любая помощь будет высоко ценится!

1 Ответ

0 голосов
/ 09 марта 2019

Пожалуйста, попробуйте запустить этот путь, и дайте мне знать.

class MyTab: UITabBar {

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