【问题标题】:Constraints for subview not working in iOS 8, XCode 6 (6A313). Works iOS 7子视图的约束在 iOS 8、XCode 6 (6A313) 中不起作用。适用于 iOS 7
【发布时间】: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 中添加了几个新的布局指南,leftLayoutGuiderightLayoutGuide。您可以尝试将您的约束设置为那些,但请记住,这在 iOS 8 之前是不可用的。
  • 这与您的问题无关,但您忘记致电didMoveToParentViewController:
  • @AndrewMonshizadeh 没有 leftLayoutGuiderightLayoutGuide 这样的东西。 iOS 8 中存在边距,但这无关紧要;手动构造的约束不会固定到边距。
  • @bavelasq 什么时候上面的代码运行,好吗?即周围的方法是什么?

标签: ios objective-c cocoa-touch uiview


【解决方案1】:

就我而言,问题与标记为UIView-Encapsulated-Layout-WidthUIView-Encapsulated-Layout-Height 的约束有关。当我删除它们时,一切都表现得好像我的视图大小为零,一切都集中在屏幕的左上角。当我把它们留在里面时,新的约束按预期工作。我还保留了标记为_UILayoutSupportConstraint 的约束。

【讨论】:

    【解决方案2】:

    就我而言,它只发生在 iOS 8.3 上。与现有约束存在冲突。一位同事发现我需要先删除所有现有的约束,然后再添加其他约束。

    [self.view removeConstraints:self.constraints];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 2015-03-16
      • 2014-12-08
      相关资源
      最近更新 更多