I am very very new to constraints, I have always had an issue with them. I have a logo, 2 text feilds and a button, what I am trying to do is get them to be placed in the same spot regardless the size of the device (centered and middle top position) I tried adding the missing Constraints but it stretched out my image and everything is getting massive on iPads and the look not to be in the same position. I have attached screenshots, I have also created an example app with the one scene.
[![enter image description here][1]][1]
[![enter image description here][2]][2]
What am I doing wrong? Why are these so hard?
I have tried what matt suggested and the end result was this....the logo covers everything, but I get errors now :(
[![enter image description here][3]][3]
[![enter image description here][4]][4]
[![enter image description here][5]][5]
[1]: https://i.stack.imgur.com/81Isa.png
[2]: https://i.stack.imgur.com/qLZcj.png
[3]: https://i.stack.imgur.com/NYqjQ.png
[4]: https://i.stack.imgur.com/w08GS.png
[5]: https://i.stack.imgur.com/CKwh8.png
Use Stack Views to place all the elements vertically.
Then make your stack view centre vertically and horizontally within the container/superview.
Lastly, make stack view top, bottom, leading, trailing anchors greater than equal to zero, so that it never goes out of the screen bounds.
Related
I have images with text written vertically in some cases and horizontally in others. I need to process horizontal and vertical text images differently. Hence, for that I need to first check if the image contains a vertical text or not.If someone can suggest some way of finding if an image contains vertical text or not, it will be helpful. For example see the following imageshttps://i.stack.imgur.com/A5txz.jpg and https://i.stack.imgur.com/7LU28.jpg. The first one contains a vertical text and the second one contains a horizontal text.
I am trying to create this layout, but I am struggeling so badly that I have no words left for it. I have tried for three days but yet I can't create the layout.
I am trying to have the three imageviews in the middle scale if the height of the view changes, but they are not behaving correct. I have successfully manage to get the ratios correct though, but the images either goes behind the blue button, or the blue button suddenly looses its height or the text disappears.
Isn't it possible to have three image views in the middle that take whatever height is left after the title and text + blue button have been laid out and then properly show them with correct ratios?
You can try multiple approaches here:
Size Classes:
Lay out the views for each possible size (probably the easiest but the least flexible
Use Constraints with different priorities:
You can set a max-height for the image views by adding a constraint for the height and then setting its relationship to equal or less than. This allows the image to scale up until a maximum size is reached.
Then add a second constraint between the bottom box and the images. Set the priority of this constraint to 750. This makes the image grow in height, when the bottom view moves down.
Then add another constraint which sets the aspect ratio of the image. If you use the mode of the view to Aspect Fit, the image will now always be scaled up to fit the image view.
To make all images scale proportionally, add constraints for equal width and equal height between them, then set the factor to match the proportions between these images.
The last step is now to vertically align the images properly as you showed in the pictures. To do that, add constraints between the images, which align them by their vertical centers, bottoms, baselines or tops.
The Apple Documentation has shown me how to place constraints between objects that are apart from each other in the view, like here. However, what if you have a display element that is contained inside of another, as shown below:
Is it possible to set constraints that will "trap" the gray square within the red square?
Make sure you have set the necessary constraints for the red box and place the gray box inside the red box.
Red box constraints:
In my case i want the red box in center of the screen with fixed width and height.
Gray box constraints:
Trapped inside the red box with the distance of 64 all side.
You can make the gray square a subview of the red square, then constrain it to the red square, which will be its superview.
The specific constraints would depend on how you wanted the gray square to size itself in relation to the red square.
I am having troubles aligning multiple images by using auto-layout in xcode.
I tried different settings, but nothing seems to work yet (refer to attached photos).
It would be great to hear some opinions from you guys, either in code or storyboard.
I want the image to be aligned equally in different screen sizes.
Too much space
Last image scaled too much
When Equally width is set
Constraints Setting
You need extra views to achieve this
In the sample picture the red rectangles they are all invisible UIView with constraints:
fixed length leading space to the view on the left(or the superview)
fixed length trailing space to the view on the right(or the superview)
fixed length height
This way it's the invisible views who have different width in different screen size while the size of images between them is fixed.
For left most item you should specify leading space to superview, for right most specify trailing space to superview. For all inner gaps between views specify horizontal spacing and for each neighbor view specify equal width constraint. Additionally I recommend for you to specify Align center Y for all views and set the y position constraint only for one of them
I currently try to get a fullscreen background for my iOS Project this means the image should stretch over the full background.
I currently try 2 things but won't worked out as solution.
1)
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
This only set the background image on the actual image size and repeat it in x and y, it does not stretch the image to fullscreensize.
2)
use a UIImage and make it equal to width and height and use aspect fill, this also does not give the wanted solution and also use much memory
Is there any other way to get this done?
So you have two ways to do it always with AutoLayout
1) With an addition of an Inequality constraint like shown in the images below.
2) Or by adding Leading, Trailing space and Top, bottom space Constraint and centering vertically and horizontally constraints but NO explicit height nor width constraint.