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
}
}
再加上上面的答案:
在第二种方法最初似乎不工作后,我做了一些额外的修补,找到了解决方案。
TLDR;顶部答案的第二个解决方案几乎是可行的,但对于某些版本的xCode, ctrl+拖动到“顶部布局指南”并选择垂直间距不起作用。然而,首先调整表格视图的大小,然后选择“顶部空间到顶部布局指南”即可
将一个空白的ViewController拖到故事板上。
将UITableView对象拖到View中。(不是UITableViewController)。使用蓝色布局指南将其定位在正中心。
拖一个UITableViewCell到TableView中。这将是你的原型重用单元格,所以不要忘记在Attributes选项卡下设置它的重用标识符,否则你会崩溃。
创建UIViewController的自定义子类,并添加<UITableViewDataSource, UITableViewDelegate>协议。不要忘记在标识检查器中将你的故事板的ViewController设置为这个类。
在你的实现文件中为你的TableView创建一个outlet,命名为TableView
右键单击TableView并将数据源和委托拖到你的ViewController中。
现在是不剪切到状态栏的部分。
抓取你的表视图的顶部边缘,并将其移动到一个虚线蓝色的自动布局指南,是靠近顶部
现在,您可以从表格视图控制拖动到顶部,并选择顶部空间到顶部布局指南
它会给你一个关于TableView的模糊布局的错误,只需添加缺少的约束,你就完成了。
现在你可以像往常一样设置你的表视图,它不会剪辑状态栏!
我使用一个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。