Answers for "swift create constraints programmatically"

1

swift ways to setup constraints

//https://stackoverflow.com/a/26181982/13171606
//There are multiple ways to add contraints, please open Link for almost ecery type of constraint

override func viewDidLoad() {
    let newView = UIView()
    newView.backgroundColor = UIColor.red
  
    view.addSubview(newView)
    
    newView.translatesAutoresizingMaskIntoConstraints = false//compulsory
    newView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    newView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    newView.widthAnchor.constraint(equalToConstant: 100).isActive = true
    newView.heightAnchor.constraint(equalToConstant: 100).isActive = true
}
Posted by: Guest on February-13-2021
1

swift edit constraint programmatically

yourHeightConstraintOutlet.constant = someValue
yourView.layoutIfNeeded()
Posted by: Guest on March-04-2020

Code answers related to "swift create constraints programmatically"

Code answers related to "Swift"

Browse Popular Code Answers by Language