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中更改顶部栏下的设置和调整滚动视图,迫使框架到一个新的区域。
一图胜千言万语:
这将修复一个UITableViewController(没有任何神奇的数字)。我唯一不能让它修复的是如果你在打电话,在这种情况下,tableView的顶部被下推太多。如果有人知道如何解决这个问题,请告诉我们。
class MyTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureTableViewTop()
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition({ (context) -> Void in
}, completion: { (context) -> Void in
self.configureTableViewTop()
})
}
func configureTableViewTop() {
tableView.contentInset.top = UIApplication.sharedApplication().statusBarFrame.height
}
}
这将修复一个UITableViewController(没有任何神奇的数字)。我唯一不能让它修复的是如果你在打电话,在这种情况下,tableView的顶部被下推太多。如果有人知道如何解决这个问题,请告诉我们。
class MyTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureTableViewTop()
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition({ (context) -> Void in
}, completion: { (context) -> Void in
self.configureTableViewTop()
})
}
func configureTableViewTop() {
tableView.contentInset.top = UIApplication.sharedApplication().statusBarFrame.height
}
}
对于任何有兴趣复制此操作的人,只需遵循以下步骤:
创建一个新的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.
我已经在一个空应用程序上测试了这两种方法,它们都可以工作。您可能需要做一些额外的调整,以使它们适用于您的项目。
再加上上面的答案:
在第二种方法最初似乎不工作后,我做了一些额外的修补,找到了解决方案。
TLDR;顶部答案的第二个解决方案几乎是可行的,但对于某些版本的xCode, ctrl+拖动到“顶部布局指南”并选择垂直间距不起作用。然而,首先调整表格视图的大小,然后选择“顶部空间到顶部布局指南”即可
将一个空白的ViewController拖到故事板上。
将UITableView对象拖到View中。(不是UITableViewController)。使用蓝色布局指南将其定位在正中心。
拖一个UITableViewCell到TableView中。这将是你的原型重用单元格,所以不要忘记在Attributes选项卡下设置它的重用标识符,否则你会崩溃。
创建UIViewController的自定义子类,并添加<UITableViewDataSource, UITableViewDelegate>协议。不要忘记在标识检查器中将你的故事板的ViewController设置为这个类。
在你的实现文件中为你的TableView创建一个outlet,命名为TableView
右键单击TableView并将数据源和委托拖到你的ViewController中。
现在是不剪切到状态栏的部分。
抓取你的表视图的顶部边缘,并将其移动到一个虚线蓝色的自动布局指南,是靠近顶部
现在,您可以从表格视图控制拖动到顶部,并选择顶部空间到顶部布局指南
它会给你一个关于TableView的模糊布局的错误,只需添加缺少的约束,你就完成了。
现在你可以像往常一样设置你的表视图,它不会剪辑状态栏!