How to Align Everything to Centre Xcode - ios

I am trying to align all items to centre in Xcode as you can see here but the problem is it looks un aligned in the other views
I have tried to do this using constraints but these do not seem to make any difference after updating frames.
All I am trying to achieve is for everything shown to be centred across all devices.
Thanks.

You can simply pin them using the horizontal center in container constraint in Storyboard.
Select all your views and assign it here:

I aligned Horizontal and Vertical centre in container from the Editor -> Align menu and then added constraints using the constraints editor for Height and Size. I also setup some top constraints (using I bars in constraints editor) so the items below my image all lined up with it correctly.
For more info, watch this Youtube video provided by Lennet.

Related

An image view on an Image View xcode

I'm developing an app using Xcode 8.1 and and swift 3.0.
Using storyboard I've expanded an image view to take up the whole view and set a picture to it to be used as a background image (using self.view.background doesn't give the image the same layout).
This works. I have defined the constraints for it plus added 2 textboxes and a button that also have constraints.
Now I try to add a new image on top of the background image, but then I cannot define the constraints for the front-most image view. I get something like this with two errors telling me to define the constraints but when I do nothing happens.
The way it looks you as per the screenshot, you should just give the padding (spacing to nearest neighbour) for left , right and top in your storyboard.
If there are any warnings just set the current constraints to supersede other views. This can be achieved by selecting the warning and then update constraints and check the box apply to all views in container. It should work.
You need to pin the imageView, what constraints have you set so far?
Just set the constraints for the front image view (as on the your screenshot as well). I don't see any problems.
have you set below constrains
1. Top Layout
2. Bottom layout
3. Leading
4. trailiing
5. height
or
horizontal in center
vertical in center

Xcode 8.1 Auto Layout

Hi there I am currently trying to get the following Scene to scale correctly for any scene.
This is currently how it looks on a iPhone 7 Plus:
I managed to get it to scale the blue background correctly by adding the following constraints:
However, I don't know how to get the UIImageView, UITextField and UI Label to scale in the same way. I Want them to keep the same proportions as they have now on the iPhone 7 view. I have looked for youtube tutorials but none seem to show how to introduce a way to have these items scale as I want them to.
Any help is much appreciated, Thanks.
You can add constraints between UIImageViews, Labels, the window, etc. by holding control and clicking-and-dragging between two elements. For example,
UIImageView: add these constraints:
Vertical Spacing to Layout Guide (This constrains the top of the image to the top of the window)
Leading Space to Container Margin (Left side of image stays 'connected' to the left side of the window)
Trailing Space to Container Margin (Right side of image stays 'connected' to the right side of the window)
Vertical Spacing (vertical spacing between the image and the Label)
You can use this same idea to constrain the Label and Text Field to the left side of the window and to keep the vertical spacing between the UIImageView, Label, and Text Field consistent on all devices.

How to Align four buttons at dead center in Xcode 7?

How do I align these four buttons to be at the center (horizontally and vertically) for the diff sizes of screens. I see different sizes and positions of the buttons when I run the project in the different size of the simulator. Is there a way to make the four buttons to be at center (and same size) for ALL devices?
I don't have any experience with constrains, it has been a while since I used Xcode. I read tutorials about constrains for hours, but I still don't understand how to use it.
Screenshot
Thank you and happy holidays :)
The easiest way to accomplish this is to use a UIStackView. Very simple to use, and it automatically manages the layout for the buttons.
Remove the existing constraints for your buttons, then select all four and choose Editor -> Embed in -> Stack View.
Add two constraints to center your stack view in the view, and you're done!
Yes you can use Stack View, but it is only supported from ios 9, so if you are trying to have constraints below ios 9, follow below steps -
Add a UIView as container for your buttons, and add following constraints to it. (We will not give fixed height constraints to this view,let it determine its height intrinsically i.e we will use buttons to determine the size of container as buttons have their intrinsic size.)
a. Align Center X to superview
b. Align Center Y to superView
c. Trailing and Leading space
Now add first button and add following constraints -
a. Center horizontally in center.
b. Top space from superview (as buttons and have its intrinsic size so we dont need to give height and width, it will calculate from its content)
Now for rest of three buttons add following constraints -
a. Center horizontally with first button
b. Add top and bottom space to all buttons with their respective top and button views (buttons). For last button i.e 4th add bottom space constraints with container view.
Note - Now that top button have top space constraints with container view and last button have bottom space constraints with container view and all remaining buttons are connected to each other with top and bottom space, this will determine the height of container view and your buttons will be always appear in center of screen for all size.
Below is the link for screenshot with all required constrints explained above.
Screenshot with required constraints

Aligning two button images at a fixed distance in ios app-Swift

I am working on an ios app where I want to have two buttons at a fixed distance from each other. Something like below:
I tried dragging one constraint between the two images. But when I run it in simulator it shows overlap as below.
Can someone guide how to put both the pictures at a fixed distance. Also, I am seeing when I turn the simulator as right in hardware option some of my buttons are not getting auto aligned to horizontal screen and doesn't get displayed. How to fix this too?
Thanks
First of all clear all constraints. and follow below steps :
select both button with command button press
Give Width & Height constraints for both. (Pin at bottom-right side)
give Horizontal Centre in container constraints for both. (Align at bottom-right side)
Now, update all constraints (Resolve Auto-Layout Issue bottom-right side)
Hope, it's work..
Design your view according to this image
Drag a new view and then put your two button in it
Constraint for new view
Top
Horizontal center
Fix height and width
Constraint for left button
Top and leading
Fix height and width
Constraint for right button
Top and traling
Fix height and width
It will work defiantly.

Need to fix autolayout issues with only UIButton's and UILabels

Currently i am trying to use AutoLayout on my iOS app in xcode 6. The storyboard I am using consists of only UIButton and UILabel elements, which i would like to resize and fit based on the device. Everything works on the iphone 6 Plus and 6 but when I get to the 5S or 5 things get weird. I can't use size classes necessarily due to both 5s and 6/6P using compact width regular height. IS there a way to fix this?
all images can be found here
Yes! With autolayout there's always a way.
From those pictures it looks like you may have a vertical constraint on the "play" button that is attached to the top of the view. Try putting that constraint on the "How Long" label. That way the button position will be relative to the title instead of the whole screen.
Check to see that you have the following constraints :
Center all buttons and labels horizontally in the container (note,
this is 3 different constraints you need to set). You can select
all 3 at once and from the Align menu center them horizontally in container.
Constrain How Long to Top Layout Guide (Top Space to Top Layout Guide)
Constrain Play to How Long (pin vertical spacing)
Constrain Leaderboard to Play (pin vertical spacing)
Depending on how you want Leaderboard to float you may want to pin it to the Bottom Layout Guide instead of to play. The buttons and labels will then move according to your constraints.

Resources