ios - 使用约束将按钮移向目标,没有反应?

标签 ios objective-c xcode autolayout constraints

我希望红色按钮动画朝向第二个按钮的领先位置:

enter image description here

一些例子展示了如何用数字改变“常量”,但我想自动放在第二个按钮的前导位置。

我试过了,但是红色按钮没有移动,不过动画日志被正确调用了:

- (void)updateConstr{

    NSLayoutConstraint *newLeading = [NSLayoutConstraint
                                                  constraintWithItem:self.redB
                                                  attribute:NSLayoutAttributeLeading
                                                  relatedBy:NSLayoutRelationEqual
                                                  toItem:self.secondButton
                                                  attribute:NSLayoutAttributeLeading
                                                  multiplier:1.0
                                                  constant:0.0f];

    self.leadingConstraint = newLeading;//is an IBOutlet pointing on the constraint (see the image)
    [self.redB setNeedsUpdateConstraints];
    [UIView animateWithDuration:0.5 animations:^{
        [self.redB layoutIfNeeded];
        NSLog(@"animations");//is called, but the red button does not move
    }];
}

- (IBAction)firstAction:(id)sender { //after a click on button "one"
    NSLog(@"firstAction");
    [self updateConstr];
}

最佳答案

必须这样做:

- (void)updateConstr{

    NSLayoutConstraint *newLeading = [NSLayoutConstraint
                                      constraintWithItem:self.redB
                                      attribute:NSLayoutAttributeLeading
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.secondButton
                                      attribute:NSLayoutAttributeLeading
                                      multiplier:1.0
                                      constant:0.0f];

    [self.redB.superview removeConstraint: self.leadingConstraint];
    [self.redB.superview addConstraint: newLeading];
    self.leadingConstraint = newLeading;

    [UIView animateWithDuration:0.5 animations:^{
        [self.redB layoutIfNeeded];
    }];
}

关于ios - 使用约束将按钮移向目标,没有反应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27593430/

相关文章:

ios - Facebook 身份验证后 View 延迟显示

ios - 收到的事件子类型是无法识别的选择器

ios - 如何对 size_t 进行操作并最终得到 CGFloat?

iOS:用于发送图像数据的套接字通信

ios - 将值保存到磁盘 XCode

ios - 在 View Controller 之间传递数据 DidSelectRowsAtIndexPath Storyboard

Mac 10.6.3 上 xcode 3.2.3 安装失败

xcode - 在 Xcode 中以文本形式查看 .svg 文件?

ios - 如何使用 NSMutableArray 和 UISearchController 进行过滤

ios - 如何从弹出演示 View Controller 导航到另一个 View Controller