The first screen of my application is a UITableViewController without a navigation bar, which means that the content flows under the status bar so there's a lot of text collisions. I've adjusted both the properties for Under top bars and Adjust scroll view insets which do actually stop it from scrolling under, but at the cost of keeping the top of the table view under. I've attempted to set the UITableView frame to offset by 20 pixels, but it doesn't appear to take effect and as I currently need the app to be compatible with iOS 6 I can't jump to iOS 7 Storyboards to force autolayout to use the top height guide. Has anyone found a solution that works for both versions?

我尝试过的事情:设置edgesForExtendedLayout,在Storyboard中更改顶部栏下的设置和调整滚动视图,迫使框架到一个新的区域。

一图胜千言万语:


当前回答

我发现最简单的方法,尤其是当你在标签栏中添加表格视图时,首先添加一个视图,然后在该视图中添加表格视图。这将为您提供您正在寻找的最高边际指导。

其他回答

我使用一个UISplitViewController与一个导航控制器和一个tableviewcontroller。在尝试了许多解决方案后,这对我来说很有效:

float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 7.0f) {

    // Move the view down 20 pixels
    CGRect bounds = self.view.bounds;
    bounds.origin.y -= 20.0;
    [self.navigationController.view setBounds:bounds];

    // Create a solid color background for the status bar
    CGRect statusFrame = CGRectMake(0.0, -20.0, bounds.size.width, 20);
    UIView* statusBar = [[UIView alloc] initWithFrame:statusFrame];
    statusBar.backgroundColor = [UIColor whiteColor];
    [statusBar setAlpha:1.0f];
    [statusBar setOpaque:YES];
    [self.navigationController.view addSubview:statusBar];
}

它类似于Hot Licks的解决方案,但将子视图应用到navigationController。

对于任何有兴趣复制此操作的人,只需遵循以下步骤:

创建一个新的iOS项目 打开主故事板,删除默认的/初始的UIViewController 从对象库中拖出一个新的UITableViewController 将它设置为初始视图控制器 向表提供一些测试数据

如果你按照上面的步骤,当你运行应用程序时,你会发现什么都没有,包括调整Xcode的复选框“扩展边下{上,下,不透明}条”,以阻止第一行出现在状态栏下,你也不能通过编程来解决这个问题。

例:在上述情况下,以下内容将不起作用:

// These do not work
self.edgesForExtendedLayout=UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets=NO;

这个问题非常令人沮丧,我相信这是苹果的一个bug,特别是因为它出现在他们自己的对象库中预先连接的UITableViewController中。

我不同意那些试图用任何形式的“神奇数字”来解决这个问题的人。“使用20px的delta”。这种紧密耦合的编程绝对不是苹果想让我们在这里做的。

对于这个问题,我发现了两个解决方案:

Preserving the UITableViewController's scene: If you would like to keep the UITableViewController in the storyboard, without manually placing it into another view, you can embed the UITableViewController in a UINavigationController (Editor > Embed In > Navigation Controller) and uncheck "Shows Navigation Bar" in the inspector. This solves the issue with no extra tweaking needed, and it also preserves your UITableViewController's scene in the storyboard. Using AutoLayout and embedding the UITableView into another view (I believe this is how Apple wants us to do this): Create an empty UIViewController and drag your UITableView in it. Then, Ctrl-drag from your UITableView towards the status bar. As the mouse gets to the bottom of the status bar, you will see an Autolayout bubble that says "Top Layout Guide". Release the mouse and choose "Vertical Spacing". That will tell the layout system to place it right below the status bar.

我已经在一个空应用程序上测试了这两种方法,它们都可以工作。您可能需要做一些额外的调整,以使它们适用于您的项目。

我这样做的视网膜/非视网膜显示

BOOL isRetina = FALSE;

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    if ([[UIScreen mainScreen] scale] == 2.0) {
        isRetina = TRUE;
    } else {
        isRetina = FALSE;
    }
}

if (isRetina) {
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars=NO;
    self.automaticallyAdjustsScrollViewInsets=NO;
}

我在ios 11中遇到了这个问题,但ios 8 - 10.3.3的布局是正确的。对于我的案例,我设置垂直空间约束为Superview。Top Margin代替Superview。顶部适用于ios 8 - 11。

对于Xcode 7,取消导航栏的“半透明”复选标记对我来说很有效。