auto layout stretching image issue - ios

I'm trying to stretch an image from the very left of the view to the very center. I tried pinning constraints using left 0, bottom 0, and right 300, but it doesn't exactly goto the center. Any ideas?

Assuming that you know how to set the left, top, bottom, height constraints I will explain you how to set the right constraint, which will cause the imageView to span from the left to the center of the screen. Setting any width constraint will not work, because the width depends is different on every device.
click your imageView
choose Align -> Horizontally center in container
click on the newly created constraint
choose the Size Inspector
Change the First Item from Center X to Trailing
The "standard" Horizontally center in container causes the Center X of your view and its super view to be aligned. If you change the First Item to Trailing instead of Center X you therefore align the trailing edge / right of your view to the centerX of your superview.

Related

I can't align this Image View in center of container - iOS

See the image below
I really need align this view in the center of the container.. (StackView) !!
To align the StackView a use Constraints Top 20 right and left 10 only it..
And I'm trying to align this image view at the center of the container but I don't know how I can do it.
Remove the left constraint , drag from the stackView to the superView and select center Horizontally from the popup and click AddConstraints
Or
click the left constraint and edit it to centerX , then set constant = 0
If in attribute inspector, you put alignment for Stackview to "center", it should be in the center. Also, give the image width and hight.

How to achieve this layout with auto layout ?

I m trying to create attached layout, I have added a superview and four subviews each view is aligned to one of the sides, to achieve the + sign in the middle .
Constraints I have added are: aspect ration to each subview and leading trailing margins, along with this also have added a width constraints to left bottom view.
But obviously this isn't working on smaller screen and everything gets messed up, not sure what exact constraints should i add so that I get the desired result on variable screen sizes.
create a superview with required aspect ration.
Add four subviews and make all equal width and equal height.
Pin each superview like "top, left" "top, right" "bottom, left" ""bottom, right""
Give one sub view with ratio according to superview width like if u want 10 px padding horizontally then give 320:155
And Height of one view according to their width.
Find the sample code made for this.
https://github.com/iTamilan/AutoLayoutPlus
You should use proportional height and width for the views, for eg:
For the Top Left view align Top Left view's top to top of the superview and similarly do this for leading. And now make width proportional to half the width of the super view minus half of the separation between the two views.
In short: Top left view's leading = superview's leading
Top left view's top = superview's top
Top left view's width = superview's width/2 - separation/2
Top left view's height = superview's height/2 - separation/2
Similarly do this for the rest of the views and you will be good to go, and your views will be fine on every device

Center image in LaunchScreen storyboard

I have an imageView in my LaunchScreen.storyboard. I centered it with two constraints:
Center Horizontally (Align Center Y)
Center Vertically (Align Center X)
I also have:
Width constraint equal to 84
What happens is that the image is just below the center. It's not perfectly centered.
Any suggestions?
Thanks!!
Give constraint as below:
1) Top space to superview
2) Bottom space to superview
3) Leading to superview
4) Trailing to superview
This will fill the whole nib with an imageview
One easy way to do it is to add constraints on each side that are equal to the one on their opposite side to center your image. I recently did it to center a label between another label and the the remaining 3 edges of the view

XCode 7 Autolayout Constraints: How to make 2 images stay horizontally in the center

I'm trying to position 2 UIImage views in the center of the screen, side by side. If it's just 1 UIImage, I can just set them to be vertically and horizontally center in the container. If I have 2 UIImage views side by side, I can only set it to be vertically centered, but not horizontally centered since both UIImage views would stack over each other..
Could someone please guide me ? Thanks and greatly appreciated!
Take a view, put both the images inside that view. Add the constraints on those images with respect to the view you made (Edge to edge to the view). Now add the constraints on the view in respect of the super view. (Center vertically and horizontally). That's it!
An elegant way to solve this is to apply constraints on UIImageView itself.
Now think that if you need to have both the image views in superview's centre then one imageView should have it's trailing edge at the centre of superview and another should have a leading edge at the centre.
Control-Click on imgvLeft and drag it to superview, you will see some constraint option to select; choose trailing edge constraint. Now go to that constraint and click it you will see details as shown in picture below. Change the second item from trailing to centreX and keep the constant 0.
Same way you can apply the leading constraint on imgvRight and change the second item to centreX.
Now if you wish to keep some distance between both the views then you can change the constant value in constraints. For example if you need an spacing of 20 px then change the constant to spacing/2 i.e. 10.

How can I give constraints some margin from the center of superView

I am using auto layout. I have one UIView that takes place at 20px margin from the center of the superview.
Right now I have given the Top Space to superview that i have make in the iPhone 6+ Xib. but when i run in the iPhone 4 simulator it goes to too much down.
Any idea
If you want the view to be placed relative to the center of the superview, you should be using centerX and/or centerY constraints, not a constraint to the top of the superview. In IB, if you choose the "Vertical Center in Container" constraint to center the view vertically, you can edit that constraint to change its constant value which will offset the view from the center by whatever number you choose.

Resources