导航推快速时隐藏自定义中间按钮4

时间:2018-12-24 09:01:23

标签: ios swift uitabbarcontroller swift4 uitabbar

我的标签栏中有3个栏项目,中间的项目设置为禁用,并用中间的自定义按钮将其覆盖

Swift 3 - How do I create a prominent button on a tab bar (e.g. camera button)

当导航推到新屏幕时,我想隐藏底部的栏。我尝试了此操作,但由于从未调用过viewDidDisappear而无法正常工作

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidAppear(animated)
    hidesBottomBarWhenPushed = true
    centerButton.isHidden = true
}

你们很高兴告诉我如何实现这一目标?

2 个答案:

答案 0 :(得分:0)

好像您那里有错字。使用super.viewDidDisappear()

override func viewDidDisappear(_ animated: Bool) {
    //super.viewDidAppear(animated)
    super.viewDidDisappear(animated)
    hidesBottomBarWhenPushed = true
    centerButton.isHidden = true
}

答案 1 :(得分:0)

您必须为推送的视图控制器设置该属性。请务必注意,在viewDidLoad中进行设置为时已晚。您应该在初始化视图控制器实例时进行设置:

class PushedVC: UIViewController {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        sharedInit()
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        sharedInit()
    }

    private func sharedInit() {
        hidesBottomBarWhenPushed = true
    }

}

当然,您还可以在使用情节提要时检查特定视图控制器的复选标记Hide Bottom Bar on Push

要确保通过这种方法隐藏中心按钮/将其与标签栏一起显示,您必须将其添加为UITabBar子类中标签栏的子视图。然后,您覆盖hitTest以使其完全可点击(当然,只有在按钮超过标签栏的框架时才需要):

class TabBar: UITabBar {

    let centerButton = ...

    // ...

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if centerButton.frame.contains(point) {
            return centerButton
        }

        return super.hitTest(point, with: event)
    }

    // ...

}