【问题标题】:How to force ios perform auto-layout?如何强制ios执行自动布局?
【发布时间】:2014-08-23 04:46:48
【问题描述】:

我正在将当前控制器的视图添加到容器视图中。第一个视图的矩形是原始设计矩形(320x568),然后更改为容器视图框架(320x386)。检查控制器的自动布局,并且 xib 包含视图垂直中心的按钮(IB 中视图垂直中心的蓝色虚线水平线)。但是在更改视图高度(386 而不是 568)后,按钮不在新的中心。

如何强制 ios 应用约束?

更换控制器:

- (void)switchToSection: (NSInteger)selectedIndex {
    [self.indexLabel setText: [[NSString alloc] initWithFormat:@"selected segment #%d", selectedIndex]];
    [self.indexLabel sizeToFit];
    self.indexLabel.center = CGPointMake(self.view.center.x, self.indexLabel.center.y);

    UIViewController *oldViewController =
        _currentSelectedIndex != UISegmentedControlNoSegment
            ? self.controllers[_currentSelectedIndex]
            : NULL;
    UIViewController *newViewController = self.controllers[selectedIndex];
    _currentSelectedIndex = selectedIndex;

    if (oldViewController != NULL)
        [self finishTransitionToOldViewController:oldViewController];

    [self willTransitionToViewController:newViewController]; // settings view.height = container.height
    [self.containerView addSubview:newViewController.view];
    [self didTransitionToViewController:newViewController];
}

通过控制器创建视图:

- (void)viewDidLoad
{
    [super viewDidLoad];
    CGRect viewRect = self.view.frame; // created originally viewRect   CGRect  origin=(x=0, y=0) size=(width=320, height=568)  
}

将容器的矩形应用到子视图:

- (void)willTransitionToViewController:(UIViewController *)viewController
{
    CGRect childRect = self.containerView.bounds;

    viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    viewController.view.frame = childRect; // expect iOS to apply constraints

    [viewController.view setNeedsUpdateConstraints]; // no luck
    [viewController.view layoutIfNeeded]; // no luck
}

【问题讨论】:

  • 你试过[self.view layoutIfNeeded]吗?
  • [viewController.view setNeedsUpdateConstraints] ?
  • 没有[viewController.view layoutIfNeeded]
  • 不幸的是,没有运气。我在viewController.view.autoresizingMask 之后尝试了layoutIfNeeded setNeedsUpdateConstraints 。我在设置矩形的地方添加了 cmets
  • 使用 cmets 更新代码

标签: ios autolayout


【解决方案1】:

使用自动布局的第一条规则是你永远不能使用someView.frame = blah;

您可以检查框架,但不能设置框架。

您的代码有点混乱。您说您正在使用 AutoLayout,但您显示的代码都没有使用 Auto Layout。

只是为了清楚。使用 autoresizingMask 不是使用 AutoLayout。

我猜你已经勾选了“使用自动布局”,但实际上并没有添加任何约束。

您能否展示带有您添加的约束的情节提要的屏幕截图。

编辑

好吧,我是对的。

首先,当您拖动视图时出现的蓝线不是约束。它们是帮助您在屏幕上放置东西的指南。

在自动布局中,您不使用CGRectMake(...) 定义视图的框架,而是向视图添加约束,以便可以推断其框架...10 points from the left of the screenbutton A is 15 points below the bottom of image view BLabel X is vertically centred in view Y

然而,它的意义远不止于此。

我建议从一个关于自动布局基础的教程开始。

我最喜欢的地方之一是Ray Wenderlich's beginning Auto Layout tutorial

【讨论】:

  • 按钮位于视图中心,它使用“自动布局”进行定位。控制器的“自动布局”被选中。我如何在这里添加图片?顺便说一句,autoresizingMask 是做什么的?
  • 在 IB 中添加了控制器视图屏幕截图。好的。如果我不能只使用view.frame = ...,我可以用什么来设置新的视图矩形
  • @4ntoine 您能否截屏显示所选按钮和检查器中的测量窗口(显示大小和位置的位)。这会有所帮助。
  • 更新了图片。我可以看到没有设置任何约束,但是在垂直移动按钮中心时我可以看到蓝色虚线。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-26
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 2013-04-12
  • 2016-03-05
  • 2014-10-23
相关资源
最近更新 更多