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。
Abrahamchez的解决方案
https://developer.apple.com/library/ios/qa/qa1797/_index.html
为我工作如下。我有一个单独的UITableviewcontroller作为我的初始视图。我尝试过偏移码和嵌入navcon,但都没有解决状态栏透明度。
添加一个Viewcontroller并将其作为初始视图。这将显示关键的顶部和底部布局指南。
将旧的Tableview拖到新控制器的View中。
做所有的事情来改造表到新的控制器:
修改旧的视图controller.h文件,从UIViewController继承/子类,而不是UITableViewController。
添加UITableViewDataSource和UITableViewDelegate到视图控制器的。h。
重新连接故事板中需要的任何东西,比如搜索栏。
最重要的是设置约束条件,就像苹果问答一样。我没有插入工具栏。不确定确切的顺序。但是布局指南上出现了一个红色图标,可能是在我构建的时候。我点击它,让Xcode安装/清除约束。
然后我点击所有地方,直到我找到垂直空间约束,并将其顶部值从-20改为0,它完美地工作了。
适用于swift 3 - In viewDidLoad();
let statusBarHeight = UIApplication.shared.statusBarFrame.height
let insets = UIEdgeInsets(top: statusBarHeight, left: 0, bottom: 0, right: 0)
tableView.contentInset = insets
tableView.scrollIndicatorInsets = insets
这段代码:
1. 获取状态栏的高度
2. 为表视图的顶部提供一个与状态栏高度相等的内容插入。
3.为滚动指示器提供相同的插入,因此它出现在状态栏的下方。
对于任何有兴趣复制此操作的人,只需遵循以下步骤:
创建一个新的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.
我已经在一个空应用程序上测试了这两种方法,它们都可以工作。您可能需要做一些额外的调整,以使它们适用于您的项目。