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
我有UITabbarController和UINavigationController。我有一个UIView的子类我把它作为UIViewController的视图分配给navController。这是很标准的东西,对吧?我就是这么做的
_productCategoryView = [[ProductCategoryView alloc] initWithFrame:self.view.frame];
self.view = _productCategoryView;
This view
has a UITableView
as subView
这个视图有一个UITableView作为子视图
_productCategoryTableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
_productCategoryTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_productCategoryTableView.backgroundColor = [UIColor clearColor];
[self addSubview:_productCategoryTableView];
For the sake of debugging I am setting self.backgroundColor = [UIColor blueColor]
on the view.
为了调试,我设置了self。背景色= [UIColor blueColor]在视图上。
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
从上面对tableView的初始化可以看出,视图和表的框架是相同的。然而,当我在ios7中运行时,视图的起源被设置在UINavigationBar后面。这是可以理解的,因为我设置了self。navigationbar。半透明的=是的;在UINavigationController的子类中。但是我不明白的是为什么桌子就在导航条下面?它是不是也应该从(0,0)navBar后面开始?参见下面的截图场景1。注意导航条后面的蓝色色调
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
现在,我在导航堆栈上推另一个viewController,通过使用[self。导航控制器pushViewController .....]。我有一个自定义的UIView,里面有tableView。但是,我在这个表上面也有一个UILabel,为了调试,我给了它一个红色。这一次,我将标签的起点设置为几乎与视图的起点相同
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);
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
.
根据上面的逻辑,标签应该是可见的,对吧?但这次不是。这次标签在导航条后面。
Notice, the red hue behind navBar.
注意,导航条后面的红色色调。
I would really like to align the subView below the navBar consistently. My questions are
我真的希望将子视图始终对齐到导航条下面。我的问题是
1. How is the tableView offset by 64pixels (height of nav + status bar in iOS 7) automatically, even though it's frame is same as the view's?
1。虽然tableView的框架和视图的框架是一样的,但是它是如何自动被64px (iOS 7中nav +状态栏的高度)偏移的呢?
2. Why does that not happen in the second view?
2。为什么这不会发生在第二视图中?
3 个解决方案
#1
130
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.
默认情况下,UITableViewController的视图会自动插入iOS7中,这样它们就不会从导航栏/状态栏开始。这是控制器通过在接口构建器中的UITableViewController的属性检查器选项卡上的“调整滚动视图内嵌”设置,或者通过UIViewController的setautomaticallycontrollsscrollviewinsets:方法。
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的内容,如果您不希望它的视图内容扩展到顶部/底部栏下,您可以使用接口构建器中顶部栏/底部栏设置下的扩展边。可以通过edgesForExtendedLayout属性访问它。
#2
98
- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
}
Swift 2:
斯威夫特2:
self.edgesForExtendedLayout = UIRectEdge.None
Swift 3:
斯威夫特3:
self.edgesForExtendedLayout = []
#3
9
@Gank's answer is correct, but the best place to do this is on the UINavigationControllerDelegate
(if you have one):
@Gank的答案是正确的,但最好的做法是在UINavigationControllerDelegate上(如果你有的话):
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
viewController.edgesForExtendedLayout = UIRectEdge.None
}
#1
130
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.
默认情况下,UITableViewController的视图会自动插入iOS7中,这样它们就不会从导航栏/状态栏开始。这是控制器通过在接口构建器中的UITableViewController的属性检查器选项卡上的“调整滚动视图内嵌”设置,或者通过UIViewController的setautomaticallycontrollsscrollviewinsets:方法。
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的内容,如果您不希望它的视图内容扩展到顶部/底部栏下,您可以使用接口构建器中顶部栏/底部栏设置下的扩展边。可以通过edgesForExtendedLayout属性访问它。
#2
98
- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
}
Swift 2:
斯威夫特2:
self.edgesForExtendedLayout = UIRectEdge.None
Swift 3:
斯威夫特3:
self.edgesForExtendedLayout = []
#3
9
@Gank's answer is correct, but the best place to do this is on the UINavigationControllerDelegate
(if you have one):
@Gank的答案是正确的,但最好的做法是在UINavigationControllerDelegate上(如果你有的话):
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
viewController.edgesForExtendedLayout = UIRectEdge.None
}