I am new to iOS and i want to set constraints to 3 buttons like following image.
First i want to set Background view (Green colour) = (0, 100, device width, 150);
then 3 buttons on the Background view (buttons are subviews of Background view).
Buttons height and width is fix i.e., 100 and 100.
Space between the buttons is 50 and buttons y position is 5
please help me to set constraints, the following image illustrate the Design. Thanks in advance
A bit tricky to explain, but I'll try, so select button2 and click the pin option and add the five constaints.
The select button1, add the following constaints and repeat for button3.
Finally you can align button2 to center horizontially.
Hope that helps, also read through the link Ulli posted, it's a good one, good luck.
Related
I'm working on a calculator app that looks like this so far:
The clear button is constrained to 8 pixels below the text field that's above it (which also has constraints that keep it 8 pixels from the edges in the top left corner), the 7 button is constrained to 8 pixels below the clear button, etc. and all of these buttons have a constraint that says they are all equal heights.
My goal is to add a constraint that says the 0 button is 8 pixels above the bottom of the parent view, and then have all the buttons automatically resize to maintain the size and spacing constraints between them. However, adding this constraint just generates some conflicts rather than resizing the buttons. Is there a simple way to tell my app to resize the buttons to satisfy constraints? This is a picture of what I'm hoping these buttons would look like if all goes as planned:
Thanks so much!
Your problem is as a reason for varying in screen height satisfying your constraints can only be done by letting one part (Above 4 blocks / 0 button) to resize itself according to the constraints set for the other part , So Insert the first 4 blocks (4*4) in a UIStackView (inside it 4 horizontal stackViews ) and make it Fill Equally
then when you give any height to 0 Button the above stackView will resize accordingly
Note : I know there is a button on the bottom - right that spans 2 rows , no problem add it above the design i mentioned
I'm totally new to Swift and in general, to iOS development. I just want to center a rounded profile picture with a table view above. In iPhone 5 it looks centered, but in iPhone 6 and 7 it look aligned to left, I don't know how to fix with constraints, I'm a little bit confuse.
This is my constraint tree:
How it looks storyboard for iPhone5
How it looks storyboard for iPhone 6/7
This is how it looks in emulator with iPhone 6
Can you guys help me to figure out what I'm doing wrong?
Thanks in advance!
Cheers.
It looks like you've set the width and height of your image view to hard-coded values, which will result in it looking centred for some views, but not all.
Reset your constraints, then set your distance from each of the four sides to zero. That'll centre your image!
Go to Editor -> Resolve Auto Layout Issues -> Reset to Suggested Constraints...
...then click that button down the bottom right and set each of your four sides' distances to zero.
Make sure you set Update Frames to "All Frames in Container" before clicking "Add 4 Constraints".
Try setting horizontal constrain between your My UI Image View and HeaderView. You can set centre hozintal with HeaderView.
Go to button down the bottom right and set centre vertical and centre horizontal to your My UI Image View.
Hope it helps. Happy Coding!!
I am currently having difficulty with AutoLayout. I am using interface builder and am trying to position two buttons of equal width side by side as illustrated in the following image.
From the following Preview image, my titleImage has been properly constrained and is displaying correctly however the buttons are not. I have experimented by aligning button 1 with the leading edge of titleImage, and button2 with the trailing edge of titleImage, however the width distribution between the two buttons becomes skewed at this point, as demonstrated below.
My objective is to understand what constraints are missing and should be applied to the two buttons to maintain equal widths regardless of device. If possible I would like to achieve this through interface builder as opposed to additional code.
Add the following constraints
Assign equal width from button1 to button2.
Assign horizontal spacing between both buttons.
Assign leading space from button1 to its superview.
Assign trailing space from button2 to its superview.
Assign top space to both the buttons.
Let me know if it works for you.
Follow Steps and Screenshots for easy solution
Step-1)
For Button 1 : Set Constraints: (1)Leading, (2)Top or Bottom asper your need, (3)Height
Step-2)
For Button 2 : Set Constraints: (1)Trailing,(2)Top or Bottom asper your need, (3)Height
Step-3)
Press Ctrl + Drag From Button 1 to Button 2
Select Horizontal Spacing
Step-4)
Select Both Button (using Command) and Add Constraints Equal Width
OUTPUT
Hope it Helps you :)
Stack layout in iOS9, will do the job really nice.
Add stack view to your view and configure as follow:
You can check my example - you can easy aim this, by using proportional constraint.
Also you can easy aim proportional multiple UIViews.
Please look attached example
https://dl.dropboxusercontent.com/u/48223929/TestViewController.xib
My solution is to
Put a small view in the middle of two buttons and make it centre(Horizontal centre in container and vertical centre in contener as 0).
Add height and width to the small view.
Add buttons the constraints and give horizontal space constraints to small view.
Give the small view background colour same as buttons or View's colour.
Note: See the Screenshot.
so my storyboard look like this
But when I open on iOS simulator, the item (playing card) is off to the right
What is wrong with the spacing/alignments of Storyboard?
(This is with the most recent iOS 8 and XCode 6)
Select the playing card ImageView and press the Align button in the auto layout toolbar.
Check the Horizontal Center in Container, ensuring the value is set to 0, and click Add 1 Constraint.
Then press the Pin button and add a top spacing to nearest neighbour constraint of 0, as so:
Add constraints to make it horizontally centered in the view. It will do.
See Apple's document, it will help.
I was literally bugging out about this same problem with the same example. I finally figured it out.
First constrain the size of the button. Then with descending contraints: Align center x to center x of button. Align center y to center y of button. BINGO!! Thanks for your input. #Adrian Sluyters. I firth thought the Detail View was a zoomable view, but the constraints simply adjust where the center of the view is. Lesson learned.
See my post:
Detail View on Storyboard too zoomed in - creates misaligned objects in simulator
I have read lot of documents and tutorials about autolayout and constraints but I don't know what I'm doing wrong but in my case the examples doesn't work...I know that this is a repeated question but I don't know what I can do...
I'm trying to fix a button to my bottom screen. Bottom with 0 distance. In 4 hinches screen all is ok but when I'm changig to 3,5" the button dissapear. This is what I'm doing:
Normal button
Select button and add the bottom space constraint
Button look likes fine
When screen size changes, the button dissapears
Thanks a lot!
You can complete the following steps to achieve that:
Add bottom constraint, width and height:
Add horizontal centre constraint:
Now you can test on your storyboard by clicking the Apply from factor button:
Select the button, and select "Update Frames"
Suggestion: you should add constraints to the button for left, right and height.