ios 如何让我的 iOS7 UITableViewController 不出现在顶部状态栏下?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20687082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make my iOS7 UITableViewController NOT appear under the top status bar?
提问by Travis Griggs
My root controller is a TabBarController (tabbed application). One of the tabs, is a UITableViewController. When I switch to that and scroll through the items, they show up under the status bar at the top (signal, battery, etc). I don't want that. I want that to be opaque or... something. It's visually jarring with the text of the table cells underlapping the status stuff.
我的根控制器是一个 TabBarController(选项卡式应用程序)。其中一个选项卡是 UITableViewController。当我切换到它并滚动项目时,它们会显示在顶部的状态栏下方(信号、电池等)。我不想要那个。我希望它是不透明的或......什么。表格单元格的文本与状态内容重叠,这在视觉上很不和谐。
Can I fix this from my Storyboard with some attributes setting that I don't understand? Or do I need to add some methods to my subclasses? Or maybe I need to wrap my UITableViewController with some other kind of controller?
我可以使用我不理解的一些属性设置从我的故事板中解决这个问题吗?或者我需要在我的子类中添加一些方法吗?或者我可能需要用其他类型的控制器包装我的 UITableViewController?
I've tried numerous variations of the ViewController Layout and Extend Edges settings in the Storyboard attributes page, but none of them seem to change it for the better.
我在 Storyboard 属性页面中尝试了 ViewController Layout 和 Extend Edges 设置的许多变体,但似乎没有一个变的更好。
Update: I think my problem is very similar to iOS 7: UITableView shows under status bar. The only difference, is that I'm embedded in a TabBarController, and that case is as the root view. I tried the solution listed there of embedding in a NavigationController
and setting Show Navigation Bar
to False
, but it didn't make any difference.
更新:我认为我的问题与iOS 7非常相似: UITableView 显示在 status bar 下。唯一的区别是我嵌入在 TabBarController 中,这种情况是作为根视图。我尝试了其中列出的嵌入 aNavigationController
并设置Show Navigation Bar
为的解决方案False
,但没有任何区别。
Screen Shots:
屏幕截图:
My storyboard (shrunk) showing a tabbed controller, with 2 children, one single view, and the other the table view.
我的故事板(缩小)显示了一个选项卡式控制器,有 2 个孩子,一个是单一视图,另一个是表视图。
Settings for the tab bar controller
标签栏控制器的设置
Settings for the table view controller
表视图控制器的设置
What the app ends up looking like on my phone
该应用程序最终在我的手机上看起来像什么
How the Story Ended
故事如何结束
Despite lots of answers below, none of them really worked. Some kind of a little, but not really. I tried the Embed in NavigationControllerapproach as well, and that also had issues. What did work though, was this:
尽管下面有很多答案,但没有一个真正有效。有点,但不是真的。我也尝试了Embed in NavigationController方法,但也有问题。什么起作用了,是这样的:
- Add UIViewController
- Set child controller relationship with it and tab bar controller (just like the other two I already had)
- Add a TableView (not controller) to the new UIViewController, position as desired, it'll snap to the bottom of the status bar
- Set the TableView's
delegate
andtableSource
as the new controller - Create a custom UIViewController subclass and update the class type of the controller in the storyboard
- Copy the table related methods from my custom UITableViewController subclass to my new subclass
- Select my prototype table cell from the original, and command+drag it to the new table view
- Happily deletethe original TableViewController (and wrapper NavigationController) too
- Update the tab bar item to match the previous
- Chock another one up for "you're trying to hard"
- 添加 UIViewController
- 设置子控制器与它和标签栏控制器的关系(就像我已经拥有的另外两个)
- 将 TableView(不是控制器)添加到新的 UIViewController,根据需要定位,它会吸附到状态栏的底部
- 将 TableView 的
delegate
和设置tableSource
为新的控制器 - 创建自定义 UIViewController 子类并更新 storyboard 中控制器的类类型
- 将与表相关的方法从我的自定义 UITableViewController 子类复制到我的新子类
- 从原始表格中选择我的原型表格单元格,然后命令+将其拖动到新表格视图
- 也愉快地删除原始的 TableViewController(和包装器 NavigationController)
- 更新标签栏项目以匹配上一个
- 为“你很努力”再添一个
采纳答案by Travis Griggs
In case anyone misses the How the story endedsection at the end of the (now long) question, the short answer is: Use a simple UIViewController with a TableView, instead of a TableViewController if you want to achieve the stated goal.
如果有人错过了(现在很长)问题末尾的故事如何结束部分,简短的回答是:如果您想实现既定目标,请使用带有 TableView 的简单 UIViewController而不是 TableViewController。
回答by JustAnotherCoder
Try this in viewDidLoad:
在 viewDidLoad 中试试这个:
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
20 px being the height of the status bar. If you have a navigation bar use 64 instead of 20
20 px 是状态栏的高度。如果您有导航栏,请使用 64 而不是 20
回答by Florian
In case anyone is still reading this thread:
如果有人仍在阅读此线程:
What worked for me is to also uncheck the "Extend Edges" options in the parent tab bar controller. That gives a consistent behaviour in iOS7 as well as iOS6.
对我有用的是还取消选中父标签栏控制器中的“扩展边缘”选项。这在 iOS7 和 iOS6 中提供了一致的行为。
回答by user3696154
I was having the same problem when using the SWRevealController.
使用 SWRevealController 时我遇到了同样的问题。
Using the comments above and below I was able to get it to work by putting this in the
使用上面和下面的评论,我能够通过把它放在
-(void)viewWillAppear instead of ViewDidLoad
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
回答by Robert
Have you tried adding something like this to the view controller's viewWillAppear
method:
你有没有试过在视图控制器的viewWillAppear
方法中添加这样的东西:
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
回答by Elsa
You can also solve this through Storyboard.
您也可以通过 Storyboard 解决这个问题。
Select the Table View from the Project Outline (left side of the editor) and then go to Properties (right side) > Size inspector tab > Scroll View > Content Insets > Top
从项目大纲(编辑器左侧)中选择表格视图,然后转到属性(右侧)> 大小检查器选项卡 > 滚动视图 > 内容插入 > 顶部
回答by Brian Broom
I added the following to viewWillAppear
我添加了以下内容 viewWillAppear
self.tableView.contentInset = UIEdgeInsetsMake(22, 0, 0, 0);
回答by Dilip Jangid
suppose your table's @IBOutlet is
假设您的表的@IBOutlet 是
@IBOutlet var tableView: UITableView!
create a function prepareTable(). add the below line into that method
创建一个函数prepareTable()。将以下行添加到该方法中
tableView.contentInset.top = UIApplication.shared.statusBarFrame.height
call this method from ViewDidLoad()
从 ViewDidLoad() 调用此方法
回答by Ricardo LR
For me an easy solution was (code is in C# that's because appears to be invalid)...
对我来说,一个简单的解决方案是(代码在 C# 中,因为它似乎无效)...
Just in the UITableViewController constructor create a new UIView (set same dimension of the TableView).
Add the TableView to the new created view.
Set new created View to the UITableViewController's View property...
只需在 UITableViewController 构造函数中创建一个新的 UIView(设置与 TableView 相同的维度)。
将 TableView 添加到新创建的视图中。
将新创建的 View 设置为 UITableViewController 的 View 属性...
On the constructor of the UITableViewController
关于 UITableViewController 的构造函数
var view = new UIView ();
view.Frame = this.TableView.Frame;
view.AddSubview (this.TableView);
this.View = view;
回答by bitmusher
As of iOS 8.4 I didn't get anywhere with the various storyboard options listed in other answers.
从 iOS 8.4 开始,我对其他答案中列出的各种故事板选项一无所知。
I worked around this without resorting to any dimensional constants by putting a regular view in my tab controller, then filling that with a "Container View" which I then connected to my UITableViewController with an "embed" segue.
我通过在我的选项卡控制器中放置一个常规视图,然后用“容器视图”填充它,然后我用“嵌入”转场连接到我的 UITableViewController 来解决这个问题,而没有诉诸任何维度常量。
My table now respects both my navigation bar and the tab bar at the bottom.
我的表格现在尊重我的导航栏和底部的标签栏。