4

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) 
Zoe
  • 23,712
  • 16
  • 99
  • 132
Jake2Finn
  • 446
  • 3
  • 15

1 Answers1

7

The correct way is to use:

make.top.equalTo(self.view.snp_bottom).multipliedBy(0.1) 
Grzegorz Krukowski
  • 14,128
  • 5
  • 41
  • 61