Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-Layout fit to parent via code in Swift

I have a view i'm creating via code and adding to another view as subview. The new superview can change it's frame over time and I want the newly created subview to change it's frame accordingly. How can I do that using Auto-Layout via code in Swift?

like image 817
YogevSitton Avatar asked Jan 08 '15 10:01

YogevSitton


People also ask

What is auto layout constraints in Swift?

Auto Layout constraints allow us to create views that dynamically adjust to different size classes and positions. The constraints will make sure that your views adjust to any size changes without having to manually update frames or positions.

What is translatesAutoresizingMaskIntoConstraints?

translatesAutoresizingMaskIntoConstraints. A Boolean value that determines whether the view's autoresizing mask is translated into Auto Layout constraints.


2 Answers

Here is an example:

let view = UIView() // existing view

let subview = UIView()
subview.setTranslatesAutoresizingMaskIntoConstraints(false)

view.addSubview(subview)
view.addConstraint(NSLayoutConstraint(item: subview, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: subview, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: view, attribute: .Bottom, relatedBy: .Equal, toItem: subview, attribute: .Bottom, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: view, attribute: .Trailing, relatedBy: .Equal, toItem: subview, attribute: .Trailing, multiplier: 1.0, constant: 0.0))
like image 58
Kirsteins Avatar answered Nov 15 '22 08:11

Kirsteins


iOS 13, swift 5

First, you add this code

    subview.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(subview)

Then, there are two ways of doing this in newer versions of iOS.

  1. With NSLayoutConstraint class

    NSLayoutConstraint(item: subview, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: subview, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: subview, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: subview, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1, constant: 0).isActive = true
    
  2. With NSLayoutAnchor class (less verbose)

    subview.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    subview.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    subview.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    subview.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    

Either way, on iOS 8 and later Apple recommends using isActive() instead of adding constraints directly to a view.

Additionally, I believe the purpose of the NSLayoutAnchor method is to be more concise and readable compared to NSLayoutConstraint.

like image 41
Merricat Avatar answered Nov 15 '22 08:11

Merricat