【发布时间】:2014-07-24 11:58:00
【问题描述】:
我有两个按钮,都启用了自动布局,但在运行时我需要更改它们的 frame 。我改变了但对它们没有影响我通过调用这个constraintWithItem方法和
[self.btnDeviceContacts updateConstraints];
[self.btnFacebookContacts updateConstraints];
尝试了很多次
还有[self.view setNeedsDisplay];
[self.view layoutSubviews];
调用此constraintWithItem 不会对此产生影响,仅影响第一个按钮
NSLayoutConstraint *constrain1 = [NSLayoutConstraint
constraintWithItem:self.btnDeviceContacts
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:160];
[self.view addConstraint:constrain2];
NSLayoutConstraint *constrain2 = [NSLayoutConstraint
constraintWithItem:self.btnFacebookContacts
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:160];
NSArray * array = [[NSArray alloc] initWithObjects:constrain1,constrain2, nil];
[self.view addConstraints:array];
故事板上按钮的初始帧
[self.btnDeviceContacts setFrame:CGRectMake(0, 60, 106, self.btnDeviceContacts.frame.size.height)];
[self.btnFacebookContacts setFrame:CGRectMake(107, 60, 106, self.btnFacebookContacts.frame.size.height)];
在代码中改变了按钮的框架:
[self.btnDeviceContacts setFrame:CGRectMake(0, 60, 160, self.btnDeviceContacts.frame.size.height)];
[self.btnFacebookContacts setFrame:CGRectMake(161, 60, 160, self.btnFacebookContacts.frame.size.height)];
请帮我解决这个问题,如何更新约束或如何解决这个问题。提前致谢。
【问题讨论】:
标签: ios objective-c xcode autolayout nslayoutconstraint