iOS:以编程方式更新约束

时间:2016-05-11 05:28:09

标签: ios constraints nslayoutconstraint

我有这段代码来更新UITextFiled

的约束
- (void)updateUIOnePassword {

  NSLayoutConstraint *fullTextField = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
  NSLayoutConstraint *cutTextField = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-60.0f];

  if ([self isOnepasswordAvailable]) {
    self.onepasswordButton.alpha = 1.0f;
    [self.view removeConstraint:fullTextField];
    [self.view addConstraint:cutTextField];
  } else {
    self.onepasswordButton.alpha = 0.0f;
    [self.view addConstraint:fullTextField];
    [self.view updateConstraints];
  }

  [self.view setNeedsUpdateConstraints];
  [self.view layoutIfNeeded];
}

isOnepasswordAvailable在开始时为TRUE时,它可以正常工作,当我删除OP应用程序并且isOnepasswordAvailable为FALSE再次正常工作时,但当我在isOnepasswordAvailable时再次输入id为TRUE时约束不再合适了,我在consolle中有一些警告。 你知道为什么吗?

谢谢

2 个答案:

答案 0 :(得分:0)

虽然我总是喜欢使用IBOutlet进行约束,但是你已经以编程方式创建了一个约束我相信必须有一个很好的理由:)

我发现了一些问题,

1>我检查了你的两个约束,只有那些约束变化的东西是它们的常量值。我相信你不必创建两个不同的约束,并根据条件添加和删除它们。看起来非常复杂。

您可以拥有一个名为

的属性
@property (nonatomic,strong) NSLayoutConstraint *textFieldConstraint;

并在viewWillAppear

 if ([self isOnepasswordAvailable]) {
       self.textFieldConstraint  = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
}
else {
     self.textFieldConstraint  = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-60.0f];
}
[self.view addConstraint:self.textFieldConstraint];
[self.view layoutIfNeeded];

2 - ;将方法updateUIOnePassword更新为

- (void)updateUIOnePassword {
  if ([self isOnepasswordAvailable]) {
    self.onepasswordButton.alpha = 1.0f;
    self.textFieldConstraint.constant = 0.0f;
  } else {
    self.onepasswordButton.alpha = 0.0f;
    self.textFieldConstraint.constant = -0.60f;
  }
  [self.view layoutIfNeeded];
}

这应该做的工作:)看看好友:)

答案 1 :(得分:0)

NSTextField

您的陈述无效。 - updateUIOnePassword()被调用override func viewDidLoad() { super.viewDidLoad() didButtonSelectCallback = { (button) in if let currentButton = self.currentButton { currentButton.highlighted = !currentButton.highlighted if currentButton == button { self.currentButton = nil } else { self.currentButton = button } } else { self.currentButton = button } button.highlighted = !button.highlighted } addButtonAtRandomePlace() addButtonAtRandomePlace() didButtonSelectCallback(button: addButtonAtRandomePlace()) } override func mouseDragged(theEvent: NSEvent) { guard let button = currentButton else { return } NSCursor.closedHandCursor().set() button.frame.origin.x += theEvent.deltaX button.frame.origin.y -= theEvent.deltaY } private func addButtonAtRandomePlace() -> SelectableButton { let viewWidth = self.view.bounds.size.width let viewHeight = self.view.bounds.size.height let x = CGFloat(rand() % Int32((viewWidth - ButtonWidth))) let y = CGFloat(rand() % Int32((viewHeight - ButtonHeight))) let button = SelectableButton(frame: CGRectMake(x, y, ButtonWidth, ButtonHeight)) button.setButtonType(NSButtonType.ToggleButton) button.alignment = NSCenterTextAlignment button.bezelStyle = NSBezelStyle.RoundedBezelStyle button.didSelectCallback = didButtonSelectCallback self.view.addSubview(button) return button } 没有添加为约束,它只是一个分配。

意味着您正在尝试删除数组中不存在的对象(约束数组)

现在我的建议是,制作 - (void)updateUIOnePassword { NSLayoutConstraint *fullTextField = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f]; NSLayoutConstraint *cutTextField = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-60.0f]; if ([self isOnepasswordAvailable]) { self.onepasswordButton.alpha = 1.0f; [self.view removeConstraint:fullTextField]; //Invalid statement [self.view addConstraint:cutTextField]; } else { self.onepasswordButton.alpha = 0.0f; [self.view addConstraint:fullTextField]; [self.view updateConstraints]; } [self.view setNeedsUpdateConstraints]; [self.view layoutIfNeeded]; } fullTextField的班级实例只添加一次。

当你不需要它时

cutTextField

需要修改时

fullTextField