Search code examples
swiftswift3constraints

Right upper constraint format


I am struggling with adding one button from code to the right corner of the view. Could someone explain me how can i do this without setting the left constraint ? This would be the left upper V:|-10-[v0], H:|-10-[v0] what would be inversion of it ? I was trying with this : V:[v0]-10-|, H:[v0]-0-| but it does not work like i thought

Thanks in advance!


Solution

  • Based on the comments, I'd like to provide two answers on how to place a UIButton on the top right.

    VFL:

    Your vertical pin is for the bottom right, not the top. Instead of V:[v0]-10-|, where the "pipe" character (that designates the bounds of the screen) is at the end, place it at the beginning - |-10-[v0].

    Provided you've given the button some sort of height/width - which I think you have as the code for "top left" works - this should fix things.

    Anchors

    Introduced in iOS9, layout anchors (along with layout guides) are a third way to code auto layout. Like NSLayoutConstraints, this is less "visual" than VFL. But unlike NSLayoutConstraints it's less verbose - thus more "Swiftier" IMHO.

    To pin a UIButton to the top left, you still need to give auto layout four things - height, width, and X/Y positions. In the following I'm assuming the superview of v0 is called view, like the root view of a UIViewController.

    // declare your button
    let v0 = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // remember, you ALWAYS need to turn of the auto resize mask!
        v0.translatesAutoresizingMaskIntoConstraints = false
    
        view.addSubview(v0)
    
        // create a square button
        v0.widthAnchor.constraint(equalToConstant: 100.0).isActive = true        
        v0.heightAnchor.constraint(equalToConstant: 100.0).isActive = true
    
        // pin the button 10 points from the left side of the view
        v0.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true        
    
        // here's how you would pin the button 10 points from the right side of the view
        // note the use of a negative here!
        // v0.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true        
    
        // pin the button 10 points from the top of the view
        v0.topAnchor.constraint(equalTo: view.topAnchor, constant: 10).isActive = true
    
    }
    

    Like NSLayoutConstraints, layout anchors have constants and multipliers, which you may change if you declare a name for the constraint.

    You may combine NSLayoutConstraints with NSLayoutGuides for some nice "adaptive layouts". These guides act like "spacer/invisible" UIViews except for the overhead - they aren't views. You can get a set of Apple "standard" margins (UIView.layoutMarginsGuide), or you can create a set of equally size dynamic guides to space things out equally.

    Here's two blogs about layout anchors and layout guides. The examples are written in Swift 2 but there's no syntax changes for Swift 3.