[英]Strange navigation controller behavior
I have a view that is pushed onto navigation stack like this: 我有一个这样的视图被推到导航堆栈中:
FriendsDetailViewController *detail = [[FriendsDetailViewController alloc] init];
detail.user = selectedUser;
[self.navigationController pushViewController:detail animated:YES];
Inside a view controller, I have two elements: my custom controls view, that is a view with two buttons and a label inside, and a table view. 在视图控制器内部,我有两个元素:我的自定义控件视图,即带有两个按钮和一个标签的视图以及一个表视图。 I am setting constraints to them as shown:
我正在为它们设置约束,如下所示:
-(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]];
}
But by the end I get unexpected result. 但是最后我得到了意外的结果。
Firstly, my custom controls view is black, although in code the background color is set to white. 首先,我的自定义控件视图为黑色,尽管在代码中背景颜色设置为白色。 Secondly, custom controls view is situated just as I expected, but my table View is messed up.
其次,自定义控件视图的位置与我期望的一样,但是我的表视图被弄乱了。 Somehow it does not sit on the bottom of my controls view.
不知何故,它不位于我的控件视图的底部。
I have other view controller without an embedded navigation controller, and the layout is just fine. 我还有其他没有嵌入式导航控制器的视图控制器,布局也很好。
It seems like I don't catch how navigation view is embedded in my view controller. 似乎我没有领会导航视图如何嵌入到我的视图控制器中。 I do the whole project without Interface builder, and this strange behavior is really confusing.
我在没有Interface builder的情况下完成了整个项目,这种奇怪的行为确实令人困惑。
Since iOS7, view controllers set scrollViews insets if there's a navigation bar, to make the content go behind the blurred bar (see https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instp/UIViewController/automaticallyAdjustsScrollViewInsets ) so to fix your tableView you just need to set self.automaticallyAdjustsScrollViewInsets = NO
从iOS7开始,如果有导航栏,则视图控制器会设置scrollViews插入项,以使内容位于模糊栏的后面(请参阅https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html #// apple_ref / occ / instp / UIViewController / automaticallyAdjustsScrollViewInsets ),以便修复tableView,您只需要设置
self.automaticallyAdjustsScrollViewInsets = NO
For the color of the other view, it's weird, but nothing in the code you posted changes the backgroundColor, are you sure you're setting it somewhere else? 对于其他视图的颜色,这很奇怪,但是您发布的代码中没有任何更改更改backgroundColor的内容,确定要在其他地方设置它吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.