当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - How do I hide a custom button that I add to a UITabBar?

问题描述:

 availableButton.frame = CGRectMake(0, screenHeight - tabBarHeight!, CGFloat(screenWidth/5.0) * 2.0, tabBarHeight!)

availableButton.backgroundColor = FlatGreen()

availableButton.setTitle("Turn On", forState: UIControlState.Normal)

availableButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)

availableButton.titleLabel?.font = UIFont(name: "Roboto-Bold", size: 20.0)

self.view.addSubview(availableButton)

This is the button that I added to my TabBarViewController.

When I hide my Tab Bar:

tabBarController?.tabBar.hidden = true

I want this button to be hidden as well.

My button is the width of 2 tab bar items.

网友答案:

let tabBarContext = UnsafeMutablePointer<()>()

self.tabBar.addObserver(self, forKeyPath: "hidden", options: NSKeyValueObservingOptions.New, context:tabBarContext)

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    if context == tabBarContext && keyPath == "hidden"{
        let newChange = change[NSKeyValueChangeNewKey] as! Int
        self.availableButton.hidden  = (newChange == 1) ? true :false   
    }
} 
网友答案:

You have added this button to your view, perhaps this is very bad approach to work.

self.view.addSubview(availableButton)

as tabbar is behind it so it shows. Either ad this to tabbar by using tabBarItem or hide this button manually

availableButton.hidden = YES;

Your problem will be solved.Thank you

分享给朋友:
您可能感兴趣的文章:
随机阅读: