I need to add 3 labels to my UITableViewCell when my phone is in portrait, but when it's in landscape mode it should display 5 labels. Just to add complexcity when same cell is in ipad landscape it should show 6 labels. It should look something like this.
iPhone portrait
iPhone landscape
iPad landscape
I can do this using diffrent UITableViewCell XIB's but I want do this using Size classes and storyboard. Is there any example / tutorial / suggestion to follow ?
What you're looking for is this tutorial by Apple:
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/EnableAndDisableViews.html
This panel will install a given view for a given size class, in other words, make sure it exists or doesn't exist:
(source: apple.com)
This still doesn't give you the opportunity to change it for iPad specifically because the size classes aren't distinguishable but perhaps you could smuggle as I think the iPhone has it's height as Compact in that mode and the iPad and iPhone 6(S) Plus as Regular. So you need to add that extra label in the width looking at the combination of Regular width and height.
Also make sure you install the right layout constraints in more or less the same way as in one scenario you space to the next label while in the next scenario that label is gone and you need to space it to the right side.
Related
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.
I have a label in a custom cell and it currently has a width constraint of 150. When the phone goes into landscape orientation, I would like to increase the width constraint to 300. Then of course change it back to 150 when phone goes back to portrait. How could I accomplish this?
You can combine Size Classes to achieve this. You can set up 300 on AnyWidth CompactHeight(landscape mode for iPhone) and set up 150 on CompactWidth RegularHeight(portrait mode for iPhone).
Enable Size Classes in the Interface Builder Document. Then, you will need to configure based on on different size classes. More information, you can find it online: Adaptive Layout from Ray Tutorial
Another approach will be: changing the constraint's constant value based on different mode of the screen. You will need to reference the constraint as IBOutlet and then change it programatically. But, we don't suggest to do so because Apple is tended to have different size of device and it's better to work on layout by combining Auto Layout and Size Classes
I've made my storyboard support only w:Regular h:Compact.
Cause i needed to support all devices in landscape.
But I've figure that it's not supporting iPad that way.
When i run iPad it just a blank page.
I tried to convert it to any:any but it hide all the sub views.
Is there anyway to convert this class size to any:any to support all without risking redo all this work?
Or at least I need a way to make it only for iPad?
First of all, wRegular hCompact is for iPhone 6 Plus in landscape, not iPads in landscape:
Be sure to check the description at the bottom of the size class choice.
If you want to convert existing elements to show in wAny hAny, try choosing each element, then going to the attributes inspector and clicking the + button at the bottom, then add it for any width and any height. Make sure the checkmark is there beside installed for any width and any height also.
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
I have laid out my ViewControllers in Storyboard using Auto Layout, and everything looks perfect on an iPhone 5. Now I'm testing for iPhone 4 and things are not in their correct position. For example, I placed a UIView at the bottom of its parent UIView. It's width is the screen width and its height is 100. It shows up nicely on an iPhone 5 but doesn't appear at all on an iPhone 4 (I think it's because it's drawn at the height it should for an iPhone 5 which is too far below the screen for a 4)
If I'm using Auto Layout, do I still need to make adjustments to my Views? If so, what's the point of using Auto Layout at all?
Here is the approach to make your layouts fit to retina & non retina form factors.
I am not saying these are the rules, but I follow this approach & hardly get any conflicts which is difficult to resolve.
First & foremost : Always try to design your storyboard on non-retina form factor (I mean in your case design it on iPhone4 size & then apply retina-Form Factor & verify how it fits on iPhone5 size.).
Definitely your view at the bottom of its parent will be having a fix constraint for "Y" position. Usually, you need to make a relative constraint. So when you show it up on iPhone4, the Y & the height might be going beyond the maximum Y axis value.
Try checking these values for your layout in Assistant Editor like topSpace, leadingSpace, trailingSpace, bottomSpace .
AutoLayout doesn't mean that it will fit to your needs, however it adjusts the component accordingly.
In case of any warning in Storyboard, try using the help & suggestion provided like, add missing constraint or adjust frame to suggested
Storyboard will never behave in the same manner for each & every screen or view your design. You need to practise it more & more & you get to know how to add constraints.
Hope that helps.