Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ios swift中以编程方式创建自动布局等宽约束?_Ios_Swift_Autolayout - Fatal编程技术网

如何在ios swift中以编程方式创建自动布局等宽约束?

如何在ios swift中以编程方式创建自动布局等宽约束?,ios,swift,autolayout,Ios,Swift,Autolayout,如何在多个视图中以编程方式提供相等宽度和相等高度的约束。我通过自动布局检查了google,但并不是完美的答案 我的代码如下所示: var countNoOfViews:Int = 3 @IBOutlet var viewForRow1: UIView! override func viewDidLoad() { super.viewDidLoad() self.specialButtonViewLeft() } func specialButtonViewLef

如何在多个视图中以编程方式提供相等宽度和相等高度的约束。我通过自动布局检查了google,但并不是完美的答案

我的代码如下所示:

  var countNoOfViews:Int = 3   
 @IBOutlet var viewForRow1: UIView!

override func viewDidLoad() {
        super.viewDidLoad()
 self.specialButtonViewLeft()
}

func specialButtonViewLeft(){

    for i in 0..<countNoOfViews{
        var customView:UIView!
        customView = UIView(frame: CGRect.zero)
        customView.translatesAutoresizingMaskIntoConstraints = false
        viewForRow1.addSubview(customView)

        let widthConstraint = NSLayoutConstraint(item: customView, attribute: .width, relatedBy: .equal,toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 20.0)

        let topConstraint = NSLayoutConstraint(item: customView, attribute: .top, relatedBy: .equal,toItem: self.viewForRow1, attribute: .top, multiplier: 1.0, constant: 0)

        let bottomConstraint = NSLayoutConstraint(item: customView, attribute: .bottom, relatedBy: .equal,toItem: self.viewForRow1, attribute: .bottom, multiplier: 1.0, constant: 0)

        let leadingConstraint = NSLayoutConstraint(item: customView, attribute: .leading, relatedBy: .equal, toItem: self.viewForRow1, attribute: .leading, multiplier: 1, constant: customLeadingSpaceLeft)

        customLeadingSpaceLeft = customLeadingSpaceLeft + customViewWidth

        arrayLeftBtnConstraints.append(widthConstraint)

        if i == 0{
            customView.backgroundColor = UIColor.red
        }else if i == 1{
            customView.backgroundColor = UIColor.green
        }else if i == 2{
            leftViewVal = customLeadingSpaceLeft
            customView.backgroundColor = UIColor.black
        }
        customView.alpha = 0.50
        viewForRow1.addConstraints([widthConstraint, leadingConstraint,topConstraint,bottomConstraint])
    }
}
var countNoOfViews:Int=3
@IBOUTLE var viewForRow1:UIView!
重写func viewDidLoad(){
super.viewDidLoad()
self.specialButtonViewLeft()
}
func specialButtonViewLeft(){
对于0..中的i,请尝试以下方法:

let widthConstraint = NSLayoutConstraint(item: customView, attribute: .width, relatedBy: .equal, toItem: viewForRow1, attribute: .width, multiplier: 0.25, constant: 0.0)

乘数:0.25
,表示
自定义视图的宽度将是父视图的1/4,
视图的行1
您有三种可能的方法:

1) 通过使用锚:

view.widthAnchor.constraint(equalTo:otherView.widthAnchor,乘数:1.0)。isActive=true

2) 视觉格式:

简单示例-
“H:|[otherView]-[view(=otherView)]|”

3) “老派”制约因素:

NSLayoutConstraint(项:#视图,属性:。宽度,相关者:。相等,到项:#其他视图,属性:。宽度,乘数:1.0,常数:0.0)。isActive=true


希望它能帮助一些人。

谢谢。但是我有多个视图将宽度等分为超级视图。另外,我想根据方向更改编程自动布局约束的宽度,如纵向-30和横向-40宽度。你能完成工作吗?甚至更短。
view.widthAnchor.constraint(equalTo:otherView.widthAnchor).isActive=true
:)