George Fedoseev
George Fedoseev

Reputation: 102

iOS - Use same constraints for different iPad sizes, just scaling the view?

I have an app for both 12-inch iPad Pro and iPad Air. They have same aspect ratio. But when using auto-layout, constraints of same value make app look different on 2 devices because they are tied to point size of screen.

Is it possible to have some kind of reference screen coordinate system for constraints or maybe other approach to achieve visuals just being scaled on different screen sizes of same aspect ratio?

Upvotes: 0

Views: 1052

Answers (1)

Aleksandr Honcharov
Aleksandr Honcharov

Reputation: 2513

Yes, you can use multiplier property to set constraints relative to the screen size or whatever you choose. For example, if you set it to 0.3, it means 30% of what you constrain to. For example, width of your custom view will be 30% of width of superview.

enter image description here

Also, you can use UIStackView. It takes care of resizing views inside appropriately. And Apple recommends using it all the time wherever possible.

Example of left 10% margin.

        let leftMarginGuide = UILayoutGuide()
        view.addLayoutGuide(leftMarginGuide)

        NSLayoutConstraint.activate([
            leftMarginGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            leftMarginGuide.topAnchor.constraint(equalTo: view.topAnchor),
            leftMarginGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            leftMarginGuide.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.1)
            ])

Upvotes: 3

Related Questions