Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

swift add programmatically constraint to view

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

swift constraints programmatically

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

swift edit constraint programmatically

yourHeightConstraintOutlet.constant = someValue
yourView.layoutIfNeeded()
Comment

swift constraints programmatically

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()
}
}
Comment

swift constraints programmatically

 /*
// 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)
*/
Comment

PREVIOUS NEXT
Code Example
Swift :: swift sort array 
Swift :: swift map array to dictionary 
Swift :: swift guard let 
Swift :: how to set the center in view in swift 
Swift :: swiftui create search bar 
Swift :: how to low case string swift 
Swift :: check enumatted arrray last item swift 
Swift :: swift create custom button programmatically 
Swift :: swift get slected row data in tableview cell 
Swift :: change ui button swift 
Swift :: switch button swift 5 
Swift :: make optional protocol swift 
Swift :: Swift Calling a function in Swift 
Swift :: how to set return type swift 
Swift :: swift create an empty dictionary 
Swift :: swiftui hidden 
Swift :: string to array swift 
Swift :: Swift How to declare an optional in Swift? 
Swift :: swift session.input 
Swift :: Working of Recursion in Swift 
Swift :: get absolution position of view in swift 
Swift :: swift check if array has duplicates 
Swift :: swift hmac256 
Swift :: star score rating swiftui 
Swift :: swift constraints 
Swift :: swift 5 full screen image viewer 
Swift :: AMAZONCONNECT 
Swift :: Swift Logical Operators 
Swift :: allowed filename characters swift 
Ruby :: embedded ruby tag 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =