简体   繁体   English

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

[英]How to create Auto layout equal width constraint programmatically in ios swift?

How to give programmatically constraints equal width and equal height with multiple views.I check google but not perfect answer for programmatically equal width and height constraints through auto layout.如何在多个视图中以编程方式给出等宽和等高的约束。我检查了谷歌,但不是通过自动布局以编程方式等宽和高度约束的完美答案。

my code look like below:我的代码如下所示:

  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])
    }
}

I want to add equal width constraint programmatically.我想以编程方式添加等宽约束。

You have three possible ways to do that:你有三种可能的方法来做到这一点:

1) By using anchors: 1)通过使用锚点:

view.widthAnchor.constraint(equalTo: otherView.widthAnchor, multiplier: 1.0).isActive = true

2) Visual format: 2) 视觉格式:

simple example - "H:|[otherView]-[view(==otherView)]|"简单的例子 - "H:|[otherView]-[view(==otherView)]|"

3) "Old school" constraints: 3)“老派”约束:

NSLayoutConstraint(item: #view, attribute: .width, relatedBy: .equal, toItem: #otherView, attribute: .width, multiplier: 1.0, constant: 0.0).isActive = true

hope it will help somebody.希望它会帮助某人。

Try this:试试这个:

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

multiplier: 0.25 , denotes that customView 's width will be 1/4th of the parent view, viewForRow1 . multiplier: 0.25 ,表示customView的宽度将是父视图viewForRow1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ios auto-layout:以编程方式设置宽度约束 - ios auto-layout: Programmatically set width constraint 如何在iOS自动布局中为按钮添加等间距和等宽 - How to add Equal Spacing and equal width for button in iOS Auto layout 如何在 iOS 中以编程方式从 UIButton 中删除等宽约束 - How to remove Equal Width constraint from UIButton Programmatically in iOS 如何通过自动布局以编程方式快速设置旋转时UICollectionView的宽度 - How to set width of UICollectionView on rotation with auto layout programmatically swift 使用自动布局约束以编程方式创建四个具有相同高度和宽度的UIView - Using auto layout constraints programmatically create four UIView all with equal height and width iOS 8自动版式中的全角错误约束(适用于iOS 7) - Full width bug constraint in auto layout for ios 8 (works in ios 7) 如何在Swift中以编程方式更改布局约束? - How to change layout constraint programmatically in Swift? 如何使用 Swift IOS 以编程方式创建布局和约束 - How to programmatically create layout and constraints using Swift IOS iOS自动布局:将尾随空格设置为等于超视图的宽度 - iOS Auto Layout: Set the trailing space equal to the width of the superview 以编程方式访问自动布局约束 - Access Auto Layout Constraint Programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM