【发布时间】:2014-11-07 04:23:34
【问题描述】:
只需将子视图UIView 从使用instantiateViewControllerWithIdentifier 创建的控制器添加到当前控制器视图,并添加约束以保持新子视图的大小以覆盖整个区域。下面的代码在 iOS 7 中工作。iOS 8 将子视图的大小调整到左上角的一小部分。 iOS 7 符合我的预期,即在父视图的整个大小上调整子视图的大小。我会附上一张图片,但没有代表。将 translatesAutoresizingMaskIntoConstraints 设置为 YES 可以解决此问题,但是当方向更改或大小更改时,视图不会遵守约束并调整大小。
spotCheckStoresViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"visitStoresViewController"];
spotCheckStoresViewController.mainViewController = self;
spotCheckStoresViewController.dataMode = kDataModeViews;
spotCheckStoresViewController.lastRow = [self getViewsLastRow];
spotCheckStoresViewController.view.tag = -200;
currentView = spotCheckStoresViewController.view;
[self addChildViewController:spotCheckStoresViewController];
[self.view insertSubview:currentView belowSubview:_menuViewController.view];
currentView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
//[self.view updateConstraints];
//[self.view layoutIfNeeded];
我也尝试过设置子视图的框架。任何想法在 iOS 8 中可能发生了哪些变化以改变以这种方式使用的约束的行为?
【问题讨论】:
-
我已经为自己解决了这个问题,方法是向 IB 中的控制器主 UIView 添加另一个子 UIView,设置约束以坚持前导、尾随、顶部和底部,然后添加我的原始子-view 到那个子 UIView。似乎完成了这项工作。
-
在 iOS 8 中添加了几个新的布局指南,
leftLayoutGuide和rightLayoutGuide。您可以尝试将您的约束设置为那些,但请记住,这在 iOS 8 之前是不可用的。 -
这与您的问题无关,但您忘记致电
didMoveToParentViewController:。 -
@AndrewMonshizadeh 没有
leftLayoutGuide和rightLayoutGuide这样的东西。 iOS 8 中存在边距,但这无关紧要;手动构造的约束不会固定到边距。 -
@bavelasq 什么时候上面的代码运行,好吗?即周围的方法是什么?
标签: ios objective-c cocoa-touch uiview