How to create different constraints for iphone 5 and iphone 6 - ios

Is it possible to create a constraint specifically for iPhone 4s and 5s without affecting iPhone 6?
Please see image above:
The scrollview's height is with equal height with the content view. I changed the multiplier to make it 40% of the actual height of the cell.
The cell is a fix height, the same height as the main window.
If you can see on the preview on the left, there is really no space for 5s and 4s devices. So I wonder on how to approach this design issue.

Please try to use aspect ratio for all views with window view. It will calculate screen size and it will align automatically to satisfy your constraint. I had the same problem, i overcame by setting aspect ratio.
please look this, link http://mathewsanders.com/designing-adaptive-layouts-for-iphone-6-plus/

Unfortunately you cannot do this. The solution of your problem is changing the constant of a constraint. So I suggest you detect user's device see here how to do it and change the constraint value something like this:
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
if (IS_IPHONE_6) {
self.verticalConstraintOfButtonToTop.constant = 22;
} else if (IS_IPHONE_5) {
self.verticalConstraintOfButtonToTop.constant = 17;
}
}
or you can create separate storyboard and use it.
Also you can use Size Classes to create universal storyboard for iPhone and iPad or portrait / landscape modes.

I don't really understand your question. Perhaps this will help:
Constraints are like other IB items in that they can be IBOutlets. Just ctl drag from a constraint in the IB object outline to the associated view controller. In the view controller you can adjust its values in code depending on the device type.
Hope this helps.

Related

View size control in iPad and iPhone

I am designing one of my view in my
MainView
I am plan to have a smaller View instance in my MainView. What I need to do is:
When the app is running on iPhone, the view size should be square and its width is equal to the screen's with. When the app is running on iPad, the width should be 1/3 of the screen's width.
So how to define different width ratio on iPhone and iPad. How to approach this? Can I do this in storyboard file or I have to do this by code? Thanks in advance.
In your storyboard file you will need to create variations for traits. There is a button in the lower right hand side of the storyboard inspector that says "Vary for Traits", using this you can set constraints to have different constant values depending on the device size(iPad/iPhone).
In this case you can pin the leading and trailing edges of the smaller view to the sides of the parent view, and for the iPhone vary the trait to be equal to zero, while the iPads space can be set to 10 points(however many points of space you want).
Creating adaptive UI's can be and is a rather complex subject, so if you have no experience, like #Zack Leonidas Hamblen said check out that article and read up to get a better understanding on what is happening and how to use it efficiently.
You can set your constraints in Storyboard, create an #IBOutlet for the constraint and do something like:
if UIDevice.current.userInterfaceIdiom == .pad {
// set the width constraint = 1/3 of the screen width
} else {
// set constraint = screen width
}

Get variable height for a UIButton for iPhones 5/6/6+

For my 1) portrait only 2) deployment target iOS7/iOS8 app, I have in my design UIButtons which have variable heights, for iPhone 5, iPhone 6 and iPhone 6+. I am trying to use auto layout for this. (Not using size classes though).
Using auto layout how can I specify variable height for UIButton for these 3 screen sizes. The buttons look fine on iPhones 5* models, but thinner on iphone 6/6+. Now in auto layout I can say height >= or = or <= say 55), but how do I specify 44 for iphone5, 55 for iphone6, 66 for iphone6+?
Is this something that I can achieve using only auto layout or do I need to manupulate (frames) it in code? What is the point of me using auto layout then?
In addition to frames my designs also specify different font sizes. How do I go about this. Any best known methods for this, Stack-O friends .. ??
You are correct to ask "what is the point of auto-layout if I have to manipulate frames directly"? Thankfully, you don't have to.
An easy way of doing it is specifying the height in relation to a view of standard height (think a view that fits the whole screen).
For example, we can set our button's height to equal half the height of the view.
This way, the button is always going to scale with the view, either upwards or downwards (size-wise). Auto-layout will guarantee that the relation between them will always be 1/2.
Now the button will be half the size of its superview, regardless of size.
It sounds like you need to be modifying the height constraints constant value.
In your xib/storyboard, create an outlet to your view/controller for the height constraint.
At runtime, probably in viewDidLoad, you will work out which device you're on, and then just change the constant of the height constraint.
CGFloat height;
// if iPhone 5
// height = 44
// else..........
self.buttonHeightConstraint.constant = height;
This will automatically trigger a flag that tells AutoLayout to recalculate frames and layout the view again.
I managed this by adding a Height-Constraint to the UIButton. Make an Outlet and you can set the Height in your UIViewController subclass with _myConstraint.constant = 55;

iOS Autolayout constants issues which cause the UIView not occupy the whole width in iPad

Here is the screenshot of my ViewController in my Storyboard. As you see, there is a UITableView inside.
And the autolayout setting is like this:
There is only one setting for "wAny hAny". That means the layout should be the same in iPhone and iPad. Unfortunately it's not. It's fine in iPhone. But in iPad, the result is like this:
The tableview is not taking the whole width of the UIView. Why? Can anybody suggest? Thanks
Uncheck constrain to margins, and set all constraint values zero.

How to handle iPhone 4s, 5 and 6 screen size, and make all the contents look the same across al the devices

I am using Xcode 6.1 and in one of the views I have, contains many labels. However, these labels look different in each different devices. Although I have add constraints to all the labels, but still facing the same issue.
How can I get the same view layout across all devices?
How can I make the width and height for each label is flexible, depends on the screen size?
Do I need to design different xib for each device?
The following images showing the result am getting, with auto resize enabled, and constants been added.
on iPhone 6
on iPhone 5
on iPhone iOS 7.1
We have provided with a simple solution . Using Aspect ratio constraint we can get a relative look in all device screens.
I had the same issue and I found this >>
http://mathewsanders.com/designing-adaptive-layouts-for-iphone-6-plus/
So when designing for lowest to highest screen resolution we can't set a specific width and height for all controls as it may look weird. So using this constraint we can have a relative look for all screens.
If you don't want to use constraints or you want to keep the same width & height , you can add scrollview to hold all controls. For that you can set a resizing frame for scrollview with fixed content size .
To solve this...
1) Go to xib file and uncheck the Auto layout
2) Then set the autosizing manually in the following manner for each and every subview.
Use self.view.bounds.size.width to get the width of screen, then use relative value to initialize all labels.
One soultion is that you can Design a story board for each screen size like one for iphone 5, one for iphone 6 and one for iphone 6+ and in your AppDelegate method "didFinishLaunchingWithOptions" you can get the screen size and popup corresponding storyboard
in this wasy you can set different constraints according to different screen size

how can I use one storyboard for 4" and 3.5" iphone screens with autolayout (ios6 + ios7)?

While there are many questions and answers about building a storyboard layout that would work both on 4" and 3.5" screen sizes, I couldn't find a suitable solution for the following scenario.
I use autolayout with ios6 (and ios7), and I don't have to support landscpae or ipad. I have a UIView with several subviews, which have to look as the mockup below.
It is easy to set up autolayout constraints in the storyboard to fit either of the screen sizes. My question is - how do I make autolayout choose the correct constraints depending on the screen size at runtime?
Note, that I DONT want to use 2 different storyboards. Doing so across my whole application would be a lot of work, and I would have to hook up all the delegates, outlets and actions on each storyboard. A change in a screen would require me to do double the work.
I have tried 2 methods to make this work on one storyboard, but I'm not satisfied with either of them.
Double the constraints. The larger constraint (50) has a higher priority than the lower constraint (30). The problem with this approach is that on the 3.5" screen size, autolayout may pick a just few lower priority constraints - enough to satisfy the layout - but leave some high priority constraints.
Subclass NSLayoutConstraint. In this method, all the constraints in the storyboard are set to be NSDualLayoutConstraint. In in initialization code of NSDualLayoutConstraint, the constant of the constraint is changed to the value of 3_5_constant in case the runtime screen size is 3.5". this method is more deterministic, but you can't see the 3.5" layout in the interface builder preview.
If only interface builder constraints had a secondary constant value that would be applied when the screen size is 3.5", it would solve my problem..
So I remain with my question - how can I properly use a single storyboard to layout its subviews correctly for 4" AND 3.5" screen sizes?
If you want to use only one storyboard and you do not want to utilize auto layout, which would make your life a lot easier for what you've shown in your diagram, you will have to layout your views in code.
You will just need to detect if the user is running on a 4" device and layout your views accordingly.
#define IS_568_SCREEN (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON)
if (IS_568_SCREEN) {
// Lots of code to layout for 4" devices
} else {
// Lots of code to layout for 3.5" devices
}
However, if you were to use autolayout for this, you'd find it's going to save you a ton of time and code. Instead of having to manually layout every view in code using the solution I mentioned above, you'd simply need to update the y and height constraints depending on the device.
Considering this diagram showing what autolayout would handle for you and what you'd need to update manually, this should help paint a better picture of just how much you'll save with utilizing autolayout.
#define IS_568_SCREEN (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON)
if (IS_568_SCREEN) {
self.layoutConstraintY.constant = 50.0f;
self.layoutConstraintHeight.constant = 248.0f;
} else {
self.layoutConstraintY.constant = 30.0f;
self.layoutConstraintHeight.constant = 220.0f;
}
[self layoutIfNeeded];
This stackoverflow answer has solved the same problem for me.
Key is make an IBOutlet to the constraint (e.g height). Then do something like this
- (void)updateViewConstraints {
[super updateViewConstraints];
self.myConstraintFromIB.constant =
[UIScreen mainScreen].bounds.size.height > 480.0f ? 200 : 100;
}
You will need to edit your constraints programatically. You may find this question helpful for that.
You could also have two separate Storyboard files, or two viewController scenes in one storyboard file.
How about adding an extra view and including the square view inside it? The extra view should be transparent so no-one sees it. Set the constraints of the extra view to expand with the screen size and set the constraints of the square view to be centred in the extra box. Select a multiplier for the square view size relative to the extra box

Resources