Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

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
}

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 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

swift constraints


 - firstView.coverWholeSuperview()
 - firstView.constraints(size: CGSize(width: 44, height: 44), centerX: view.centerXAnchor, centerY: view.centerXAnchor)
 - firstView.constraints(top: view.topAnchor, 
                         leading: secondView.leadingAnchor, 
                         bottom: view.bottomAnchor, 
                         trailing: secondView.trailingAnchor, 
                         padding: UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12))

Comment

Type Constraints Swift

//create a generic function with type constraint
func addition<T: Numeric>(num1: T, num2: T) {

  print("Sum:", num1 + num2)
}

// pass Int value
addition(num1: 5, num2: 10)

// pass Double value
addition(num1: 5.5, num2: 10.8)
Comment

PREVIOUS NEXT
Code Example
Swift :: swiftui foreach 
Swift :: replace character in swift 
Swift :: swift thread.sleep 
Swift :: power number in swift13 
Swift :: costume font size swift ui 
Swift :: deselect cell swift 
Swift :: how to change background color of ui button swift 
Swift :: Thread 1: breakpoint 1.1 
Swift :: tuple swift 
Swift :: swift reload tableviewcell at index 
Swift :: navigationbar large title swift 
Swift :: custom tab bar swift ios 
Swift :: iOS & Swift - The Complete iOS App Development Bootcamp 
Swift :: for loop swift 
Swift :: swift5 get uiview height 
Swift :: change ui button swift 
Swift :: swift create an empty array 
Swift :: swift paged scrollview get current page 
Swift :: uitableview bottom inset 
Swift :: make text autoresize swiftui 
Swift :: swift uilabel font bold 
Swift :: Swift How to declare an optional in Swift? 
Swift :: Swift Add Two Numbers 
Swift :: Swift Initializer 
Swift :: swift weekday component values 
Swift :: xcode button center text 
Swift :: customize change color size MDCActivityIndicator swift 
Swift :: Memberwise Initializer for structs Swift 
Swift :: swift uitextfield only numbers keyboard lock programmatically 
Swift :: Example: Nested Tuple 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =