首页 > 使用了autolayout就无法在viewDidLoad中改变视图的frame了吗?

使用了autolayout就无法在viewDidLoad中改变视图的frame了吗?


  1. 从初始位置出现是因为在执行布局操作的时候会根据你的约束来生成 frame,你的初始约束导致的这个行为的发生。

  2. 如果你想从左滑向中间,那么你应该先设置初始约束在屏幕左边 (UILabel)|Screen|。然后在执行 Target Action 行为时修改 UILabel 约束的 constant。接着执行布局操作。就如@paul_ding 的第二个答案,不过建议更改成:

- (IBAction)moveAction:(id)sender {
    self.lb_centerLeadingConstraint.constant=320;//根据需求改变大小
    [UIView animateWithDuration:1 animations:^{
        [self.lb_center layoutIfNeeded];

    }];
}

1 把label的约束拿出来

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lb_centerLeadingConstraint;

2 点击时改变约束

- (IBAction)moveAction:(id)sender {
    [UIView animateWithDuration:1 animations:^{
        self.lb_centerLeadingConstraint.constant=320;//根据需求改变大小
        [self.lb_center layoutIfNeeded];

    }];
}
【热门文章】
【热门文章】