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中更改顶部栏下的设置和调整滚动视图,迫使框架到一个新的区域。

一图胜千言万语:


当前回答

这是用Swift写的 调整@lipka的回答:

tableView.contentInset = UIEdgeInsetsMake(20.0, 0.0, 0.0, 0.0)

其他回答

如果你还需要支持iOS 6,你必须有条件地把它下移。也就是说,在iOS 7中,你只需要将它向下移动20个点(通过帧操作或使用自动布局),而在iOS 6中,你可以不动它。我不相信你们能在IB中做这个,所以你们必须在代码中做。

EDIT

你可以在IB中,通过使用iOS6/iOS7增量。在iOS 7中设置你的位置,然后在iOS 6中设置Y为-20点。更多信息请参见这个SO问题。

我认为使用UITableViewController的方法可能和你们之前做的有点不同。这对我来说很有效,但你可能不会喜欢。我所做的是有一个视图控制器和一个指向UItableViewController的容器视图。这样我就可以使用TopLayoutGuide提供给我的故事板。只要把约束添加到容器视图,你就可以兼顾iOS7和iOS6了。

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

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;
}

再加上上面的答案:

在第二种方法最初似乎不工作后,我做了一些额外的修补,找到了解决方案。

TLDR;顶部答案的第二个解决方案几乎是可行的,但对于某些版本的xCode, ctrl+拖动到“顶部布局指南”并选择垂直间距不起作用。然而,首先调整表格视图的大小,然后选择“顶部空间到顶部布局指南”即可


将一个空白的ViewController拖到故事板上。 将UITableView对象拖到View中。(不是UITableViewController)。使用蓝色布局指南将其定位在正中心。

拖一个UITableViewCell到TableView中。这将是你的原型重用单元格,所以不要忘记在Attributes选项卡下设置它的重用标识符,否则你会崩溃。

创建UIViewController的自定义子类,并添加<UITableViewDataSource, UITableViewDelegate>协议。不要忘记在标识检查器中将你的故事板的ViewController设置为这个类。 在你的实现文件中为你的TableView创建一个outlet,命名为TableView

右键单击TableView并将数据源和委托拖到你的ViewController中。

现在是不剪切到状态栏的部分。

抓取你的表视图的顶部边缘,并将其移动到一个虚线蓝色的自动布局指南,是靠近顶部

现在,您可以从表格视图控制拖动到顶部,并选择顶部空间到顶部布局指南

它会给你一个关于TableView的模糊布局的错误,只需添加缺少的约束,你就完成了。

现在你可以像往常一样设置你的表视图,它不会剪辑状态栏!

chappjc的答案在使用xib时工作得很好。

当以编程方式创建TableViewControllers时,我发现最干净的解决方案是将UITableViewController实例包装在另一个UIViewController中,并相应地设置约束。

下面就是:

UIViewController *containerLeftViewController = [[UIViewController alloc] init];
UITableViewController *tableViewController = [[UITableViewController alloc] init];

containerLeftViewController.view.backgroundColor = [UIColor redColor];

hostsAndMoreTableViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
[containerLeftViewController.view addSubview:tableViewController.view];

[containerLeftViewController addChildViewController:tableViewController];
[tableViewController didMoveToParentViewController:containerLeftViewController];

NSDictionary * viewsDict = @{ @"tableView": tableViewController.view ,
                              @"topGuide": containerLeftViewController.topLayoutGuide,
                              @"bottomGuide": containerLeftViewController.bottomLayoutGuide,
                              };
[containerLeftViewController.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|"
                                                                                         options:0
                                                                                         metrics:nil
                                                                                           views:viewsDict]];
[containerLeftViewController.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topGuide][tableView][bottomGuide]"
                                                                                         options:0
                                                                                         metrics:nil
                                                                                           views:viewsDict]];

干杯,本