Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative positioning using auto layout and SnapKit

I have a label which I want to have the same relative position on the screen, no matter what device is used. E.g. the label is positioned 10% off from the views top margin and 30% off from the views left margin.

A constant will always do the positioning e.g. 150 px off from the views margin and will therefore be greater for devices with a small resolution, while devices with a bigger resolution will only have a smaller distance...

Is there a way to realize this programmatically e.g. with the help of SnapKit?

My code currently looks like this:

import UIKit
import SnapKit


class worldViewController: UIViewController {
      lazy var correctFieldNew = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

            self.view.addSubview(correctFieldNew)
            correctFieldNew.backgroundColor = UIColor.blueColor()

            correctFieldNew.snp_makeConstraints { (make) -> Void in
                make.size.equalTo(CGSizeMake(90, 30))
            }
    }
}

I feel like I have to use the multiplier here, but the label does not move an inch when I write something like:

make.top.equalTo(self.view).multipliedBy(0.1) 
like image 954
Jake2Finn Avatar asked Sep 06 '16 12:09

Jake2Finn


Video Answer


1 Answers

The correct way is to use:

make.top.equalTo(self.view.snp_bottom).multipliedBy(0.1) 
like image 173
Grzegorz Krukowski Avatar answered Nov 15 '22 04:11

Grzegorz Krukowski