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中更改顶部栏下的设置和调整滚动视图,迫使框架到一个新的区域。
一图胜千言万语:
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]];
干杯,本
我最终使用了一个额外的背景视图,添加在TableView之后,放在状态栏下:
self.CoverView = [[UIView alloc]init];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
self.CoverView.frame = CGRectMake(0,0,self.view.bounds.size.width,20);
}
self.CoverView.backgroundColor = [UIColor whiteColor];
self.TableView = [[UITableView alloc]initWithFrame:CGRectMake(0,
self.CoverView.bounds.size.height,XXX, YYY)];
[self.view addSubview:self.TableView];
[self.view addSubview:self.CoverView];
它不是很漂亮,但它是一个相当简单的解决方案,如果你需要使用xib-less视图,同时适用于IOS6和IOS7
对于那些像我一样不愿意在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
}
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,它完美地工作了。