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.
我尝试过很多不同的视图控制器布局和扩展故事板属性页面的边缘设置,但是没有一个看起来更好。
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.
更新:我认为我的问题非常类似于ios7: UITableView显示在状态栏下。唯一的区别是,我被嵌入到TabBarController中,这种情况是作为根视图。我尝试了其中列出的在NavigationController中嵌入并将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.
我的故事板(收缩)显示了一个选项卡式控制器,包含两个子视图、一个视图和另一个表视图。
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 NavigationController approach 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
- 向新的UIViewController中添加一个TableView(而不是controller),按需要的位置,它将会吸附到状态栏的底部
- Set the TableView's
delegate
andtableSource
as the new controller - 将TableView的委托和表源设置为新控制器
- Create a custom UIViewController subclass and update the class type of the controller in the storyboard
- 创建一个自定义的UIViewController子类并更新故事板中控制器的类类型
- Copy the table related methods from my custom UITableViewController subclass to my new subclass
- 将表相关的方法从我的自定义UITableViewController子类复制到我的新子类。
- Select my prototype table cell from the original, and command+drag it to the new table view
- 从原始文件中选择原型表单元格,并将它拖到新的表视图中。
- Happily delete the original TableViewController (and wrapper NavigationController) too
- 愉快地删除原始的TableViewController(和包装器导航控制器)
- Update the tab bar item to match the previous
- 更新选项卡栏项目,使其与前面的内容匹配
- Chock another one up for "you're trying to hard"
- 用另一个词来形容“你在努力”
13 个解决方案
#1
34
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
#2
16
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中给出了一致的行为。
#3
6
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);
#4
5
Have you tried adding something like this to the view controller's viewWillAppear
method:
您是否尝试过在视图控制器的viewWillAppear方法中添加如下内容:
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
#5
3
I added the following to viewWillAppear
我向viewWillAppear添加了以下内容
self.tableView.contentInset = UIEdgeInsetsMake(22, 0, 0, 0);
#6
3
You can also solve this through 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
从项目大纲(编辑器的左侧)中选择Table视图,然后转到属性(右侧)>大小检查器选项卡>滚动视图>内容嵌套>顶部
#7
2
Looks like you just want to make the NavBar nonTranslucent, you could try using
看起来你只是想让导航条不半透明,你可以试试
[self.navigationController.navigationBar setTranslucent:NO];
#8
1
In case anyone misses the How the story ended section 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。
#9
1
I encountered the same problem and the solution that worked for me was to add a section header view:
我遇到了同样的问题,对我起作用的解决方案是添加一个section header视图:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
I then added 20 to the origin.y of my text on the header.
然后把20加到原点。标题上的y。
I then changed the header height by adding 20 to the original height using the
然后,我通过使用
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return myHeight+20;
}
#10
1
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).
在UITableViewController构造函数中,创建一个新的UIView(对TableView设置相同的维度)。
-
Add the TableView to the new created view.
将TableView添加到新创建的视图中。
-
Set new created View to the UITableViewController's View property...
将新创建的视图设置为UITableViewController的视图属性…
On the constructor of the UITableViewController
var view = new UIView ();
view.Frame = this.TableView.Frame;
view.AddSubview (this.TableView);
this.View = view;
#11
1
As of iOS 8.4 I didn't get anywhere with the various storyboard options listed in other answers.
在ios8.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.
我解决了这个问题,没有使用任何维度常量,通过在我的tab控制器中放置一个常规视图,然后填充一个“容器视图”,然后我用一个“embed”segue连接到我的UITableViewController。
My table now respects both my navigation bar and the tab bar at the bottom.
我的表格现在尊重我的导航栏和底部的标签栏。
#12
1
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()调用此方法
#13
0
Let's say your Outline view, or Storyboard, is a Tab Bar Controller that currently has a Table View Controller. When you run the app, your table view cells are running under the status bar that seems to have a transparent background. This sucks!
比方说你的大纲视图,或者故事板,是一个标签栏控制器,它现在有一个表格视图控制器。当你运行这个应用程序时,你的表视图单元格在状态栏下运行,看起来有一个透明的背景。这也太无聊了吧!
If you spent hours trying everything you could find on * to no avail, felt like maybe it really was time to consider a new career and were preparing to dial 1-800-LUV-TRUK or 1-800-SHT-KIKR, give yourself a pat on the back, open up whatever elixir you drink in times such as this, because it's not your fault. Really. Travis is absolutely right that no amount of code in viewDidLoad, viewWillAppear, or viewDidAppear or button selecting/deselecting in IB will help in this situation.
如果你花了几个小时尝试所有你能找到的*无济于事,觉得也许真的是考虑一个新的职业和正准备拨打1 - 800 -爱-特鲁克岛或1 - 800 - sht kikr给自己拍拍他的背,打开任何灵丹妙药你喝在这样的时候,因为这不是你的错。真的。Travis是绝对正确的,在viewDidLoad、viewWillAppear、viewDidAppear或IB中的按钮选择/取消选择不会有任何帮助。
Travis' solution above will certainly work, but it's a bit long, there's copying and pasting of code, I have a short attention span such that an episode of Bugs Bunny feels like a full-length movie to me, so I just know that I'll screw-up anything that complicated. But, hey, your mileage may, nay likely will, vary. Anyhoo...
上面特拉维斯的解决方案肯定会奏效,但它有点长,有复制和粘贴代码,我的注意力集中时间很短,所以对我来说,《兔八哥》(Bugs Bunny)的一集就像一部完整的电影,所以我只知道我会搞砸任何复杂的东西。但是,嘿,你的哩数可能,不可能,会有所不同。Anyhoo……
In Xcode 7.3 for an app running iOS 9 (I assume 7 and 8 but I don't know this for certain and am currently too lazy to check) there is an easier way that doesn't require one to write any code. And it's done all within Xcode's Interface Builder.
在Xcode 7.3中,一个运行ios9的应用程序(我假设是7和8,但我不太清楚这一点,目前我懒得检查)有一种更简单的方法,不需要一个人写任何代码。这一切都是在Xcode的接口构建器中完成的。
(Caveat: Sorry if any of the terms aren't accurate. Please let me know where I was mistaken and I'll correct any mislabeling.)
(注意:如果有任何一个术语不准确,请表示歉意。请告诉我哪里错了,我将改正任何错误。
-
Go to the Utilities area of Interface Builder and select the Object library from the library pane.
转到接口生成器的实用程序区域,并从library窗格中选择对象库。
-
Select a Navigation View Controller object and drag it into your Storyboard scene. You'll notice that two scene items appear while you're dragging–these are a Navigation Controller Scene and a Table View Controller Scene.
选择一个导航视图控制器对象并将其拖到故事板场景中。您将注意到在拖拽时出现了两个场景项——它们是导航控制器场景和表视图控制器场景。
-
Delete the duplicate Table View Controller Scene that came-along with your Navigation Controller Scene.
删除与导航控制器场景同时出现的重复表视图控制器场景。
-
Select the relationship connection between your Tab Bar Controller and your Table View Controller and hit "Delete".
选择选项卡栏控制器和表视图控制器之间的关系连接,点击“删除”。
-
Reposition the Navigation Controller and your Table View Controller the way you want in your storyboard.
按照您在故事板中希望的方式重新定位导航控制器和表视图控制器。
-
Control-drag from your Tab Bar Controller Scene to the Navigation Controller Scene and select "Relationship Segue, view controller".
control -拖动标签栏控制器场景到导航控制器场景并选择“关系Segue,视图控制器”。
-
Control-drag from your Navigation Controller Scene to your Table View Controller Scene and select "Relationship Segue, root view controller".
control -拖动从导航控制器场景到表格视图控制器场景并选择“关系Segue,根视图控制器”。
-
Lastly, in the Utilities' Objects library, drag a Navigation Item object into your Table View Controller Scene's Table View.
最后,在实用程序的对象库中,将导航项对象拖放到表视图控制器场景的表视图中。
Now when you run your app, you will have a navigation bar and your table view cells will no longer appear under a transparent status bar.
现在,当你运行你的应用程序时,你会有一个导航栏,你的表格视图单元格将不再出现在一个透明的状态栏下。
Hope this helps.
希望这个有帮助。
#1
34
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
#2
16
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中给出了一致的行为。
#3
6
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);
#4
5
Have you tried adding something like this to the view controller's viewWillAppear
method:
您是否尝试过在视图控制器的viewWillAppear方法中添加如下内容:
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
#5
3
I added the following to viewWillAppear
我向viewWillAppear添加了以下内容
self.tableView.contentInset = UIEdgeInsetsMake(22, 0, 0, 0);
#6
3
You can also solve this through 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
从项目大纲(编辑器的左侧)中选择Table视图,然后转到属性(右侧)>大小检查器选项卡>滚动视图>内容嵌套>顶部
#7
2
Looks like you just want to make the NavBar nonTranslucent, you could try using
看起来你只是想让导航条不半透明,你可以试试
[self.navigationController.navigationBar setTranslucent:NO];
#8
1
In case anyone misses the How the story ended section 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。
#9
1
I encountered the same problem and the solution that worked for me was to add a section header view:
我遇到了同样的问题,对我起作用的解决方案是添加一个section header视图:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
I then added 20 to the origin.y of my text on the header.
然后把20加到原点。标题上的y。
I then changed the header height by adding 20 to the original height using the
然后,我通过使用
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return myHeight+20;
}
#10
1
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).
在UITableViewController构造函数中,创建一个新的UIView(对TableView设置相同的维度)。
-
Add the TableView to the new created view.
将TableView添加到新创建的视图中。
-
Set new created View to the UITableViewController's View property...
将新创建的视图设置为UITableViewController的视图属性…
On the constructor of the UITableViewController
var view = new UIView ();
view.Frame = this.TableView.Frame;
view.AddSubview (this.TableView);
this.View = view;
#11
1
As of iOS 8.4 I didn't get anywhere with the various storyboard options listed in other answers.
在ios8.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.
我解决了这个问题,没有使用任何维度常量,通过在我的tab控制器中放置一个常规视图,然后填充一个“容器视图”,然后我用一个“embed”segue连接到我的UITableViewController。
My table now respects both my navigation bar and the tab bar at the bottom.
我的表格现在尊重我的导航栏和底部的标签栏。
#12
1
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()调用此方法
#13
0
Let's say your Outline view, or Storyboard, is a Tab Bar Controller that currently has a Table View Controller. When you run the app, your table view cells are running under the status bar that seems to have a transparent background. This sucks!
比方说你的大纲视图,或者故事板,是一个标签栏控制器,它现在有一个表格视图控制器。当你运行这个应用程序时,你的表视图单元格在状态栏下运行,看起来有一个透明的背景。这也太无聊了吧!
If you spent hours trying everything you could find on * to no avail, felt like maybe it really was time to consider a new career and were preparing to dial 1-800-LUV-TRUK or 1-800-SHT-KIKR, give yourself a pat on the back, open up whatever elixir you drink in times such as this, because it's not your fault. Really. Travis is absolutely right that no amount of code in viewDidLoad, viewWillAppear, or viewDidAppear or button selecting/deselecting in IB will help in this situation.
如果你花了几个小时尝试所有你能找到的*无济于事,觉得也许真的是考虑一个新的职业和正准备拨打1 - 800 -爱-特鲁克岛或1 - 800 - sht kikr给自己拍拍他的背,打开任何灵丹妙药你喝在这样的时候,因为这不是你的错。真的。Travis是绝对正确的,在viewDidLoad、viewWillAppear、viewDidAppear或IB中的按钮选择/取消选择不会有任何帮助。
Travis' solution above will certainly work, but it's a bit long, there's copying and pasting of code, I have a short attention span such that an episode of Bugs Bunny feels like a full-length movie to me, so I just know that I'll screw-up anything that complicated. But, hey, your mileage may, nay likely will, vary. Anyhoo...
上面特拉维斯的解决方案肯定会奏效,但它有点长,有复制和粘贴代码,我的注意力集中时间很短,所以对我来说,《兔八哥》(Bugs Bunny)的一集就像一部完整的电影,所以我只知道我会搞砸任何复杂的东西。但是,嘿,你的哩数可能,不可能,会有所不同。Anyhoo……
In Xcode 7.3 for an app running iOS 9 (I assume 7 and 8 but I don't know this for certain and am currently too lazy to check) there is an easier way that doesn't require one to write any code. And it's done all within Xcode's Interface Builder.
在Xcode 7.3中,一个运行ios9的应用程序(我假设是7和8,但我不太清楚这一点,目前我懒得检查)有一种更简单的方法,不需要一个人写任何代码。这一切都是在Xcode的接口构建器中完成的。
(Caveat: Sorry if any of the terms aren't accurate. Please let me know where I was mistaken and I'll correct any mislabeling.)
(注意:如果有任何一个术语不准确,请表示歉意。请告诉我哪里错了,我将改正任何错误。
-
Go to the Utilities area of Interface Builder and select the Object library from the library pane.
转到接口生成器的实用程序区域,并从library窗格中选择对象库。
-
Select a Navigation View Controller object and drag it into your Storyboard scene. You'll notice that two scene items appear while you're dragging–these are a Navigation Controller Scene and a Table View Controller Scene.
选择一个导航视图控制器对象并将其拖到故事板场景中。您将注意到在拖拽时出现了两个场景项——它们是导航控制器场景和表视图控制器场景。
-
Delete the duplicate Table View Controller Scene that came-along with your Navigation Controller Scene.
删除与导航控制器场景同时出现的重复表视图控制器场景。
-
Select the relationship connection between your Tab Bar Controller and your Table View Controller and hit "Delete".
选择选项卡栏控制器和表视图控制器之间的关系连接,点击“删除”。
-
Reposition the Navigation Controller and your Table View Controller the way you want in your storyboard.
按照您在故事板中希望的方式重新定位导航控制器和表视图控制器。
-
Control-drag from your Tab Bar Controller Scene to the Navigation Controller Scene and select "Relationship Segue, view controller".
control -拖动标签栏控制器场景到导航控制器场景并选择“关系Segue,视图控制器”。
-
Control-drag from your Navigation Controller Scene to your Table View Controller Scene and select "Relationship Segue, root view controller".
control -拖动从导航控制器场景到表格视图控制器场景并选择“关系Segue,根视图控制器”。
-
Lastly, in the Utilities' Objects library, drag a Navigation Item object into your Table View Controller Scene's Table View.
最后,在实用程序的对象库中,将导航项对象拖放到表视图控制器场景的表视图中。
Now when you run your app, you will have a navigation bar and your table view cells will no longer appear under a transparent status bar.
现在,当你运行你的应用程序时,你会有一个导航栏,你的表格视图单元格将不再出现在一个透明的状态栏下。
Hope this helps.
希望这个有帮助。