Xcode Constraining UILabel vertically over a dynamic UIImageView - ios

I am trying to constrain a label that is sitting on top a UIImageView. The ImageView keeps its aspect ratio for the different screen sizes so its length and width change according to the device.
I am running into an issue if I constrain the top of the label to the top of the image view at (for example) a constant of 58. For the lower resolution image that places the label where I want it visually. However on the higher resolution images that position is not where I want it visually. I have also tried adjusting the top constraint so that the constant is 0 and use the multiplier to adjust the position of the label. This however does not fix the problem and the label ends up at different locations on the image.
I really would not like to have to edit these constraints programmatically as I will have way more labels on different view controllers that would be a pain to program. Really hoping I can achieve this in IB.

I'm confused... why aren't you pinning a constant distance from the top of the label to the bottom of the image view?

Related

Align Image View without stopping a stackView auto-resizing

I am currently facing an issue where I have a horizontal stack view with a UILabel and a ImageView. I am needing the stackView to dynamically change height depending on how many lines of text there is. I am also wanting the imageView to be aligned with the bottom of the UILabel(even when there is a second line).
I have currently tried:
Adding a UIView and aligning the ImageView in it. This stops the stackView auto-resizing.
Changing the Content Mode to bottom doesn't work either as I am wanting aspect fit.
Thanks for any suggestions!
You should be able to get your desired result pretty easily.
Constrain your Stack View to all 4 sides, using however much top/bottom and left/right "padding" you want.
Give your Image View a Width constraint (I'm guessing you want it around 24), and an Aspect Ratio of 1:1. That will keep it from stretching vertically. Content Mode: Aspect Fit
Set your Stack View properties to:
And... you should be all set.

Unable to center/properly scale button and label in view using constraints?

Im having a frustrating problem as I've worked with constraints in the past and have simply used the "Add missing constraints" to do the job, however I am having a LOT of difficulty achieving something very simple -
I have a larger button and a label positioned on my xib file. I need both these centered horizontally and scale with the device, and more or less maintain the distance between each other. Basically just need them to look reasonably good and for the button (which has an image within it) to scale but maintain aspect ratio.
Add missing constraints has not worked in the least as this results in neither the button nor label scaling (both stay small) and the space between them being too big on larger screens. I've tried every other constraint configuration but the button just ends up being squished or going off the screen. Here is what I'm trying to achieve:
The troublemaker is the button. Is there a way to do this programmatically? I am desperate here. How can I configure the constraints?
How do you set a distance between 2 objects?
Center the button horizontally and vertically with the view. Center the label to the view and give vertical spacing from button to the label. Also give width constraint to label if necessary. This will give you the desired effect.
For UIButton:
1.Centre Horizontally & centre vertically
2.Give Aspect ration (or) use leading,trailing,top,button for scale it to different device.
For UILabel
1.Centre it horizontally.
2.Set vertical to the UIButton.
2.Set Aspect ratio.
Hope this helps you.

How to use auto-layout to put things in a percentage of a screen size IOS

So I am trying to make my launch screen for an app. The launch screen is made up of 2 text boxes and 2 UIImageViews.
This is my sketch up of what it will look like
My problem is I can not get auto layout to work with percentages of the screen, instead it has these numbers that I dont quite know what to do with. So while I know that the two image views are a 4th of the screen width from the edges I dont know how to put it their. Also I dont know how to make the image size occupy the same ratio of space on all devices.
Could you please get me started on setting this up?
autolayout does not Have Percentage support but it supports Aspect Ratio.
you need to add one view and add your two imageViews and textFields add equal width and height constraint for ImageViews and same for TextField
you can also add constraints to ImageView to expand in rest of space for that just add margin constraints and equal width and Height of ImageViews.
add aspect Ratio to ImageView's Width and SuperViews width as your requirement.
you can add aspect Ratio with individually to width and Height.
here as you mentioned in que. you can add aspect ration 4:1 for 4th of the screen size.
do same for TextField.
if you still need help than tell me I can explain in depth with screenshot.

How to manage the gap between two views in auto layout

I am using auto layout for my ViewController I know how to define the gap between two views using constraints programmatically. What I want to do now is when the screen size increase, increase the gap also. Because my UIViewsare positioned properly in iphone 4s and 5s but in 6 and 6 plus they are positioned in a small area of the screen. I know adding multiplier we can set the aspect ratio of a view but, how to increase the gap between 2 views when the screen hight increase.
UPDATE
Let say this image,, there is a logo above this please login label.
This is my verticle position constraint
V:|-70-[Title]-130-[lblFirst]-0-[lblSecond]-20-[textusername]-10-[txtpassword]-10-[btnLogin]
this Title is an image 130pix above to this Please Login label. I want to increase this 130 when it comes to iphone 6 and 6 plus
Take a UIView with alpha 0 and clear color and use it instead of the gap and use aspect ratio to the view used for gap and top and bottom or left and right whatever your situation to the views between whom you want to make the gap dynamic. And also you need to add some more alignment constraints to the gapView but it will work. I could not found any other method to do this easily so i have used this method everywhere in all projects and all working fine. I think UIView with clear color and alpha 0 will not effect the screen or performance in any way. We can use this.
Here is an example like if you have two text fields vertically and you want to increase the gap between them dynamically then take a view between then with the exact same width of text field and add those constraints.
1) Top of GapView to upper text field.
2) Bottom of GapView to below text field.
3) Equal width to any text field or leading and trailing to superview or main view.
4) Proportional Height to main view(you can add this constraint by adding equal height from subview to any of superview and then by changing the multiplier);
And your GapView will increase and decrease accordingly.

Center 4 images with constraints autolayout

I'm new to autolayout and i'm kind of stuck on how to center these 4 images in all different devices like it looks on the images. i've tried to apply the auto configured constraints but then it will have that distance and that does not fit on all devices. So my question is what constraints do i need to apply on all different images in order to make all image centered with same distance?
Here is how my cell in storyBoard looks like:
image of the constraint options on image 1:
image in simulator:
/// MY TRY ////
Here you can see the constraints that i've added and the result?
result:
Your approach is almost correct, it simply lacks size constraints for the images.
If you want to dynamically resize the images and keep the space between them constant, put constraints on the images for the width to be >=20(or any other value, depends on your needs) and a constraint for keeping the aspect ratio. Then ctrl-drag from UIImageView1 to UIImageView2 and set a constraint for equal widths. Repeat that from UIImageView1 to UIImageView3 and from UIImageView1 to UIImageView4.
If you want the images to always keep their fixed size and dynamically sized spaces between them you need another approach:
The trick here is to place 3 empty UIViews between the UIImageViews so that it looks like this:
Set constraints to the top for all views
Set constraints to the left and right for UIImageView1 and 4
Set constraints for all UIImageViews to the same width
Set constraints for all UIViews between the UIImageViews to the same width
Set constraints for all the distances between the views to 0
Set the width constraints for the UIViews to >= 0
This way makes the empty UIViews between the UIImageViews to resize dynamically and all the same width.
I hope you get the idea.
For the first image add a leading space to superview and for the last image add a trailing space to superview and add vertical spacing between first and second, second and third, third and fourth and also set horizontally center within the container to each of the images
The objects on your storyboard require at least 4 constraints per object.
When there's orange lines around the UIImageView object like appears in your question, this means a constraint is missing, or conflicting and can often mean the image does not display at runtime like you'd expect.
In the first instance I recommend you select all objects and 'clear constraints', then start again.
Although not exactly the same, here is a link to an answer which explains the auto-layout constraints in a bit more detail. Once you understand it, auto-layout can be very powerful.
>>Link to similar auto-layout issue - iOS CustomTableViewCell: elements not aligning correctly<<
I hope this helps
If you want the images to always keep their fixed size and dynamically sized spaces between them then very easiest way first you drag the four UIView in your StoryBoard
set constrain top left right and height for all the UIViews and give the equally width to all the UIViews(drag from one UIView to another and sets its equally width repeat this work for all the UIViews).
Image about layouts
Secondly you have to drag your images set in these UIViews and sets their constrain height width and centre horizontal in container and centre vertically in container.
In last set the background colour of all UIViews default(white) you get the result it will work....i assure.
Screenshot of Xcode

Resources