为什么UIViewController在UINavigationBar下扩展,而UITableViewController却没有? [英] Why does UIViewController extend under UINavigationBar, while UITableViewController doesn't?
问题描述
我有 UITabbarController
,其中包含 UINavigationController
。我有一个 UIView
的子类,我指定为 UIViewController的
在视图
navController
中。这是非常标准的东西,对吗?我是这样做的。
I have UITabbarController
with UINavigationController
in it. I have a subclass of UIView
that I assign as the view
of UIViewController
in the navController
. This is pretty standard stuff, right? This is how I do it
_productCategoryView = [[ProductCategoryView alloc] initWithFrame:self.view.frame];
self.view = _productCategoryView;
此视图
有 UITableView
as subView
_productCategoryTableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
_productCategoryTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_productCategoryTableView.backgroundColor = [UIColor clearColor];
[self addSubview:_productCategoryTableView];
为了调试我设置 self.backgroundColor = [UIColor blueColor在视图上显示
。
从 tableView
的上述初始化中,可能会认为视图和表的框架
是一样的。但是当我在 iOS 7
中运行时,视图的原点设置在 UINavigationBar
之后。这是可以理解的,因为我在 UINavigationController
的子类中设置 self.navigationBar.translucent = YES;
。但我不明白的是桌子是如何坐在 navBar
的正下方?它不应该从(0,0)
开始,它位于 navBar
之后?请参阅下面的屏幕截图 Scenario 1
。注意蓝色色调 navBar
From the above initialization of tableView
one might think that the view's and table's frame
is same. However when I run in iOS 7
, the view's origin is set behind the UINavigationBar
. This is understandable because I am setting self.navigationBar.translucent = YES;
in my subclass of UINavigationController
. But what I don't understand is how come the table is sitting just below the navBar
? Shouldn't it also start from (0, 0)
which is behind the navBar
? See screenshot Scenario 1
below. Notice the blue hue behind navBar
现在,我推送
另一个 viewController
在导航堆栈上,只需使用 [self.navigationController pushViewController .....]
。我再次有一个自定义 UIView
,其中包含 tableView
。但是我在这个表上面还有一个 UILabel
,再次进行调试,我给它一个 redColor
。这次我将标签的来源
设置为与视图的几乎相同
Now, I push
another viewController
on the navigation stack, simply by using [self.navigationController pushViewController.....]
. Again I have a custom UIView
with a tableView
in it. However I also have a UILabel
above this table, and again for debugging, I gave it a redColor
. This time I am setting the label's origin
to be almost same as the view's
CGRect boundsInset = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(10, 10, 10, 10));
CGSize textSize = [_titleLabel.text sizeWithFont:_titleLabel.font
constrainedToSize:CGSizeMake(boundsInset.size.width, MAXFLOAT)
lineBreakMode:NSLineBreakByWordWrapping];
printSize(textSize);
_titleLabel.frame = CGRectMake(boundsInset.origin.x,
boundsInset.origin.y,
boundsInset.size.width,
textSize.height);
因此,按照上面的逻辑,标签应该是可见的,对吗?但这次不是。这次标签位于 navBar
之后。
So, going by the logic above, the label should be visible, right? But this time it's not. This time the label is behind the navBar
.
注意,navBar背后的红色色调。
Notice, the red hue behind navBar.
我真的想在navBar下面一致地调整子视图。我的问题是
I would really like to align the subView below the navBar consistently. My questions are
1。 tableView如何自动偏移64像素(导航高度+ iOS 7中的状态栏),即使它的帧与视图相同?
2。为什么这不会发生在第二个视图中?
推荐答案
默认情况下,UITableViewController的视图会自动插入在iOS7中,它们不会从导航栏/状态栏下面开始。这是通过Interface Builder中UITableViewController的Attributes Inspector选项卡上的Adjust scroll view insets设置控制,或者是UIViewController的 setAutomaticallyAdjustsScrollViewInsets:
方法。
By default, UITableViewController's views are automatically inset in iOS7 so that they don't start below the navigation bar/status bar. This is controller by the "Adjust scroll view insets" setting on the Attributes Inspector tab of the UITableViewController in Interface Builder, or by the setAutomaticallyAdjustsScrollViewInsets:
method of UIViewController.
对于UIViewController的内容,如果您不希望其视图的内容在顶部/底部栏下方扩展,则可以使用接口顶部栏杆下方的延伸边缘/底部条形图下的设置生成器。这可以通过 edgesForExtendedLayout
属性访问。
For a UIViewController's contents, if you don't want its view's contents to extend under the top/bottom bars, you can use the Extend Edges Under Top Bars/Under Bottom Bars settings in Interface Builder. This is accessible via the edgesForExtendedLayout
property.
这篇关于为什么UIViewController在UINavigationBar下扩展,而UITableViewController却没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!