XCode 9.3: macOS 10.13.4
I have an swift app where the buttons are off-kilter. I need them at the bottom of the screen and centered. The iOS simulator for iPhone 8 shows the buttons correctly. However, the simulator for the iPhone SE and the actual SE (my iPhone) show the buttons anchored to the right edge of the screen. How can I insure that the buttons are displayed correctly on all iPhones?
First sol
this layout can be done correctly if the center of the middle button is equal to the center of the screen
Second sol
create a UIStackView with these constraints
1- left , right , bottom to screen
2- height say 100
with distribution set to fillEqually , spacing set to say 20
drag the buttons inside it and they will look right in any device
Remove the constraints from the 3 buttons: Calc, Clear, and Help
Align clear to be horizontally centered
Align Calc trailing offset from Clear's leading.
Align Helps Leading offset from Clear's trailing.
Align Clear bottom offset from the bottom margin.
Align Clear, Calc, and Help along to center vertically with one another.
You have two choices: 1) use the legacy struts and springs mode in interface builder to distribute your items as you need them or 2) spend the time learning auto layout, and use it instead. There isn't any easy answer because of all the different screen sizes, and you haven't even considered iPad screens here. The best way to pick up auto layout is to view the WWDC videos on the subject. Yes, it will take longer, but your app will respond better to screen changes, and you will also be able to inherently accommodate new screen sizes.
Use a horizontal stack view for buttons, and set spacing between buttons.
Approach 1 - Set trailing space and leading space, pin the view to bottom and set a fixed height for stack view.
Approach 2 -
Set fixed height and width to stack view and center horizontally, and pin the stack view to bottom.
Always try to use stack view, with that you will reduce the number of autolayout issues you run into.
Please add the constraint for stack view and adjust like that:
Related
I have an Iceland map which contains 9 different area buttons. Every button has a different size. I want the map can show the same on different devices. I only know to set the central button with horizontally in container and vertically in container. However, other buttons, no matter how I set the constraints, they will be a mess on iphone SE or iPhone Plus. (I use iphone 8 as normal)
Can anyone teach me how to set the constraints for the 8 left buttons? thank you!
The best thing for this is a vertical UIStackView nested with 3 horizontal stacks
MainStackView constraints
centered vertically & horizentally , width & height propotional to screen
then drop 3 stackViews inside it set axis = horizontal and drop 3 Buttons for every inner stack
Note: distribution is fillEqually for all the stacks , spacing = 10
look to this Demo
Something I've done in the past is used a single control as the anchor for all the others.
Basically this means taking the centre button and anchoring it to the middle of the container view (centre horizontally and vertically)
I then constrain the middle/top and bottom controls to it (vertical space and horizontally centred)
From there each control in the row is then constrained against these elements (horizontal spacing and vertically centred)
You might also consider constraining the sizes to the centre control as well, so all the controls share the same size. For me, this means constraining the width of the control to a set value and then applying an aspect ratio to the height. I do this because then I can change the value of the width constraint and ALL the control will change size
Because iPhone 5s, iPhone, iPhone+, iPhone X all share the same "sizing classes", it makes it some what more difficult (as you can't apply traits - but you can do it for iPad and iPhone ;)).
At this point, I would bind the centre controls width constraint (and the height if you need it) to the source code and when the view is loaded, determine the device screen size and make minor adjustments to the constraints values.
If you would prefer a complete "storyboard" solution, you could constraint the width of the centre control to the super view and apply a modifier
Using the "device" templates from the storyboard, layout one a iPhone 5s and iPad Pro
The long and short of it is, you have "options". You might even consider using UIStackView to define the rows, personally I find it easier to constraint the controls to a central anchor point - but that's my experience and your needs might differ
I am working on IOS app and its my first app to be build on IOS. I have good experience of developing Android apps. So I have gathered basic information and knowledge about IOS development. I am working on autolayout. But I am not getting it work completely. I have seen Some developers are working on code, But I just want to use IB as I believe there will be some way to handle this.
So looking for help, Please do not down vote my this post As I am posting this after trying almost everything I get on internet.
First take a look at what I have developed and what it is :
in picture there are different numbers of simulator such as Ipad pro 12.9, Ipone 8 plus and I phone se
What I have done so far:
I have taken the View (the white box in middle). I have made it center vertically and center horizontally.
I have set view height as 300 points, and width to match the superview with some margins
I have put the Text Fields for taking Phone and PIN from user and OK button as shown in picture.
Problem & what I want:
- The problem is view is center vertical and horizontal but not its
content are. I want this view to manage its height on the basis of
its content inside. so that It can be looked as center aligned
vertically. Right now its not center align vertically
- Secondly the problem is the width of text fields, as on Ipad they looks too bad. So I really want them to be look good on all devices.
Is there any solution for this using the adaptive/Auto layout? please help me that How can I get this using IB.
Try following solution:-
Fix the width of textfield.
For label use minimum font scale(search on google).
For width and height you have to use adaptive layout.
width = 70% of superview which means multiplier for your view will be 0.7
height = 60% of superview which means multiplier for your view will be 0.6
Refer to the image:-
Equal width with the superview
Set the multiplier as 0.7
#Android Teem,
For this you need to use SizeClasses along with the AutoLayout.
Below I am trying to give you solution.
use items structure as like below. Don't use another UIView inside main view. Instead user StackView. It automatically adjust contents inside it. For more just look for "UIStackView" on Apple Doc.
Next you need to add constraints to stack view as
Leading 10 (for x)
Trailing 10 (for y)
Allign to center (for width)
height stack view will automatically adjust.
Now double click on leading and trailing constraint will open size attributes. Click on "+" icon just before Constant. It will show you a pop up. add Regular Width Regular Height for iPad and set values as 200 (I have give 200, you can give based on your calculation and requirement). this will show like below image
Now Either run or check from preview for all screens. you will get desired results. my changes show result as below
Happy Coding
Here is what you need to do. Go to the storyboard
Right click on the white area of the screen and drag the cursor to the green area.
Few options will appear, select Equal Heights
Now the white dialog box will become equal height to the green screen but don't worry. Select that height constraint and set the
multiplier to let's say 0.5 for 50% height of super view. Repeat the
same for width
Remove the height constraint and hook the bottom most button to the bottom of the view
I know this is a really easy and basic question but this is my 3rd day with this little problem. I know this is a little problem for advanced iOS programmers. I have an app which contains 1 text field, 1 button, 1 background, 1 logo, and 5 labels.
I want to see the same view when I launch at iPhone 4 or iPhone 6, etc. I know I have to use auto layout and constrains but I just started and it's really my 3rd day that I couldn't save the problem.
Can someone please help me? I'm so tired of this
Constraints you'll want to add. By the way, you should become comfortable making dynamic layouts and auto layout in storyboards if you want to make an app.
Background
Top to top of view
Right to right of view
Left to left of view
Bottom to bottom of view
Karabuk University Image
Center Horizontally in view
width and height in aspect ratio
Current position from top margin of view
Left to leading & right to trailing of Not Hesa Button (below)
Not Hesaplama Button
Height set
Distance from Karabuk University Image (above) set
Left and right 40 from left and right margins
Center horizontally in view
Hesapla Button
Center Horizontally in view
Height set
Leading and Trailing equal to Not Hesaplama Button (above) Leading and Trailing
Distance to Not Hesamplama Button set
Ad At Bottom
(p.s. iAd is being shut down, look at Google AdMob or Amazon Ads)
Bottom to bottom of view
Left to left of view
Right to right of view
Height set
Don't just copy it, try to understand it and incorporate for the rest of your app. Good AutoLayout tutorials: Ray Wenderlich AutoLayout for iOS 9
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
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.