Autolayout to Keep View Sizes Proportional

AutoLayout to keep view sizes proportional

This can be resolved by adding one more dummy view(dummyView) with its constraints set to Fixed width, height and aligned to centerX of Superview.Then add left view and right view Horizontal spacing constraint to dummyView.

Sample Image Sample Image

iOS - proportional spacing with autolayout

Note - this is kind of a sketchy way of doing it.

I had a similar issue and resolved it by adding dummy views between my objects to represent the spacing between them. You can constrain the dummy views to scale proportionally to the rest of your views, which will make the spacing between your objects scale properly with the overall size. I set the hidden property of my dummy views so they wouldn't show up (note - they still get laid out properly when they are hidden).

Hope that helps.

Edit:

This method is imperfect (you have to clutter IB with extraneous views), but like @sha says it seems to be the only way to get it done.
It turns out that other people have been giving similar advice. I came across these references which might be helpful:

AutoLayout to keep view sizes proportional

AutoLayout: layout consistency with proportional element spacing with 3.5" and 4" screens

Keeping buttons distance proportional to each other while scaling buttons

Constrain your second view's horizontal center and vertical center to the first view's, the constant you set here does not matter but I suggest setting it to how you expect the views to be spaced just so it looks correct in your Storyboard. Create an outlet to both of those constraints in your view controller. In your controller's viewDidLayoutSubviews() update each constraint's constant to it's matching axis(x = width, y = height).

@IBOutlet weak var secondViewCenterXConstraint: NSLayoutConstraint!
@IBOutlet weak var secondViewCenterYConstraint: NSLayoutConstraint!

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
secondViewCenterXConstraint.constant = secondView.frame.size.width
secondViewCenterYConstraint.constant = secondView.frame.size.height
}

centerXConstraint
centerYConstraint
iphone
ipad

You'll see in my screen shots I have the second view's top left corner matching the bottom right corner of the first view. If you want to space them more similar to what you have in your screen shot then you could use something like below. I'm just taking a guess on the calculations, you'll have to edit them according to the spacing you want to accomplish. But all calculations you make for these constants should be based on some kind of multiplier of the view's width and height.

secondViewCenterXConstraint.constant = secondView.frame.size.width * 0.9
secondViewCenterYConstraint.constant = secondView.frame.size.height * 0.5

I created a repo to better show my example.

How to use autolayout to scale view for all screen sizes?

I've done some tinkering and I believe I've found a solution. I will demonstrate how to do it for the two larger squares roughly in the middle of your view.

Here is a reference image:

Sample Image

First, you need to create a container (my blue view) that spans the entire width of the superview, and has a height the same or larger than your inner squares (orange).

This view needs the following constraints: Align Center X to Superview, Align Center Y to Superview (OR constraints to give it the Y position you desire), Height Equals: [square height], Trailing Space to Superview, Leading Space to Superview.

The inner orange squares must be placed inside the container, and must be given the following constraints: Align Center X to Superview, Leading Space to Superview (value = space between square and edge of superview), Trailing Space to [other view] (value = space between squares). Select the two squares together, and add Aspect Ratio, Equal Width and Equal Height constraints.

The items in bold depend on which square you're selecting. The left one should have leading space to superview and trailing space to the other view, and the right one should have these constraints swapped.

This should give you your desired effect with autolayout.

iOS - Autolayout - Increase/Decrease view size proportionally on orientation change

The reason your views go away when you add constraints is because a UIView has no intrinsic content size, so its size is {0,0}. The view appeared when you didn't add constraints because the system adds constraints for you, if you don't add them yourself; the system added ones are top, left, width, and height. So, you need to set the size of the views somehow. You can give them explicit size constraints, you can pin them to the edges of the superview, you can give them relative heights based on other views, etc.

Since you want the 2 views to get proportionally smaller in landscape, you should give them heights that are relative to the superview. You do this by selecting the view and the superview, and choosing "Equal Heights" from the pin menu. Edit that constraint to change the multiplier to something like 0.25 for the blue view and 0.2 for the orange one (this assumes that orange or blue view are the first item in the constraint -- if they are the second, then you should use the inverse values of 4 and 5). You should also do the same for the widths, since it seems you want them to get proportionally smaller too.

AutoLayout to adjust image size proportional to 50%

I can see you have missing constraints on your scene, so that could be an issue.

I would put the UIImageViews in a container view pinned to your main view, making sure there are no missing constraints. The benefit of this is in debugging - you can give it a different BG colour and make sure it's resizing itself properly on the simulator/device. I found that putting the image views inside this helps enormously.

Then you need to specify Aspect Ratio constraints on your image views, and also make them equal widths and heights. Add some small constant horizontal spacing between your image views and top/leading/trailing to the container view, and let auto-layout decide the optimal image sizes.

I've included a screenshot of my storyboard:-

Sample Image

This renders like so:-

Sample Image

Auto Layout proportionally Scaling Views

Try proportional height and width. You need to make constraints for Equal Heights and/or equal Widths and then you double click on the constraint and change the multiplier for example 0.2 if you wish to make First Item to be 20% of second item in width and 2.0 for 200% (means double) in width. to make it increase or decrease proportionally according to your requirementSample Image

Proportional distance to top in autolayout and device rotation

Use the multiplier of a constraint, accessible by selecting the constraint in the XIB.

Xcode autolayout constraint landscape orientation issue where view shrinks to zero size

Different devices have different size classes.

For example, an iPhone 13 Pro Max uses:

wC hR   // portrait orientation
wR hC // landscape orientation

whereas an iPhone 8 uses:

wC hR   // portrait orientation
wC hC // landscape orientation

Note that landscape is wC hC, not wR hC.

Change your "landscape" variation to Width: Any Height: Compact

Sample Image

See the Apple docs here for a list of devices and size classes.



Related Topics



Leave a reply



Submit