ios - 奇怪的导航 Controller 行为

标签 ios objective-c iphone interface-builder ios-simulator

我有一个像这样被推送到导航堆栈的 View :

FriendsDetailViewController *detail = [[FriendsDetailViewController alloc] init];
detail.user = selectedUser;
[self.navigationController pushViewController:detail animated:YES];

在 View Controller 中,我有两个元素:我的自定义控件 View ,这是一个带有两个按钮和一个标签的 View ,以及一个 TableView 。我正在为它们设置约束,如图所示:

-(void)setupView {
    self.tableView = [[UITableView alloc] init];
    self.tableView.dataSource = self;
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.tableView];


    self.controlsView = [[ControlsView alloc] init];
    self.controlsView.player = self.player;
    self.controlsView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.controlsView];
    [self setControlsViewConstraints];
    [self setTableViewConstraints];
}

-(void)setTableViewConstraints {
    NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.controlsView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    NSLayoutConstraint *leadingConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
    NSLayoutConstraint *trailingConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];
    NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [self.view addConstraints:@[topConstraint, leadingConstraint, trailingConstraint, bottomConstraint]];
}

-(void)setControlsViewConstraints {
    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
    NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];
    NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30];
    [self.view addConstraints:@[top, leading, trailing, height]];
}

但到最后我得到了意想不到的结果。

enter image description here

首先,我的自定义控件 View 是黑色的,尽管在代码中背景颜色设置为白色。其次,自定义控件 View 的位置与我预期的一样,但我的 TableView 一团糟。不知何故,它没有位于我的控件 View 的底部。

我有其他没有嵌入式导航 Controller 的 View Controller ,布局也很好。 enter image description here

我好像没明白导航 View 是如何嵌入到我的 View Controller 中的。我在没有 Interface builder 的情况下完成整个项目,这种奇怪的行为确实令人困惑。

最佳答案

从 iOS7 开始,如果有导航栏, View Controller 会设置 scrollViews insets,以使内容位于模糊栏后面(参见 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instp/UIViewController/automaticallyAdjustsScrollViewInsets) 所以要修复你的 tableView 你只需要设置 self.automaticallyAdjustsScrollViewInsets = NO

对于另一个 View 的颜色,这很奇怪,但是您发布的代码中没有任何内容更改背景颜色,您确定要在其他地方设置它吗?

关于ios - 奇怪的导航 Controller 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32011705/

相关文章:

IOS ScrollView 问题

ios - tableview 中的图像加载另一个几秒钟

iOS 8 UIActionSheet 忽略 View Controller supportedInterfaceOrientations 和 shouldAutorotate

ios - UIImagePickerControllerOriginalImage 在 iPhone 4 中收到内存警告

iPhone - 使用 '*' 作为角标(Badge)

ios - Xcode 6 Beta 6 上的 iPhone 5c 配置

ios - UITapGestureRecognize 和 UILabel 在函数中获取标签文本

ios - Xcode 4 单元测试链接器错误

Objective-C:所有字符串文字是否总是加载到内存中?

objective-c - 如何使窗口处于事件状态或获得焦点?