johnbakers
johnbakers

Reputation: 24750

Putting a view in front of the tab bar

I am trying to conceive of a way in which I can float a view over top the tab bar. This would essentially cover it. I'm not sure how to go about this. If the tab bar is itself a subview of another controller, then this would seem easy, but from what I understand you cannot really do this without "abusing" the view controller methodology (i.e. addSubview: tabbar.view or something similar).

Basically, there is limited space on an iphone screen and the tab bar is the navigation but at times I want to use that space to show information briefly. If the tab bar is my root controller, I would think this is not possible.

I've read other posts on here that suggest using the setHidesBottomBarWhenPushed but I'm not using a nav controller, and I also want to dynamically control when the bar gets hidden or covered by another view, so I can move that new view out of the way when desired and show the tab bar again.

Any other suggestions to point me in the right direction would be appreciated.

Upvotes: 3

Views: 5051

Answers (3)

Aleksey Shevchenko
Aleksey Shevchenko

Reputation: 1241

Something like that:

if let indeedTabBarController = self.tabBarController {

    let buttonHeight: CGFloat = 49 // height of tab bar
    let buttonWidth = UIScreen.main.bounds.width / 3 // in case if you have 3 tabs
    let frame = CGRect(x: 0, y: UIScreen.main.bounds.height - buttonHeight, width: buttonWidth, height: buttonHeight)
    let button = UIButton(frame: frame)
    button.addTarget(self, action: #selector(self.someAction), for: .touchUpInside)

    indeedTabBarController.view.addSubview(button)
}

Upvotes: 0

Praveen S
Praveen S

Reputation: 10395

I would suggest the following.

UIView
   |
    --UIView (this will hold the tab bar controller)
        |
         -- Tabbar controller
   |
    --Full Screen View

So basically the tab bar is enclosed in a UIView. This is added to the main UIView. When you want to display full screen you can add that view to the main UI view. Thus there would be no need to hide/unhide the tab bar.

Upvotes: 1

Ed Marty
Ed Marty

Reputation: 39690

You could use the root window. If you just get the main window from [[UIApplication sharedApplication] keyWindow], you can add views directly to that. That will allow you to float anything over anywhere on screen again. You'll still have a little snafu to deal with vis-a-vis rotation, but if you aren't doing automatic rotation, then it's as simple as not worrying about it.

Another option would be to actually create a new UIWindow and put stuff in it wherever you want. This is how UIAlertViews actually work. That way, it doesn't interfere with your view hierarchy at all. This method has one issue, in that only one window will dispatch events, including touches, to their subviews. So using this basically allows the user to still touch the bar underneath. The first option may be better, but I'm just throwing this out there in case anybody finds it useful.

Upvotes: 6

Related Questions