Get iPhone Status Bar Height
I need to resize some elements in relation to the height of the iPhone's Status Bar. I know that the status bar is usually 20 points high but this isn't the case when it's in tethering mode. It gets doubled to 40. What is the proper way to determine to determine it's height? I've tried
[[UIApplication sharedApplication] statusBarFrame]
but it gives me 20 x 480 in landscape which is correct but then it gives me 320 x 40 in portrait. Why isn't it giving me the opposite of that (40 x 320)?
The statusBarFrame returns the frame in screen coordinates. I believe the correct way to get what this corresponds to in view coordinates is to do the following:
- (CGRect)statusBarFrameViewRect:(UIView*)view
{
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect statusBarWindowRect = [view.window convertRect:statusBarFrame fromWindow: nil];
CGRect statusBarViewRect = [view convertRect:statusBarWindowRect fromView: nil];
return statusBarViewRect;
}
Now in most cases the window uses the same coordinates as the screen, so [UIWindow convertRect:fromWindow:]
doesn't change anything, but in the case that they might be different this method should do the right thing.
你是否这样做:
CGRect rect;
rect = [[UIApplication sharedApplication] statusBarFrame];
NSLog(@"Statusbar frame: %1.0f, %1.0f, %1.0f, %1.0f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
你可以测试哪个是两个值中较小的那个,这将是真正的高度。
链接地址: http://www.djcxy.com/p/42674.html下一篇: 获取iPhone状态栏高度