为什么 UIViewController 在 UINavigationBar 下扩展,而 UITableViewController 没有? [英] Why does UIViewController extend under UINavigationBar, while UITableViewController doesn't?

查看:17
本文介绍了为什么 UIViewController 在 UINavigationBar 下扩展,而 UITableViewController 没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UITabbarControllerUINavigationController 在里面.我有一个 UIView 的子类,我将它分配为 navControllerUIViewControllerview.这是很标准的东西,对吧?我就是这样做的

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;

这个 view 有一个 UITableView 作为 subView

This view has a 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].

For the sake of debugging I am setting self.backgroundColor = [UIColor blueColor] on the view.

从上面tableView的初始化可能会认为view和table的frame是一样的.但是,当我在 iOS 7 中运行时,视图的原点设置在 UINavigationBar 后面.这是可以理解的,因为我在 UINavigationController 的子类中设置了 self.navigationBar.translucent = YES;.但我不明白的是,桌子怎么就在 navBar 的正下方?它不应该也从 navBar 后面的 (0, 0) 开始吗?请参阅下面的屏幕截图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

现在,我在导航堆栈上push另一个viewController,只需使用[self.navigationController pushViewController.....].同样,我有一个自定义的 UIView,其中包含一个 tableView.然而,我在这张表上方还有一个 UILabel,再次为了调试,我给了它一个 redColor.这次我将标签的 origin 设置为与视图的

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.

我真的很想一致地对齐导航栏下方的子视图.我的问题是

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 选项卡上的调整滚动视图插入"设置的控制器,或者通过 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 的内容,如果您不希望其视图的内容扩展到顶部/底部栏下方,则可以使用 Interface Builder 中的 Extend Edges Under Top Bars/Under Bottom Bars 设置.这可以通过 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆