2017-10-08 102 views
1

我遇到了一个问题,我想要一个视图(indicatorView)的宽度等于一个按钮。我正在使用Snapkit,但我认为这个问题也应该与裸NSLayoutConstraint相关。以下是我在做什么使indicatorView's宽度等于button约束使宽度等于另一个

indicatorView.width.equalTo(button.snp.width) 

然而,这使得按钮的宽度等于所述indicatorView的宽度,而不是使indicatorView的宽度等于按钮的宽度。

如何指定关系的方向?那可能吗?

+0

你是指'ActivityIndi​​catorView'? –

+0

像这样的约束始终是双向的。它只是说这两个项目必须具有相同的宽度。你必须通过宽度约束或通过绑定它的边缘来为你的UIButton设置一个明确的宽度。 – danieltmbr

回答

0
let f = indicatorView.frame 
indicatorView.frame = CGRect(x: f.origin.x, y: f.origin.y: width: button.snp.width, height: f.height) 
1
indicatorView.translatesAutoresizingMaskIntoConstraints = false 
indicatorView.widthAnchor.constraint(equalToConstant: button.widthAnchor).isActive = true 
0

你是否在正确的封闭包装呢?所以它应该看起来像这样:

indicatorView.snp.makeConstraints { (make) in 
     make.width.equalTo(button) 
}