ios - 为什么 UIViewController 在 UINavigationBar 下扩展,而

我有 UITabbarControllerUINavigationController。我有一个 UIView 的子类,我在 navController 中指定为 UIViewControllerview。这是很标准的东西,对吧?我就是这样做的

_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/

相关文章:

iphone - UISegmentedControl 文本以编程方式

ios - UITableViewCell 选中行的文字颜色变化

objective-c - 请求访问相机胶卷的权限

ios - 如何使用图像和标签制作自定义 UIBarButtonItem?

ios - 如何检查 NSArray 中的对象是否为 NSNull?

ios - 检测 iPhone 应用程序中是否存在摄像头?

iphone - 快速将单个引脚添加到 MKMapView?

ios - 如何在运行时更改约束优先级

ios - 禁用 UITextField 的自动更正

objective-c - 将 Xcode 组创建为文件系统文件夹的工作流程