xcode size class vs actual iphone resolutions - ios

Something still not very clear. We know for example iphone 5s has a resolution of 320 x 568 points.
If on xocde, storyboard I use “wAny hAny” size class and place a VIEW(UIView) sized 500h x 500w and constrain it to the top, left, right and bottom of super view.
In there I can fit 100 50x50 points images, like 10 images per row.
Now if I run this app on the iphone 5 simulator I am not able to see 10 images in a row since the width is only 320?
Maybe iOS will resize the images so they can hit? What if I don’t want those images to be resized?
Can someone please help me with this or guide me to a document that explain this?

The superview will be resized, when the app runs, to fit the screen. So, since you have pinned the view to the top, left, right, and bottom of the superview, it will be resized as well.
It is up to you to take all this resizing into account when you design your interface and add your constraints. The whole point of constraints is to allow you to design in the face of all this resizing.
Thus, as for your 10 images, they will certainly not fit at fixed width 50, 10 to a row, when the app actually runs, because (as I just said) the superview is going to be resized. It is up to you to design the interface and the constraints so that they will fit.

Related

How to scale buttons for different devices

I have buttons on my screen I have attached constraints but on different sizes of screen I don't see my buttons right as I want to. There are the same size on different screens and I can't see all buttons on small screens
Here is a good version of it on iphone 8 plus in xcode
and then I change view to iphone se or any another iphone and I get these troubles:
What do I need to do to have the same screens?
Make the buttons have a width that is in proportion to the view
width.
Then add a 1:1 aspect ratio constant so that the buttons remain
square.
Then position the buttons in relation to the screen dimensions (not
absolutely).
This should get the job done for you.
Setting proportional width
Suppose the main view's dimension is 375 x 667 in interface builder. Suppose you place a 90 x 90 button on it. To make it always have a width in proportion to the main view's width,
Add an equal widths constraint to the superview for the button.
Edit the constraint's multiplier: Change it from 1 to (90/375)
Doing the above makes sure that for different screen widths, the button's width will also change in proportion.
Here are screen shots from iPhone XR and iPhone 5s, that I took from a sample I just did to try this out. As you can see, the buttons stay within the screen and maintain their shape. Hope this helps.
You don't seem to have defined the constraints of the button to superview. eg: left edge, top edge...
*edit: post complaint below #raisedeybrow
You will need to define (add constraints) how far from the edge (left and top, right and bottom) you want your button/s to be. Then apply content hugging and compression priorities on buttons for all of them to scale as you'd like. Some will need to be bigger, some you don't want to allow to shrink etc. Lots to set if you'd want it to look fluid

How to do this custom auto layout? [duplicate]

This question already has answers here:
Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5
(2 answers)
Closed 5 years ago.
I am trying to get this layout to work but have failed in these past two days trying.
All the labels have a constant height. All squares on the left are images of icons that are not important.
My problem is regarding the Y axis. This is a frame of an iPhone SE. I need it to expand accordingly to bigger screens like iPhone 8 Plus and iPhone X. Similarly, I could design it in Interface Builder for iPhone X and it should shrink reasonably until iPhone 4s. As far as my understanding goes, size classes don't work on iPhone - iPhone variation since they are all compact width and regular height.
These vertical lines highlighted in blue are not visible in the real app. They are an attempt to always center the black horizontal lines between two UILabel frames. So, what I really need is: if the screen gets smaller, the blue vertical lines should shrink. If the screen gets larger, the blue vertical lines should expand. The problem arises when I try to also make the UITableView have a variable height.
Anyone has any ideas to how I could solve this problem? It's very important to me.
There are many ways to achieve what you're asking for, but UIStackView is great for these kind of things. To answer your question in detail would take too long, but here's a screenshot of a quick mockup in interface builder, perhaps you can figure it out.
I didn't quite understand what you meant by those blue lines, in my example the tableview height is dynamic as its hugging priority is lower than other elements. But you could give desired height to each element and set stackview distribution property to "fill equally", then the stack view will fit your screen in y axis and will create equal spacing between elements.
and the simulator screenshot:
And here I gave each container view a static height and set the main stack view distribution to "equal spacing", as you can see it adjusts the spacing between the container views:
If you want them to be the same proportion depending on your screen, you can make them with multiplier:
Try make them the same height as your container view, but with multiplier 0.15, or something. If they equal, you need one constraint for 1:0.15 and all others to be equal to this one

Swift how to resize elements depending on screen size

I've got all my elements in the view and have added constraints to position them correctly on any device.
But when it's run on a 3.5 inch iPhone some of the bottom stuff is cut off and when it's run on a iPhone 6Plus there's a lot of extra space on the bottom.
How could I resize all the elements to look good on all devices?
This is a universal app to run on iPhone and iPad.
I guess you constraint an element as width equals 300...It's very bad for some elements that should fill (or fill percent of) the screen. I suggest you make percentage constraints. For example, make equal width to view, click Edit button and set Multipiler 0.6. It looks like this in storyboard:
make two constraints (trailing and leading for example) and make aspect ratio for your image, maybe this will help you.

Autolayout UIImageview Swift

So,
I am trying to build an app from design, the design consists of 3 images horizontally on a portrait layout
Supporting Iphone 5 and upwards.
From this guide http://iosdesign.ivomynttinen.com/ the Iphone 5 should have 640px of horizontal space.
Each image is 100px by 148px high, therefore I have created added 3x UIImageview and laid them out horizontally.
Now without any constraints I get the following
If I auto constrain I get the following, same if I add constraints manually
Skips out the middle.
If I go to any any constraint , I get an error message in the logger.
This can be a vertical space to top layout constraint, just the one and it breaks.
I assume as images are only 100px wide, I should have plenty of room.
On the UIImageview I have done the following
Set Height and Width
Content is AspectFit
Clip Subviews Selected
Auto re-size selected
I have either not understood App design and the images do not fit, or have done some incorrectly with IB and UIImageview?
Any thoughts on this.
Sorry not a lot of code as it's all Interface builder.
Thanks
Sorry,
My own fault, "reset to auto constraints" and "clear constraints" had a bit of an issue, there was a bunch of constraints still kicking about when I delved in deeper.
In the end deleted then all and then manually added.

Using Auto Layout in Interface Builder with complex views

So, here's a tricky one. I'm working on a portrait only app that should be compatible with the iPhone 4s, 5/5s/5c and 6/6 Plus. Unfortunately, I'm having some troubles working with Auto Layout in order to make the user interface fully compatible with the previous listed devices.
Here's a screenshot of how the UI should look like (this is from an iPhone 6 Plus that I'm using as a reference for the other screen sizes):
I have a 338x338 points blueCircle, a 180x180 redCircle, a 118x118 greenCircle, a 84x84 purpleCircle and, finally, a 50 points tall adBanner. The result I would like to achieve is to have an iPhone 6 Plus UI that respects these view sizes and a properly shrunk down UI for all the other ones; in addition to that I will let the users pay to remove the advertisement banner on the bottom so I need to resize the UI accordingly too (set circles vertical spacing to the banner and set its height to 0 maybe?). I messed around with Interface Builder and I came out with a decent - not so precise - result. Take a look at the UI on an iPhone 4s screen:
It looks decent but there's major problem. In Interface Builder I'm working on a Freeform ViewController with the same width of an iPhone 6 Plus (414 points) on which I've added all those circle views with the previously listed frame sizes. To make an example, let's temporarily ignore all the other constraints to concentrate on the aspect ratio ones: on the 338x338 blueCircle I've added an aspect ratio constraint, the same on the 180x180 redCircle. Then I've control-dragged from the redCircle to the blueCircle and clicked again on aspect ratio so that it resizes accordingly to the blueCircle. As I said the result wasn't precise since, if I println() redCircle's frame, the console will say that its width equals to 175.666666666667 (instead of 180) points.
Here's the project so you can check it out yourself. I believe its worth more than a thousand words. I'm not very good at Auto Layout and I'm sure I'm creating too many useless constraints. What could I do to improve my layout?
you need to set height and width of all the circle views as proportional to its parent view.
I set the height of blue view as 0.5 proportional to its parent view and its width will be in aspect ration with itself as 1:1
repeat same for all the other views and apply vertical spacing and horizontal spacing as required between views
this is how it looks on iphone 4
this is how it looks on iphone 6 plus

Resources