Constraints to be set for x position - ios

I've a scenario where in I've to display four circles. These should be in center of screen. I tried to place x and top constraint but this doesn't works fine for iPhone 4 device.
What constraints should I set so that all four circles should appear in center for all iPhone device?
Further, there is UICollectionView in bottom. I tried to set the top, left, w and height but this didn't worked.
Any solution?

1) Embed your 4 circle in UIView.
2) Set UIView constraint top, fixed width,fixed height and horizontal center
3) Now Select all 4 circle and set constraint like top, leading,trailing,bottom and equal width.

Related

Image view autolayout to pin 60pt from horizontal edges, but auto scale height?

I'm trying to figure out an approach to the following:
I have a square image, I want to align it horizontally and vertically in the centre of the screen and constrain it in a way where its left and right edges are 60pt from screen edges.
I got thus far, but am now getting an error from XCode about top and bottom image constraints. How can I make it so image keeps its square ratio and auto resizes based on screen size?
Example, images width became 294pt in screenshot below, yet height remained unchanged, where as it should change to 294 pt as well.
You need to add 3 more constraints.
Vertically align your view to superview
Horizontally align your view to superview
Set aspect ratio of your view to 1:1 (Important)
On the whole you should have 5 constraints. The screenshot is given below.
this error occurs when auto layout did not find enough information about a component on the storyboard. as you did it here you only provide the trailing and leading constraint so now it asks for height and width.
if you want the same height on every device selects the height constraints that will fix the heigh 300.
Or simply you can do this set the alignment constraint to Center vertically and horizontally and then add top and leading constraints

Auto layout issue for devices in the same family and orientation

I'm working on my beginner app and checked on the preview option for my app. I've noticed that across the iPhone X, 8+, 8 and SE the spacing of my objects are off. I've learned about the custom size class for various devices for different orientations but nothing what said about the same device family in the same orientations (ex: iPhones in portrait mode). This is what I am seeing.
The iPhone X looks fine but the others, not so much. I'm guessing that instead of using hard numbers for auto layout I should be using a ratio based on the height and width relative to the screen. Is that right?
Try to take Base ViewController as iPhone4S. Drag UIView called as HolderView, which should hold that UILabel and Two UIViews.
As per Design, give HolderView's width and height be same. Like Square shape.
Give Constraints as, left and right as 20px, CenterHorizontally and CenterVertically, and finally give AspectRatio to itself.
HolderView
Add UIView for UILabel to HolderView called as LabelHolderView.
Give constraints for LabelHolderView as left and right 20px, CenterVertically and Height as 90px (approx - Two lines for UILabel)
Click CenterVertically constraints and decrease Multiplier value to 0.4.
You can get answer from here that how to change multiplier value.
LabelHolderView
Add UIView for Two Action to HolderView called as ActionHolderView.
Give constraints for ActionHolderView as left and right 20px, CenterVertically and Height as 160px (approx)
Click CenterVertically constraints and increase Multiplier value to 1.3.
Inside that ActionHolderView, drag Two UIView called as PhotoView and NoteView and Embed in UIStackView as Horizontal axis, Fill Equally and 8 as Spacing.
Give constraints for UIStackView as top, left, right and bottom as 0.
ActionHolderView
Change all UIView's BGColor as per your design. Then, add UILabel and UIButton and UIImageView to respective UIViews.
For UILabel, top, right, left and bottom as 0. (number Of lines as 2)
For Two UIButtons ,right, left and bottom as 8 and height as 30
For UImageView, Width and Height be 50px (Change width and height from Size Inspector - Square shape). Give Constraints as CenterHorizontally, CenterVertically and AspectRatio to its SuperView and AspectRatio to itself
ViewController
You need to set height/width constraints for your items. I would also put your add photo/add note buttons in a view to make it easier for constraints. Don't be afraid to mess around with it, you can always to command z to restore the previous version.

Xcode 8 constraint help required

I am new to IOS development and have the attached screen that I am developing in portrait mode only. I have fiddled around with constraints for half of today but not really got anywhere with making this work on anything other than a 5s.
Not sure if i constrain to edge of phone and/or the box at the side of the one i am trying to constrain or both!
It would be much appreciated if someone could point me in the right direction to get this to fit on all screen sizes. Landscape I think is a pipe dream at this stage:)
Here are the width constraints you should add to each button:
add a equal widths constraint for each button to the main view, then set it's multiplier to 0.5. That will make each button half the width of the screen.
for the dark blue buttons, make a leading constraint to the left edge of the view (not the margin) and set it's constant to 0.
for the light blue buttons, make a trailing constraint to the right edge of the view (not the margin) and set it's constant to 0.
For the Y positioning, you should do the following:
for the top buttons, add a vertical spacing constraint from the buttons to the title view, with the constant set to whatever you want
for each button that follows underneath, add a vertical spacing constraint to the button above it with a constant of 0
for the bottom buttons, and a vertical spacing constraint to the bottom layout guide with a constant of 0
This will adjust the height of the buttons to match the screen size / orientation while maintaining your layout. should work fine with landscape, although those buttons will be awfully skinny!

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

auto layout stretching image issue

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.

Resources