This is my first post. I just started coding. I want to make my first simple app. I own an iPhone 5S and when I add constrains in compact width / compact width & regular height(in XCode), it shows it right on the iPhone 6S plus simulator. When I try it on my iPhone it just looks weird. I know the sizes of the iPhones are different, but how can I correct this? Now, if I code in compact width / compact width & regular height, it feels like I am coding for an iPhone 6...
I hope someone can help me.
Here is the Storyboard:
I use compact/regular size classes for portrait, and any/compact for landscape, for those two phones you mention (actually, for all of the phones).
And, (squinting at your constraints in the tiny pic), it looks to me like the constraints are "wrong" (not optimal, at least). You've got the button pinned 550 down and 280 across from the edges, which won't look right for smaller devices.
Try aligning the button to horiz and vertical centers, and it will look right everywhere.
Or pin ONE of the edges of the button, and ONE of the top/bottom edges of the button, to the layout margins on one corner. Then it will look right on all devices.
Instead of removing the trailing and bottom constraints, as #Kevin suggests, set both of their constants to 0 (or whatever's reasonable), and make them greater than or equal, not equal.
For the constraints to appear fine for different screen sizes, one possible way is to add the required constraints to your button or label or any other component and have a preview look at it in the assistant editor section.(Click on assistant editor and in the split screen that comes up, find the Automatic section and within it the preview option)This helps to view the layout of your app for different screen sizes. This helps to manage your auto layout issues by a good extent.
Related
I've got all my elements in the view and have added constraints to position them correctly on any device.
But when it's run on a 3.5 inch iPhone some of the bottom stuff is cut off and when it's run on a iPhone 6Plus there's a lot of extra space on the bottom.
How could I resize all the elements to look good on all devices?
This is a universal app to run on iPhone and iPad.
I guess you constraint an element as width equals 300...It's very bad for some elements that should fill (or fill percent of) the screen. I suggest you make percentage constraints. For example, make equal width to view, click Edit button and set Multipiler 0.6. It looks like this in storyboard:
make two constraints (trailing and leading for example) and make aspect ratio for your image, maybe this will help you.
I'm working on a Calculator App, and all my code executes perfectly, but the only thing I am hung up on is the layout. On all phones the layout works perfectly, constraints seem to be perfect! Except for the iPhone 4, it just doesn't seem to be tall enough! It's quite frustrating as it's my first real project I've worked on and to see it stopped by something like this is annoying to say the least.
The buttons are 5 high and 4 wide, and all of them are square, and then I have a 320x50 AdMob ad at the top, with the value label inbetween.
On the 5, 6, and 6 plus there is plenty of space and everything looks perfect, but when I simulate a 4s, the distance from the ad to the buttons is dramatically reduced, so the value label is unseen. You can see what I mean from these screenshots.
Here's a working layout:
And here's the problematic 4s:
Does anyone have any ideas for a solution?
I'm not sure how you set up the buttons, so can't give a concrete answer.
Generally, however, you can overcome the short screen dimensions by either
(1) Detecting short screen (ie window.height < 500) explicitly and change the necessary height constraints. This looks like a hack, but often works wonderfully, as iPhone 4/4S is the only exception.
Or
(2) Setting the height constraints proportional to its superview, rather than a fixed number. This may result very tall elements on tall screens, and can be mitigated with less-than-or-equal constraints and priorities.
If I were to do the calculator UI, I'd set constraints so that the five rows of buttons have the same height of 1/5 of their superview -- by creating for 4 equal-width constraints, 1 top spacing and 1 bottom spacing constraint. Then I'd create an outlet for the view that contains all the buttons, and follow either (1) or (2).
I don't know what kind of constraints you're using, but if it works for iPhone 5+, it seems like you're doing the right thing. :-/
What I do in my apps is something like:
valueLabel.Top == ad.Bottom, so they are always touching each other and I do not set the height or width for both of them, so they can be whatever height they want. Other than that.. I do not know.
I'm struggling a bit with constraints on iOS with the differents screens sizes nowadays.
I tried google and stackoverflow to find a solution but my english does not seem fluent enough to find an answer.
I got 4 buttons verticaly aligned, the first and the last one are constrained by the edges of screens (kinda easy). But I'm really struggling with the constrains of the 2 middle buttons. I can't find a way to make them equally spaced from the left and right buttons on every size screens.
Is there something easy and tricky to make these constraints right ? Or am I doing it wrong and should I try to do it programmaticaly ?
Thanks
Add three invisible views in middle of each button, make their width equal with each other with constraint then your four buttons will be equally spaced. For the Y then you just need to pin it at your desired place.
If you are going to support only iOS 9 and higher, then use a UIStackView.
The solutions to this problem is very simple. This can be solved using the concept of "spacer views". You have to place invisible views between each red coloured view. You would be requiring 3 in your case. Then make their background as clear colour. .
Next, make their width equal and constrain their edges to the views that are after and before that view. You then define the size for red coloured view.
REMEMBER dont give the "clear coloured views" any fixed width. It would be determined by the runtime.This would solve the problem. Tell me if any more information is required.
Here is a blog post for the solution for this
http://adamdelong.com/fluid-layouts-with-auto-layout-size-classes-spacer-views-and-constraint-priorities/
This is the youtube video for this
https://www.youtube.com/watch?v=eSG-3-QpmWk&feature=youtu.be
Besides Tj3n's answer with views between buttons, you could use
A UIStackView (iOS 9!) where you use for settings Axis: horizontal, Distribution: equal spacing
A Toolbar (depends on what you want to do with your buttons) with toolbar items and flexible Space between them
Why not size classes? Apple introduced the concept of adaptive user interfaces in iOS 8 relying on a combination of Auto Layout and size classes.
If you aren't aware of what is size classes, there are plenty of tutorials available, please find one.
Summary: Apple very cleverly removed two story borads for iphone and ipad and made a single story board for universal app. No you dont have to struggle trying to apply autolayout constraint that satisfies all the screen sizes :)
Below are few of the size classes and their meaning :)
Regular width x Regular Height ----> iPad Potrait mode/ipad landscape mode
Compact width x Regular Height ----> iPhone 6 plus,iPhone 6,iPhone 5s,iPhone 5,iPhone 4s potrait mode
Compact width X Compact Height ----> iPhone 6,iPhone 5s,iPhone 5,iPhone 4s landscape mode
Regular width x Compact Height ----> iphone 6 plus landscape mode.
You can select the size classes you want to support from story board :) and start applying constraints specific to each size classes (like buttons in middle) or if you have generic one (like your buttons fixed to screen) for all the size classes.
You can deploy, remove, reuse or delete the constraints form various size classes.
SUMMARY: Buddy, If you are not using size class yet, its a high time to start using it :) There is a wonderful video on it in apple WWDC sessions 2014 i believe. Download, watch, start playing with it.
Happy coding :)
you can use equations to get this appearance.
use views' trailing points to get this.
View1.trailing = superview.trailing*(2.0f/9.0f)
View2.trailing = superview.trailing*(4.0f/9.0f)
View3.trailing = superview.trailing*(6.0f/9.0f)
View4.trailing = superview.trailing*(8.0f/9.0f)
if you make View1.width = superview.width*(1/9.0f) you can achieve what you want.
there are a lot of similar solutions for this issue. but the base is using trailing points.
It may also be done with using centerX positions of Views.
View1.centerX = superview.trailing*(3.0f/15.0f)
View2.centerX = superview.trailing*(6.0f/15.0f)
View3.centerX = superview.trailing*(9.0f/15.0f)
View4.centerX = superview.trailing*(12.0f/15.0f)
Thanks to LearneriOS answer, I solved my problem.
In order to get my wished result, I created 3 views with 10 width.
My first and my last button were already constrained. I constrained my first extra view to my first button with Horizontal spacing then i constrained my first extra view with the Center vertically in container. I then constrained my extra view to his own width and heights.
There come the important part: I did go on the constraint menus and selected the width constraint. The value inside was still 10 but I did change the priority from 1000 to 750.
Then I did copy my first extra view and constrained all of them to the nearest buttons, the same way I did with my first entra view but I removed their width constraint (to all the extra views but the first one) and constrained the extra views with the first one by plugging the: Equals Width.
Then I got my result, I hope it was clear enough and thanks again.
I'm a bit stuck and any help would be greatly appreciated!
I'll give you a quick overview. I have designs for a screen that were built in sketch using an iphone 6 screen size, then redesigned to fit and look right for an iphone 5/5s/5c, and 6 plus as those are the only devices that I want to support.
But the design portion or implementing the designs for just one specific screen size is easy and I understand that part. Where am I'm lost completely, is how to implement a design in one view controller that looks the way that it should in all the screen sizes I designed for.
I've gone through more than a few auto layout and size class tutorials and not sure how I properly use them so that the app recognizes "this is an iphone 5, use these image sizing and placements instead, and this is an iphone 6 plus, use these" and so on.
Everything I've seen to this point regarding auto layout and constraints only use 1 set of numbers to judge distance from elements for example, but all screen sizes would have different distances.
What am I missing or not understanding? I know I'm looking at something improperly.
Thanks in advance for all help!
You're correct that just one set of numbers could be used to judge distances, but this can still describe how a view should appear on different screen sizes. Your problem may be that you're thinking of constraints as describing the frame of your view? (Which obviously has to be different on every device). I find it more helpful to think of constraints as describing how each edge of my view relates to another view.
For example here's a view controller I setup with all the same constraints and how it would look on different devices. The constraints describe how the large grey view is pinned it's left and right edges 20 points from the left and right edges of its container view. It's pinned to 8 points from the top and 8 points from the top of the label. The label is centred vertically and horizontally and it has intrinsic content size. Each button is pinned 20 points from the bottom edge with button 1 and 2 being pinned to the 20 point from the left and right edges respectively.
I don't know if that helped or if that wasn't the answer you were after and you need to arrange your views differently depending on the device: you can tell Xcode which device size and orientation the constraints you're creating are for, using this button in Interface Builder.
Use it to select a device size/orientation. After, any constraints you create will only be used on that device. By default any width and height are selected so normally your constraints are applied to all devices.
Also, you cannot choose which devices you want to support, only the iOS version.
I am struggling to use Xcode 5's constraints to fit content (relatively) between 3.5" and 4" screens. To simplify matters, I am only concerned about portrait at this moment
Here is an example screen of a layout on a 4" screen (the tab controller at bottom isn't shown):
I want to lock the proportions of any images (such as this truck), but allow all labels, buttons etc to squeeze more closely together on the 3.5" screen. As all iPhones are the same width, I think I need to leave the x-constraints as-is, but configure dynamic y-co-ordinates, but would appreciate advice on this.
Many thanks.
You can pin the width and height of your image in your storyboard by selecting the image and then adding the necessary constraints (your numbers will be different):
You can add constraints to automatically change the vertical layout by making the relation by "greater than or equal" or "less than or equal", but, in my experience, it is difficult that the result is exactly what you want, as the autolayout engine will not make the line separations the same.
My suggestion is that once you have the layout for one of the screen sizes, you add outlets for the vertical constraints and then adjust those constraints constant property in code on -viewDidLoad. That way the distribution of space will be exactly what you want.