xcode 如何在 setStatusBarHidden:Yes 后设置顶部位置 = 0?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/519689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:28:29  来源:igfitidea点击:

How to set the top position = 0 after setStatusBarHidden:Yes?

iphonexcode

提问by Unreality

I found that after setting the

我发现在设置之后

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO]

at viewDidLoad, then if I want to align an image to the top, I will need to set its position as .y = -20;

at viewDidLoad,然后如果我想将图像与顶部对齐,我需要将其位置设置为 .y = -20;

Is there anyway to make the y coordinate of the top positionto 0? or is it doomed to be -20 after hidding the status bar?

无论如何要使顶部位置的 y 坐标为0?还是隐藏状态栏后就注定是-20?

Thanks for reading.

谢谢阅读。

采纳答案by Justin Gallagher

I had a similar problem at one point and this bit of code fixed it for me:

我曾经遇到过类似的问题,这段代码为我修复了它:

[viewController.view setFrame: [viewController.view bounds]];

回答by Steve Cotner

Justin Gallagher's solution is almost right, but has one major side effect.

Justin Gallagher 的解决方案几乎是正确的,但有一个主要的副作用。

Hiding the status bar and then setting the view's frame to its own bounds will work in the current orientation. But rotation will be ugly. If you are in portrait, for instance, rotating the device to landscape will cause the entire view's frame to be shifted to the right 256 points, leaving a large black space on screen.

隐藏状态栏,然后将视图的框架设置为其自己的边界将在当前方向下工作。但是旋转会很丑。例如,如果您是纵向的,将设备旋转到横向将导致整个视图的框架向右移动 256 点,在屏幕上留下一个大的黑色空间。

bmoeskau's solution (to another side effect) in the comments above avoids this problem:

bmoeskau 在上面评论中的解决方案(另一个副作用)避免了这个问题:

[self.view setFrame: [[UIScreen mainScreen] bounds]];

回答by epatel

I imagine you should add a UIStatusBarHidden item to the Info.plist if you want the status bar to be removed from start.

我想如果您希望从一开始就删除状态栏,您应该向 Info.plist 添加一个 UIStatusBarHidden 项目。

[email protected]

讨论@apple.com

You also could look into setting the Autosizing to resize vertical (and horizontal)

您还可以考虑将 Autosizing 设置为调整垂直(和水平)大小

See under Add A Text Viewherefor example of what to clickon in InterfaceBuilder

有关在 InterfaceBuilder 中单击的内容的示例,请参阅此处的添加文本视图

Quote:

引用:

Click the horizontal and vertical lines in the internal box so that they become solid red lines. The animated preview shows that the text view's internal size will grow and shrink with the window.

单击内部框中的水平线和垂直线,使它们变成红色实线。动画预览显示文本视图的内部大小将随窗口增大和缩小。

回答by Alex Wayne

Check to make sure the size of your root view in your nib is properly set to 480x320. Some of the template project create those at 460x320 to account for the status bar. If you load a view that span full screen and the status bar is hidden, it should just work and you shouldn't need to do anything special at all.

检查以确保笔尖中根视图的大小正确设置为 480x320。一些模板项目创建了 460x320 的模板来解释状态栏。如果你加载一个全屏视图并且状态栏隐藏,它应该可以正常工作,你根本不需要做任何特别的事情。

回答by mikemike396

If you have a scroll view nested inside a view make sure to change that also. This code solved all my issues.

如果您有一个嵌套在视图中的滚动视图,请确保也更改它。这段代码解决了我所有的问题。

[self.view setFrame: [self.view bounds]];
[self.theScroller setFrame: [self.view bounds]];

"theScroller" is the name of my scrollview.

“theScroller”是我的滚动视图的名称。

回答by Pang

The following solution works correctly

以下解决方案正常工作

  • on both iOS 6 and iOS 7,
  • for both hiding and showing the status bar,
  • with both portrait and landscape orientation, and
  • even with the in-call status bar visible
  • 在 iOS 6 和 iOS 7 上,
  • 隐藏和显示状态栏,
  • 纵向和横向,以及
  • 即使通话状态栏可见


In your view controller, add these:

在您的视图控制器中,添加以下内容:

- (BOOL)prefersStatusBarHidden  // For iOS 7.0 or above.
{
    return _isStatusBarHidden;
}

// Call this method to show / hide the status bar and resize the view.
- (void)setStatusBarHidden:(BOOL)isStatusBarHidden
{
    _isStatusBarHidden = isStatusBarHidden;

    // For iOS 7.0 or above...
    if([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
    {
        // Update status bar visibility.
        [self setNeedsStatusBarAppearanceUpdate];   // Tell the view controller that the return value of -prefersStatusBarHidden has changed.
    }
    // Otherwise...
    else
    {
        // Show or hide status bar.
        [[UIApplication sharedApplication] setStatusBarHidden:_isStatusBarHidden withAnimation:UIStatusBarAnimationNone];

        // Resize view.
        self.view.frame = [UIScreen mainScreen].applicationFrame;
    }
}

Here, BOOL _isStatusBarHidden;is a member variable of the view controller class.

这里,BOOL _isStatusBarHidden;是视图控制器类的成员变量。

Notes:

笔记: