当依赖约束改变时,自己进行UIView重新布局

时间:2016-12-11 09:35:38

标签: ios swift uiview nslayoutconstraint

我有一个UIImageViewUIButtonUIButtonUIImageView的顶部对齐。现在,在代码中,我正在更改UIImageView的顶部,但UIButton未相应更新。我在SetNeedsLayout上尝试LayoutIfNeededUIButton,但没有任何作用。

override func viewDidLayoutSubviews() {
    profileImageView.frame.origin.y = arcView.bounds.height/1.8
    editProfileButton.layer.setNeedsDisplay()
}

我在Storyboard中设置了约束。我真的很感激这里的任何帮助。

1 个答案:

答案 0 :(得分:1)

  • 首先:您确定所有约束(对于UIImageview和UIButton)都添加正确吗?

  • 第二:在处理约束时,你应该通过修改constant的值来约束UIImageview的origin.y

您应该更改约束的profileImageView.frame.origin.y,而不是直接更改constant,而是更改约会的origin.y,告诉我们什么是imageview constant(如果应用第一个点,则此约束必须存在。 );将此约束作为 IBOutlet 添加到viewController并更改其 class ViewController: UIViewController { // let's assume that this is the @IBOutlet of the constraint, I called it 'imageViewTopMargin': @IBOutlet weak var imageViewTopMargin: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() // it's not necessary -for sure- to do this the in the viewDidLoad method // but I'm doing this for demo purposes: // let's say that you want to push the imageView to the bottom with extra 40 points: imageViewTopMargin.constant += 40 } } 属性的值(请查看代码段中的注释,它是答案的一部分):

.container {
    display: flex;
    flex-direction: row;
}
.item {
    display: flex;
    flex-shrink: 0;
}
.divider {
    display: flex;
    flex: 1;
}

<div class=container>
    <div class=item>
        <div>Left One</div>
    </div>
    <div class=divider></div>
    <div class=item>
        <div>Right One</div>
    </div>
</div>

希望这会有所帮助。