gpt4 book ai didi

iphone - 加载 View 后隐藏状态栏留下黑条

转载 作者:可可西里 更新时间:2023-11-01 05:46:09 25 4
gpt4 key购买 nike

在加载 View 后隐藏状态栏时,我遇到了一个奇怪的问题。如果我在 ViewDidLoad 方法中添加以下方法,状态栏将完全从 View 中删除:

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];

但是,如果我在 IBAction 或其他方法中调用此方法,状态栏仍会滑开,但会在后面留下一个与自高度度相同的黑条。

我考虑过将整个 View 向上移动 20 像素,但这真的能解决问题吗?我不想只是重叠一个黑条,以防在未来的操作系统升级中状态栏高度发生变化。

Status bar leaving black bar

最佳答案

硬编码任何数字总是与 future 证明背道而驰。你的担忧是正确的。正确处理状态栏的隐藏有一些技巧。但是所有需要的信息都可用。

例如 UIApplication 单例有一个名为 statusBarFrame 的属性,这正是它听起来的样子,statusBar 的 CGRect 的框架。很酷的是,一旦您调用了 setStatusBarHidden:withAnimation:,即使在动画完成之前,该属性也会为您提供新的框架。所以实际上您只剩下一些基本数学来调整 viewframe

简而言之,您的直觉是正确的;始终计算实时事物。

我使用这样的分类方法取得了很好的成功。 (即使在模拟器(Command - T)中切换通话状态栏):

@implementation UIApplication (nj_SmartStatusBar)
// Always designate your custom methods by prefix.
-(void)nj_setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation{
UIWindow *window = [self.windows objectAtIndex:0];
UIViewController *rootViewController = window.rootViewController;
UIView *view = rootViewController.view;

// slight optimization to avoid unnecassary calls.
BOOL isHiddenNow = self.statusBarHidden;
if (hidden == isHiddenNow) return;

// Hide/Unhide the status bar
[self setStatusBarHidden:hidden withAnimation:animation];

// Get statusBar's frame
CGRect statusBarFrame = self.statusBarFrame;
// Establish a baseline frame.
CGRect newViewFrame = window.bounds;

// Check if statusBar's frame is worth dodging.
if (!CGRectEqualToRect(statusBarFrame, CGRectZero)){
UIInterfaceOrientation currentOrientation = rootViewController.interfaceOrientation;
if (UIInterfaceOrientationIsPortrait(currentOrientation)){
// If portrait we need to shrink height
newViewFrame.size.height -= statusBarFrame.size.height;
if (currentOrientation == UIInterfaceOrientationPortrait){
// If not upside-down move down the origin.
newViewFrame.origin.y += statusBarFrame.size.height;
}
} else { // Is landscape / Slightly trickier.
// For portrait we shink width (for status bar on side of window)
newViewFrame.size.width -= statusBarFrame.size.width;
if (currentOrientation == UIInterfaceOrientationLandscapeLeft){
// If the status bar is on the left side of the window we move the origin over.
newViewFrame.origin.x += statusBarFrame.size.width;
}
}
}
// Animate... Play with duration later...
[UIView animateWithDuration:0.35 animations:^{
view.frame = newViewFrame;
}];
}
@end

关于iphone - 加载 View 后隐藏状态栏留下黑条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12925158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com