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 2.3。(Xcode 8.0)解决方案。我已经创建了UITableView的子类。

class MYCustomTableView: UITableView
{   
    override func drawRect(rect: CGRect) {
        super.drawRect(rect)
        contentInset    = UIEdgeInsetsZero
    }
}

内容插入应该始终为零(默认情况下)。我手动设置为零。你也可以添加一个check方法来检查,如果它不是你想要的,只要得到正确的rect就可以了。这个变化只会在tableview被绘制时反映出来(这并不经常发生)。

不要忘记在你的IB中更新TableView(在TableViewController中或只是在你的ViewController中的TableView中)。

其他回答

对于那些像我一样不愿意在UIViewController中嵌入UITableViewController的人来说,试试这个:

一个自定义的UITableViewController子类可以附加一个掩码视图到tableView的父视图。在viewDidAppear上添加蒙版,并在viewWillDisappear上删除蒙版。

private var statusBarMaskView: UIView!

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if let superview = self.tableView.superview {
        self.statusBarMaskView = UIView(frame: CGRect.zero)
        superview.insertSubview(self.statusBarMaskView, aboveSubview: self.tableView)
        self.statusBarMaskView.backgroundColor = self.tableView.backgroundColor

        // using a nice constraint layout library to set the frame
        self.statusBarMaskView.pinTop(to: superview)
        self.statusBarMaskView.pinLeft(to: superview)
        self.statusBarMaskView.pinRight(to: superview)
        self.statusBarMaskView.addHeightConstraint(with: 22.0)
        ////
    }
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    self.statusBarMaskView.removeFromSuperview()
    self.statusBarMaskView = nil
}

再加上上面的答案:

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

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


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

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

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

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

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

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

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

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

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

查看所有解决方案: 我的项目只是使用xib,所以,解决方案与故事板不为我工作。 自我。edgesForExtendedLayout = UIRectEdgeNone; 只适用于控制器,如果导航栏是可见的。 但如果你的视图只有状态栏,那是不行的。 所以我把两个条件结合起来。

- (void) viewDidLayoutSubviews {
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 7.0f) {
    CGRect bounds = self.view.bounds;
    if(self.navigationController == nil || self.navigationController.isNavigationBarHidden == YES){
        bounds.origin.y -= 20.0;
        [self.view setBounds:bounds];
    }
    else{
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
}

帮助它起作用。

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

EDIT

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

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