我有 UITabbarController
和 UINavigationController
。我有一个 UIView
的子类,我在 navController
中指定为 UIViewController
的 view
。这是很标准的东西,对吧?我就是这样做的
_productCategoryView = [[ProductCategoryView alloc] initWithFrame:self.view.frame];
self.view = _productCategoryView;
这个 view
有一个 UITableView
作为 subView
_productCategoryTableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
_productCategoryTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_productCategoryTableView.backgroundColor = [UIColor clearColor];
[self addSubview:_productCategoryTableView];
为了调试,我在 View 上设置了 self.backgroundColor = [UIColor blueColor]
。
从上面tableView
的初始化可能会认为view和table的frame
是一样的。但是,当我在 iOS 7
中运行时, View 的原点设置在 UINavigationBar
后面。这是可以理解的,因为我在 UINavigationController
的子类中设置了 self.navigationBar.translucent = YES;
。但我不明白的是,这张 table 怎么就坐在 navBar
的正下方?它不应该也从 navBar
后面的 (0, 0)
开始吗?请参阅下面的屏幕截图 Scenario 1
。注意 navBar
现在,我在导航堆栈上push
另一个viewController
,只需使用[self.navigationController pushViewController.....]
。我再次有一个自定义的 UIView
,其中有一个 tableView
。但是我在这个表上方还有一个 UILabel
,为了调试,我给了它一个 redColor
。这次我将标签的 origin
设置为与 View 的
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
后面。
注意,navBar 后面的红色调。
我真的很想始终对齐导航栏下方的 subview 。我的问题是
1。 tableView 如何自动偏移 64 像素(iOS 7 中导航 + 状态栏的高度),即使它的框架与 View 的框架相同?
2。为什么在第二个 View 中没有发生这种情况?
最佳答案
默认情况下,iOS7 中 UITableViewController 的 View 会自动插入,因此它们不会从导航栏/状态栏下方开始。这是通过 Interface Builder 中 UITableViewController 的 Attributes Inspector 选项卡上的“调整 ScrollView 插图”设置或 UIViewController 的 setAutomaticallyAdjustsScrollViewInsets:
方法设置的 Controller 。
对于 UIViewController 的内容,如果您不希望其 View 的内容在顶部/底部栏下扩展,您可以使用 Interface Builder 中的在顶部栏/底部栏下扩展边缘设置。这可以通过 edgesForExtendedLayout
属性访问。
关于ios - 为什么 UIViewController 在 UINavigationBar 下扩展,而 UITableViewController 没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19143353/