storyboard IB to big for simulator [duplicate] - ios

Here is design of my storyboard:
And here is output:
Why this button is not located in its correct location?

This is a common question on Stack Overflow. You need to add auto layout constraints to the button. It doesn't know if you mean "in the centre" or "300 points from the left". You can add them by selecting the button and tapping the button with the square on it in the bottom right of the editor.
These are the buttons.
The buttons are...
Align views. Either multiple views or along a single view with its superview.
Add constraints to a specific view. Left, Top, Height, Width, etc...
Auto constraints. Update frames to constraints. Etc...
Resizing options.
You need to add constraints to ALL of the views in your app.
So, on button2 you will need to tell it...
How far down from the top.
Align the button to the centre of the superview.
For button1 you will need...
How for down from the top.
How far in from the left.
For the map...
Zero from the bottom, left, and right. (That's three separate constraints)
How much spacing between button1 and the top of the map.

Related

Constrain multiple UIViews around centered UIView

I am trying to learn auto-layout/constraints but for the life of me I cannot figure out a solution to my problem. I have multiple buttons surrounding a 'main' button. I would like the layout to remain the same and the buttons to auto resize depending on the device AND to remain in the center of the superview. I have been reading and trying and have yet to get anywhere. Any help would be greatly appreciated.
At the moment I am not worried about landscape view but if anyone would like to elaborate that would be great.
Any references I could read or videos would be excellent as well.
Thank you kindly
Example Layout:
I can offer some advice.
Firstly, all your views and buttons should have proportional widths and heights relative to the screen size of the device you're using so it will look decent across different devices.
Secondly, apart from the width and height constraints, your middle "main" button will also have a centerX to your UIViewControllers's view as well as a center Y so you can keep it pinned to the middle. This is important because the other 6 buttons will be pinned relative to this main button.
Let's start with the other buttons, specifically the one above the main button and the one below the main button. These two buttons also have width and height constraints. Now, horizontally, you have two options - since these guys are always in the middle of the screen, you can align their centerX's to the main button, or to the UIViewController's view itself - it doesn't matter as long as it's in the middle. Lastly, we have to consider the vertical distance from the main button. These distances should be proportional to the total height of the screen so it will look good across all devices. You'll be adding a constraint from the bottom of the top button to the top of the middle button. For the bottom button, you'll be adding a constraint from the top of the bottom button to the bottom of the middle button.
Now for the other 4 buttons, apart from the height and width constraints that they will have, you should constrain the horizontal constraints to the top and bottom middle buttons, and then constrain vertically to the main button.

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

What is the best approach for horizontally aligning 5 buttons with Autolayout

I know this is a little newbie style question but I couldn't find any subject about this and I have to horizontally align multiple buttons in my view controller.
I tried to 2 approach first one is separate them with UIViews and <= width changed constraints priority and others. Second one doesn't have UIViews but buttons aligned in starboard very well. I already watched Stanford auto layout lessons. But I couldn't find any solution about this. I don't know how to approach to align multiple buttons or views can someone help me to learn and solve this problem ?
Thanks !
You just need to give leading, trailing and equal-widths constraints between adjacent buttons.
Steps (We'll only consider horizontal constraints here):
Add leading constraint from the first button to the superview (or left-side sibling view).
Add horizontal spacing constraint between:
First button and second button.
Second button and third button.
Third button and fourth button.
Fourth button and fifth button.
Add trailing constraint from the fifth button to the superview (or right-side sibling view).
Add equal widths constraint between:
First button and second button.
Second button and third button.
Third button and fourth button.
Fourth button and fifth button.
If you follow this approach, you need not add extra views to align the buttons. Please see the screenshots below for understanding this better.
As i understand your horizontally aligning 5 buttons problem with
Autolayout, I make some draw to how you fix it. It has following:
Hope, It'll helpful to you.
You should use a horizontal stack view. You set the constraints of the stack view to its superview, and then drop in your buttons. You need to adjust spacing, stretch to fill, etc in the attributes inspector and it'll be perfectly aligned.

How to add Equal Spacing and equal width for button in iOS Auto layout

Am new to Auto layout constraints in Xcode. I have one Bottom View like UITabBar with 6 UIButtons. Without constraints I aligned those buttons with 5 space padding with each buttons and each buttons has 50 width.
Now,I am trying to make it by using Auto layout to support all Screen sizes.
In Storyboard constrains I set equal width for 6 buttons and I made 60
space between each buttons. I set first button 0 to leading space and
I set the last button 0 to tail constraints.
I want the buttons with equal width and flexible spaces between views in all device sizes.
Can anyone pleas help me? Looking for the help. Thanks in advance.
check this image and made your constraints like below...
RESULT:- preview in different sizes
With Stackview (For iOS 9.0 and above)
NOTE: If you have to make app for iOS 9 and later then UIStackView is another option for you
With iOS 9 and Xcode 7, you can now have the expected result with UIStackView and only 3 or 4 auto layout constraints.
The following step by step uses Storyboard.
Create 6 UIButtons and 5 UIViews. Align them horizontally. Give some color to your views.
Select all your views and buttons and click on the Stack button. You will have all your buttons and colored views aligned in a UIStackView.
Select your stack view, go to the Utilities panel and select the Attributes Inspector item. There, click on the Distribution button and select "Fill Equally".
For now, your stack view's height relies on your buttons intrinsic content size. If you're fine with it, you can go to step 5. However, if needed, you can give your stack view a height constraint. Select your stack view, click on the Pin button, check the Height button, add the required value and click on the Add 1 Constraint button.
As an alternative, If you want each element of your stack view to have its height matching its width, select the first button of your stack view, click on the Pin button, select Aspect Ratio and click on the Add 1 Constraint button.
You can check that your aspect ratio constraint is correct in your Document outline (left panel) and, if you need, you can change it with a different constraint in the Attribute inspector (right panel).
Now, it's time to give some external constraints to your stack view. Select your stack view. Click on the pin button, make sure the Constrain to margin button is not select and set leading, trailing and bottom constraints to zero. Make sure that the bottom constraint is related to your view controller's view. Then, change the Update Frames button to "All Frames in Container". You can now click on the Add 3 Constraints button.
Your stack view is now set.
Further remark:
If you don't need your colored views width to match your buttons width, you can build a stack view with only UIButtons and simply add spacing to your stack view in the Attribute inspector. However, you will have to find a way to add a background color behind your stack view. Apple states about it in the UIKit Framework Reference:
The UIStackView is a nonrendering subclass of UIView. It does not
provide any user interface of its own. Instead, it just manages the
position and size of its arranged views. As a result, some properties
(like backgroundColor) have no affect on the stack view.
I've build a Xcode project with 4 different stack views:
one with colored views and relying on the embedded UIButtons intrinsic content size for its height,
one with colored views and with a UIButton "equal width and height" constraint,
one with colored views and with its own height constraint,
one without colored views but with spacing and embedded inside a colored view.
You can find this project on this GitHub repo.
iOS 9 has a new UIKit class called UIStackView. It is very helpful in stacking up views horizontally or vertically, like the way you want. You should check out this tutorial: Raywenderlich: UIStackView Tutorial
Select all the buttons and in Xcode’s Pin menu
Create a horizontal constraint from the top left view to the top right view by selecting the red line to the selected view’s right side nearest neighbor
Create a horizontal constraint from the top right view to the top left view by selecting the red line to the selected view’s left side nearest neighbor
Create a vertical constraint from the top view by selecting the red line to the selected view’s top side nearest neighbor
Select checkbox near to Height, Equal Widths and click on "Add 22 Constraints"
Spacer views should no longer be necessary unless you need space between borders.
You can create a StackView with equal spacing which will dynamically adjust the space between your stack views to use up all available space without altering the size of your subviews.
Interface builder can be a bit misleading here. the value you put in for spacing is the minimum spacing required not what the actual spacing will be.
The StackView will handle your spacing, making the size of your buttons equal you can use regular constraints for.

Why button in storyboard is not located in appropriate location in ios8?

Here is design of my storyboard:
And here is output:
Why this button is not located in its correct location?
This is a common question on Stack Overflow. You need to add auto layout constraints to the button. It doesn't know if you mean "in the centre" or "300 points from the left". You can add them by selecting the button and tapping the button with the square on it in the bottom right of the editor.
These are the buttons.
The buttons are...
Align views. Either multiple views or along a single view with its superview.
Add constraints to a specific view. Left, Top, Height, Width, etc...
Auto constraints. Update frames to constraints. Etc...
Resizing options.
You need to add constraints to ALL of the views in your app.
So, on button2 you will need to tell it...
How far down from the top.
Align the button to the centre of the superview.
For button1 you will need...
How for down from the top.
How far in from the left.
For the map...
Zero from the bottom, left, and right. (That's three separate constraints)
How much spacing between button1 and the top of the map.

Resources