Auto Layout Setup - ios

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)

Related

how to set autolayout for different devices by using storyboard

I have an Iceland map which contains 9 different area buttons. Every button has a different size. I want the map can show the same on different devices. I only know to set the central button with horizontally in container and vertically in container. However, other buttons, no matter how I set the constraints, they will be a mess on iphone SE or iPhone Plus. (I use iphone 8 as normal)
Can anyone teach me how to set the constraints for the 8 left buttons? thank you!
The best thing for this is a vertical UIStackView nested with 3 horizontal stacks
MainStackView constraints
centered vertically & horizentally , width & height propotional to screen
then drop 3 stackViews inside it set axis = horizontal and drop 3 Buttons for every inner stack
Note: distribution is fillEqually for all the stacks , spacing = 10
look to this Demo
Something I've done in the past is used a single control as the anchor for all the others.
Basically this means taking the centre button and anchoring it to the middle of the container view (centre horizontally and vertically)
I then constrain the middle/top and bottom controls to it (vertical space and horizontally centred)
From there each control in the row is then constrained against these elements (horizontal spacing and vertically centred)
You might also consider constraining the sizes to the centre control as well, so all the controls share the same size. For me, this means constraining the width of the control to a set value and then applying an aspect ratio to the height. I do this because then I can change the value of the width constraint and ALL the control will change size
Because iPhone 5s, iPhone, iPhone+, iPhone X all share the same "sizing classes", it makes it some what more difficult (as you can't apply traits - but you can do it for iPad and iPhone ;)).
At this point, I would bind the centre controls width constraint (and the height if you need it) to the source code and when the view is loaded, determine the device screen size and make minor adjustments to the constraints values.
If you would prefer a complete "storyboard" solution, you could constraint the width of the centre control to the super view and apply a modifier
Using the "device" templates from the storyboard, layout one a iPhone 5s and iPad Pro
The long and short of it is, you have "options". You might even consider using UIStackView to define the rows, personally I find it easier to constraint the controls to a central anchor point - but that's my experience and your needs might differ

issue balancing constraints in Interface Builder when using ratio

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).

Universal layout constraints

I have an app which I've designed in the width:any height:any universal size template in XCode (Swift). Basically, I have a custom cell, with its own uitableviewcell class too, and two labels inside of it which display array data. Anyway, I am struggling to add the correct restraints etc, so that the labels and text inside them display properly on all iOS devices. Even though I have changed the font to be able to go to 0.2 scale, there's still sometimes overlap or text clipped in the label on smaller iOS screens. It's probably a simple thing to resolve. So can anyone help me make this layout look the same, or the equivalent, on all iOS devices and all orientations too please? Here is my design:
The fact that your labels are overlapping each other is probably that you don't have the proper width constraints.
This is what I will do, taking the left label as an example:
add constraints to the left, top, bottom margin
add a width constraint to the parent view and making it 50% of the parent width
You will need to do the similar for the right label replacing the left margin constraint with a right margin one.
Now both the labels only take 50% of the width, then they will not overlap any more guaranteed.

How to control constraint over multi devices in iOS/Storyboard?

I've been working on a project that supports multiple screen sizes for iOS. Thanks to Storyboard and auto layout technology, I could be able to handle some constraint automatically based on device screen size. But in some cases, I couldn't figure out how to deal with auto constraint when I apply constant constraint in Storyboard. For example, lets say I put a button in view controller on Storyboard and set constraints (Tailing/Leading space as 44 and Top space as 50). This button surely expand its width based on device screen size but I also want to change top space proportional to its screen size. Assume base top space 50 is set for iPhone 4 screen size (320/480), then I would like to increase it as device screen height increases. The way I manage this for now is, check screen size and change its constraint constant respectively which is 50 * (568/480) (i.e. I want to keep same ratio). I wonder if there is a way to do this trick on storyboard. I would appreciate any advices.

Xcode - Adding constraints to UITableView so that it fits all screen sizes

I've read quite a few tutorials and watched numerous videos on using constraints in Xcode. For some reason, I'm still missing certain aspects that are necessary to make the user interface look the way it's supposed to for all screen sizes.
Currently, I have a UITableView laying on a view controller. The view controller is set to "Inferred" size and I have all of the different sized devices open in Assistant Editor Preview to the right so I can view the changes. I've encountered multiple problems attempting to get the constraints correct for the different screen sizes.
Problem 1: The UITableView has a width set to 600. This causes dead space to the right of the table view on the iPad preview and it causes the UITableView to extend too far on the smaller devices. If I make the width of the table view smaller so that it fits within the preview of all screen sizes and then pin the left and right edges of the table view to the edge of the Superview by specifying 0 and unchecking constrain to margins, the result I'm seeing in preview is that the entire table view disappears completely from each device size. I was surprised by this because I thought by pinning the table view to the margins, it would make the table view fit within each of the screens.
Problem 2: (This is a completely different scenario from Problem 1, above.) In this situation, I've left the size of the table view to 600 and just specified constraints for the internal components of the cell contained within the tableview. In this case, the table view is still slightly not wide enough for the iPad dimensions and it extends too far on the smaller devices which makes components in the cells to be truncated and off of center.
I've primarily been working in Storyboard with Any width, Any height set. However, if I change the setting to Compact width, Any height, I can alter the constraints to fit the smaller devices a little better, but there's still a problem with getting things to work between the 5.5 inch screen and the smaller devices.
I would like to get my UITableView to extend all the way to the edges on each device and I would like to have the view inside of the cell remain centered and keep it's relative size on each of the devices. Does anyone have any suggestions on how I can accomplish this?
if you want to make your tableview to "fill" the whole screen (device-independent) the only thing you have to do is to pin its 4 edges (top, left, bottom, right) to its superview (the viewcontrollers view in your case) with a constant of 0.
you do not specify a specific width (like in your case 600) or height.
good luck :)

Resources