I tried stackView as a newly swift dev, they are really convenient for layout related issue but, I still have a strange behavior on the iPhone 6 plus screen (see picture below)
3 image are in a stackView
StackView is in a cell, who is in a tableview obviously.
StackView constraint are set to be top,left,right 0 to the cell and 80% of the cell height
here is the result :
Result on iPhone 6:
Result on iPhone 6 plus
EDIT:
the miss-sized view happen only on iPhone 6 plus and not on iPhone 6S plus
with a constant of +14, in my case, on the cell percentage, the issue disappear (but size are no more in my design guideline)
thanks
Have you tried constraining the three images to have equal heights?
Set equal widths across images or set content hugging and content compression priorities for images so it knows how to size the stack. In my opinion the only way here is to make them equal width.
Otherwise you will always have some sort of constraint resolution based on hugging/compression values which even if the same will give ambiguous layout or if not the same and not ambiguous, will produce difference in sizes in certain cases. Equal width on the other hand should guarantee the width.
Related
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
I am having a problem with my constraints for an alert view I am building in interface builder.
When the screen is an iPhone 5 size I want the leading and trailing constraints to compress the images and make is smaller, I have a 1:1 ratio constraint set here too. This works great so far. However when the screen gets bigger e.g. iPhone7+ or iPad I want the image to remain the same size (that is the size of the image in interface builder, the iPhone7 size canvas). I have tried setting a constraint on the width of the image using <= and then setting the leading and trailing constraints on the image to priority 750. This works for the bigger screens but now when I go back to the iPhone5 screen size it is broken because the priority on the leading and trailing constraints is less than that of the ratio. The image size remains unchanged. Seems no matter how I work the constraints I can only get the size working for a single scenario iPhone5 or iPhone6+. Has anyone had this kind of issue before? Any suggestions on how I can work the constraints??
Try exploring on size classes. Explore on this link. https://www.raywenderlich.com/162311/adaptive-layout-tutorial-ios-11-getting-started.
Basically by using this you can set separate constraints to a same element with respect to size classes(which in term refers to different sizes of device screens).
You can implement it this way. It works well for iPhones of all sizes.
There are 3 key points regarding the UIView containing the image
Set leading and trailing constraint as greater than or equal (>=)
Center the view horizontally in the superview. This constraint combined with the previous ones will result in the view staying centered even when the image won't be able to stretch anymore.
Set content hugging priority to required (1000).
I've got the below layout and I've got it working well on iPhones, landscape only.
The PickerViews are currently populated with an image array. Currently, I force resize the images in the PickerViews based upon device size by setting the PickerView row height and image height to what fits each specific device (iPhone 5 -7+). However, to work on an ipad, I need to expand the ImageView in the center.
When I change the hugging values to >= it allows it to grow too large and pushes the PickerViews out the side on iPhones, despite them having a 4nit margin to superview. I've tried playing with the hugging/compression priorities but I can't make any headway.
Each item is in its own stack view. What is the best way to go about this?
Layout Image
Did you add leading and trailing constraints to ImageView? As I understand you set the sizes to pickerViews?
Anyway, could you tell all constraints you have added? And how your elements should be resized on different devices(how can be compressed if there is not enough space)
I wonder if there is any way to only target iphone 6 plus and up when it comes to size classes and constraints.
Looking at this:
I can't see it.
I have a UIview with a spacing of 20pt on all sides. The UIView works as a container for a search form. The UIView appears the same on all devices. But when it comes to Iphone 6 and up, the View appears to have some dead space in the bottom of the view. What I would like to do is to have a even space between all form items so that they take up the whole view.
So is there any way to target iphone 6 and up? Eg making buttons bigger / adding more space between items
One way to do this is to constraint the height of the buttons/fields to this UIView, so they always take for example 1/10 of its height. On the storyboard right-click drag from a control to its superview and select Height to create a constraint. Find this constraint and change it's multiplier from 1 to for example 1/10. This is the way I did it and I found it way less time consuming than playing around with freeform sizes and other size classes.
The auto layout "picker" looks like the image bellow:
But what if I only want to target iphone 6 plus and Ipad?
I use the constraints compact width | any height in order to only target 3.5 - 4.7" displays.
But what should I use in order to only target iphone 6 plus / ipad? - The any width | any height do target them but would that not also overwrite the rules set in compact width | any height ?
What I want to do is to increase spacing between three labels when the app runs on a iphone 6 plus or ipad. Also to note, my app only runs in portrait mode if that matters.
thanks in advance,
You shouldn't categorize autolayout via device, but rather by class sizes. This was a HUGE emphasis this year at WWDC due to the new iOS 9 iPad multitasking feature discussed at the keynote.
I recommend trying the following idea for your layout. Note this is just a visual interpretation of what you need to implement in IB.
All three labels have equal width constraint
|---[first label, maxwidth = x]--[second label]--[third label]---|
This will make all three labels have the same width and be evenly spaced across. This approach helps with all class sizes of compact width | compact height, etc. If you need help finding a way to do even spacing between, trying adding filler views:
|[uiview][first label, maxwidth = x][uiview][second label][uiview][third label][uiview]|
Each [uiview] in this case will have the same width as well with a background color of clear this way the margining between the labels will be even and these views aren't "seen" but help with autolayout.
If you need help with this auto layout implementation please let me know in the comments.