开发者

calculating height of UITabBar

I'm writing an app that uses UITabBar for parts of the navigation. I'm also using UIScrollView for presenting more information than what the screen can typically handle. Because of this, I'm needing to set the scroll view to take into acc开发者_运维知识库ount the height of the UITabBar so that all of the information is displayed.

Is there a way to calculate the height of the UITabBar?


If the view controller has an ancestor that is a tab bar controller, you can retrieve the height from that tab bar.

CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;


It is 320 x 49.

If you want to test, open Interface Builder, add a UITabBar, go into the ruler, you will see it

UITabBar is inherited from UIVIew so you can use the frame.size.height to get the height


In Swift:

let height = self.tabBarController?.tabBar.frame.height ?? 49.0

Relying on the actual height of the tab-bar, and using the magic number as a fallback.


Swift 3+

let tabBarHeight = tabBarController?.tabBar.frame.size.height
print(tabBarHeight ?? "not defined")

It should print 49.0 (Type CGFloat)


I was looking to do something similar with centering a label in the VISIBLE portion of a ViewController's view. This ViewController belonged to a UITabBarController.

Here's the code I used to center my label:

UILabel *roomLabel = [[UILabel alloc] init];
CGRect frame = [[self view] bounds];
float tabBarHeight = [[[super tabBarController] tabBar] frame].size.height;
frame.size.height -= tabBarHeight;
[roomLabel setFrame:frame];
[[self view] addSubview:roomLabel];
[roomLabel release];

Notice that I used [[self view] bounds] not [[self view] frame] because the latter includes the 20 pixel top bar as the Y offset (which throws off the vertical centering).

Hope this helps someone!

By the way: I'm using iOS 4.3 and XCode 4 and the "hard-code" value for the TabBar's height is still 49 for me!


I know this isn't ideal, but I really didn't want to have a magic number constant anywhere. What I did was create a throwaway UITabBarController, and get the height from there.

I did this also because [UITabBar initWithFrame:] works as desired, but doing a [bar setFrame:] doesn't. I needed the frame to be correct at creation.

UITabBarController *dtbc = [[[UITabBarController alloc] init] autorelease];

CGRect tabRect = [[[self navigationController] view] frame];
tabRect.origin.y = tabRect.size.height - [[dtbc tabBar] frame].size.height;
tabRect.size.height = [[dtbc tabBar] frame].size.height;

tabBar_ = [[UITabBar alloc] initWithFrame:tabRect];

What I like about this is that it will correctly place the tab bar at the bottom of the parent regardless of the parents size.


This should work in most cases on any instance of UIViewController:

bottomLayoutGuide.length


In swift 4 and 5. self.tabBarController?.getHeight()

extension UITabBarController{

    func getHeight()->CGFloat{
        return self.tabBar.frame.size.height
    }

    func getWidth()->CGFloat{
         return self.tabBar.frame.size.width
    }
}


Others can also try to get the height using the intrinsicContentSize property of the tab bar.

let tabBarHeight = self.tabBarController.tabBar.intrinsicContentSize.height


This is how I got it to work in swift 4.1

let tabBarHeight = CGFloat((self.tabBarController?.tabBar.frame.size.height)!)


Swift 5

    if let tabBarController = tabBarController {
        let tabBarSafeAreaHeight = tabBarController.tabBar.frame.size.height - 
            tabBarController.tabBar.safeAreaInsets.bottom
    }

This calculates the height of the UITabBar taking into account the safeAreaInsets (UIEdgeInsets)

At the time of writing this equals 49 on iPhone portrait


let screenHeight = UIApplication.shared.statusBarFrame.height +
            self.navigationController!.navigationBar.frame.height + (tabBarController?.tabBar.frame.size.height)!

This works perfectly, based it of a few ppls answer here


SWIFT 5 UPDATE :

AS this thread is old, I am posting here the update from another thread: https://stackoverflow.com/a/25550871/14559220. To sum things up,

in portrait and regular landscape, the height is still 49 points. In compact landscape, the height is now 32 points.

On iPhone X, the height is 83 points in portrait and 53 points in landscape.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜