Swift Add Programmatically Constraint To View

[Solved] Swift Add Programmatically Constraint To View | Swift - Code Explorer | yomemimo.com
Question : swift add programmatically constraint to view

Answered by : rens-wijnmalen

// Swift3+
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([ imageView.widthAnchor.constraint(equalToConstant: 16), imageView.heightAnchor.constraint(equalToConstant: 16),
])
self.addSubview(self.imageView)

Source : | Last Update : Fri, 04 Mar 22

Question : swift constraints programmatically

Answered by : happy-hyena-j4t6squdpbx2

fileprivate func setupName() { lblName.text = "Hello world" // Step 1 lblName.translatesAutoresizingMaskIntoConstraints = false //Step 2 self.view.addSubview(lblName) //Step 3 NSLayoutConstraint.activate([ lblName.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), lblName.centerYAnchor.constraint(equalTo: self.view.centerYAnchor) ])
}

Source : https://stackoverflow.com/questions/26180822/how-to-add-constraints-programmatically-using-swift | Last Update : Fri, 11 Jun 21

Question : swift edit constraint programmatically

Answered by : mobile-star

yourHeightConstraintOutlet.constant = someValue
yourView.layoutIfNeeded()

Source : | Last Update : Wed, 11 Mar 20

Question : swift constraints programmatically

Answered by : happy-hyena-j4t6squdpbx2

import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myImageView:UIImageView = UIImageView()
myImageView.backgroundColor = UIColor.red
myImageView.image = UIImage(named:"sample_dog")!
myImageView.translatesAutoresizingMaskIntoConstraints = false
myImageView.layer.borderColor = UIColor.red.cgColor
myImageView.layer.borderWidth = 10
self.view.addSubview(myImageView)
view.removeConstraints(view.constraints)
view.addConstraint(NSLayoutConstraint(
item: myImageView,
attribute: .top,
relatedBy: .equal,
toItem: view,
attribute: .top,
multiplier: 1,
constant:100)
)
view.addConstraint(NSLayoutConstraint(
item: myImageView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant:0)
)
view.addConstraint(NSLayoutConstraint(
item: myImageView,
attribute: .height,
relatedBy: .equal,
toItem: view,
attribute: .width,
multiplier: 0.5,
constant:40))
view.addConstraint(NSLayoutConstraint(
item: myImageView,
attribute: .width,
relatedBy: .equal,
toItem: view,
attribute: .width,
multiplier: 0.5,
constant:40))
}
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning()
}
}

Source : https://stackoverflow.com/questions/26180822/how-to-add-constraints-programmatically-using-swift | Last Update : Fri, 11 Jun 21

Question : swift constraints programmatically

Answered by : happy-hyena-j4t6squdpbx2

 /*
// regular use
1.leftAnchor
2.rightAnchor
3.topAnchor
// intermediate use
4.widthAnchor
5.heightAnchor
6.bottomAnchor
7.centerXAnchor
8.centerYAnchor
// rare use
9.leadingAnchor
10.trailingAnchor
etc. (note: very project to project)
*/

Source : https://stackoverflow.com/questions/26180822/how-to-add-constraints-programmatically-using-swift | Last Update : Fri, 11 Jun 21

Question : constraint programmatically swift

Answered by : real-rat-z9a5ieeys1c4

view.translatesAutoresizingMaskIntoConstraints = false let attributes: [NSLayoutAttribute] = [.top, .bottom, .right, .left] NSLayoutConstraint.activate(attributes.map { NSLayoutConstraint(item: view, attribute: $0, relatedBy: .equal, toItem: view.superview, attribute: $0, multiplier: 1, constant: 0) })

Source : https://stackoverflow.com/questions/26180822/how-to-add-constraints-programmatically-using-swift/26181982#26181982 | Last Update : Sun, 18 Sep 22

Answers related to swift add programmatically constraint to view

Code Explorer Popular Question For Swift