In preparation for a possibly bigger screen in the iPhone 6, we can obviously programmatically base layouts around screen bound size, but how do we ensure the custom xib files are formatted? For example, I have a custom UITableViewCell that is set to 320x145 in storyboard. Can I somehow prepare this for a different sized screen?
Have you looked at the new Xcode6 beta? As far as I can see, we are able to create freeform views there in storyboards:
Looks like this (+ Auto Layout) allows us to be prepared for flexible interface types.
If you truly need to prepare for something that is rumored, I would suggest the same methods that everyone used when preparing for the iPhone 5:
Download and install latest version of Xcode.
Set a 6-inch launch image for your app. This is how you get new screen height (without it, you will get 1136 px with black margins on top and bottom).
Test your app, and hopefully do nothing else, since everything should work magically if you had set auto resizing masks properly.
If you didn't, adjust your view layouts with proper auto resizing masks or look into Auto Layout if you only want to support iOS 6 going forward.
If there is something you have to do for the larger screen specifically, then it looks like you have to check height of [[UIScreen mainScreen] bounds] (or applicationFrame, but then you need to consider status bar height if it's present) as there seems to be no specific API for that.
Example:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == new screen height) {
// code for 6-inch screen
} else if (screenBounds.size.height == 1136) {
// code for 4-inch screen
}
else {
// code for 3.5-inch screen
}
The absolute best thing that you can do this time around (not just doing a bunch of if's everywhere) is to use Size Classes in your Storyboards.
Whats new in Xcode:
Size classes
Size classes for iOS 8 enable designing a single universal storyboard with customized layouts for both iPhone and iPad. With size classes you can define common views and constraints once, and then add variations for each supported form factor. iOS Simulator and asset catalogs fully support size classes as well.
Related
I am using lauchscreen.storyboard for splashscreen in my iOS application. I have added splashscreen image in the storyboard. When app is lauchning, portrait splashscreen is coming correctly. But, when opening app in landscape mode, blue color appears in both right and left side of the splashscreen.
I tried setting different splashscreen image by adding variation. But, this storyboard takes any one portrait or landscape image only.
Let me know how to set two images in storyboard, one for portrait and one for landscape or the best practice to set splashscreen for both portrait and landscape orientation. I couldn't find any solution for this in the web though it seems to be a simple problem.
Setting the same launch image for both orientations:
Add an image to your LaunchScreen.storyboard's main controller's view and constraint all edges to superview (not safe Area)
Note: to change a constraint that is referencing SafeArea you can double click it on the inspector and change the respective item. Here are some screenshots for reference
Set the image's content mode to something that scales appropriately (scale to fill or aspect fill for example)
The setup should look like this (you can enable the previews on the right by clicking the assistance editor and switch from Automatic to Preview):
Setting different launch image depending on orientation:
Click on the + button next to your image
Add a customization for regular width & compact height
Your setup should look like this:
Update: What about the iPad?
Unfortunately, it seems that (at least up to Xcode10.1) is not possible to customize your launchscreen the same way for iPad, for a couple of reasons. The main one is that iPads are Regular x Regular for both portrait & landscape. The other reason is that you cannot use custom classes in your LaunchScreen.storyboard. Because if you could, you could subclass UIImageView and override the traitCollection with something like this (essentially would be treating the iPad as an iPhone sizeclass-wise):
override public var traitCollection: UITraitCollection {
if UIDevice.current.userInterfaceIdiom == .pad && UIDevice.current.orientation.isPortrait {
return UITraitCollection(traitsFrom:[UITraitCollection(horizontalSizeClass: .compact), UITraitCollection(verticalSizeClass: .regular)])
}
return super.traitCollection
}
By the way, you can still use code like the above in the rest of the application for your windows/views if you want to solve similar problems.
I know that this is not what you were looking for in the answer, but I am afraid that (for now), you'll have to use static images in your Assets for handling iPad.
By default use portrait, Add variation to your image for landscape version with these selections.
Update your landscape picture here.
personally, I also recommend you to use AutoLayouts.
Run the app.
Example - Results -
how to set two images in storyboard?
iOS has sizeClass for almost anything you can see. You can choose image, color, etc for any situation you need based on size of the main window of your application (Not only orientation of the device)
It's more convenient. Because maybe the device is in landscape mode but it's in split screen of iPad š¤·š»āāļø. So let OS decides what is best match for the size situation.
- Size Classes in interface builder
[
- Working example:
Make use of Vary for traits as shown ion screenshots. I have tried this and has worked for me.
How can I make different layouts for different orientations on ios?
I try to make an layout in landscape to show 6 picture's in 2 rows.
But in portrait I only will show 4 pictures in 2 rows.
I already have 2 different storyboard for iPad and iPhone.
Can I do this over constrains?
Can I do this with different storyboards like in android?pain
iOS has something called Size Classes. You can use a size class to target a specific device and/or orientation. You can choose the appropriate size class at the bottom of interface builder (wAny hAny) and layout your view accordingly.
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/AboutAdaptiveSizeDesign.html
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/LayoutandAppearance.html
Adding on to #Nitesh's answer: Correct me if I am wrong, I am not very well versed in iOS development as well. I read that it is almost always recommended to use Size Classes while only using one storyboard for all targeted devices. It makes handling issues like screen sizes and orientation much more managable.
In your case, may I suggest using UICollectionView to achieve that effect? It rearranges your tiles based on how much width you have and it supports orientation quite well.
I am a little lost here.
Consider in a landscape mode I have 3 UIButtons which are of equal widths aligned next to each other with a gap and the occupy the entire space of the screen. These buttons were added programatically with fixed button sizes.
All looks fine and nice in a iPhone4s screen. Now comes the iphone6+. The way I am determining the button sizes is
buttonWidth = 21.f * [[UIScreen mainScreen] scale];
And then the button seems to fit good for the iPhone 6+ screen and covers the full area.
Will it work for iPhone 6 as well ?
Is this the right approach? How do i determine different sizes for the buttons for different screen sizes? Yes, I know adaptive layout, but we still have to specify the button sizes, dont we?
Help would be great appreciated.
I've just started writing iOs apps for work and I came across this problem which has to do with resizing my buttons on a screen to suit both the iPhone 3.5 inch and 4 inch. If you look at the screenshots below you will probably get the majority of what I am trying to solve.
So our designer has designed the layout whereby as you can see the colour buttons are supposed to fill the height of the screen for both iphone sets of devices, minus the height of the stautus bar and the navigation bar of the app.
I've had a look around a liitle bit about auto-resize, however I don't feel it is suitable for the requirements as you can see in the image.
Another approach that I thought about was in code, I would write an if statement, to determine if the person using the app was on the iphone 4 inch then load different images. However upon thinking about it more detail I would have to change the position for each of those buttons to ensure that the newly loaded images to prevent overlapping.
My question would be is there a way to change the position of the buttons in objective c to achieve what I want according to the screenshots?
Also it would be greatly appreciated if you can tell me if I am on the right track on the approach of my current problem.
Thanks heaps.
If you don't want to use AutoLayout, then I would advise turning it off in Interface Builder so you can control the frames manually. As for controlling the frames, make sure each of your views that you want to edit is accessible to your code (either its an IBOutlet or you manually instantiate it) and then adjust the myView.frame property. myView.frame is a CGRect, so you can edit it like this: myView.frame = CGRectMake(x, y, width, height).
This is my problem: I need instructions on how to fix the 3.5 inch xib for the 4 inch screen. I have already loaded the Default-568 img so I no longer have the letterbox layout.
I have the traditoinal 3.5 inch scren XIB and everything is perfect.
But when we turn to the 4 inch screen there is a large space to be filled and if I rearrange the XIB file for the iphone 5 screen when I turn to the 3.5 screen I can't see the button. Does it makes sense? Also I want to support versions prior to ios 6.
What I need is a fix or code or anything for the IPhone larger screen to be distributed proportionally.I have been reading that a solution could be duplicating the original xib file and arrange myself duplicated file in a 4 inch layout but I couldĀ“t figure out how to let the IPhone make the decision of opening one of the two xib files existing.
I have been given this piece of code but I still do not know where to paste it. I tried in the viewDidLoad but nothing;
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
NSLog(#"Ipad ");
}
else
{
NSLog(#"Iphone %f ",[[UIScreen mainScreen] bounds].size.height);
if ([[UIScreen mainScreen] bounds].size.height == 568) {
//this is iphone 5 xib
viewName=[[NSString alloc]initWithString:#"ViewControllerIphone5"];
} else {
// this is iphone 4 xib
viewName=[[NSString alloc]initWithString:#"ViewController"];
}
}
I donĀ“t know where to put that code; I know it is in the implementation file but I don't know where.I don't want autoresizing masks
Please, IĀ“ll be glad if you could help me with this problem. I need careful instructions as I donĀ“t have a lot of knowledge in the topic.
Thanks in advance!
You have 4 options
1-) using a seperate xib file for 4'' screen, and in applicationDidFinishLaunching:withOptions method deciding which xib to load depending on mainScreen height.
2-) using auto layout ( only ios 6 + )
3-) using struts and springs
4-) calling a method only on 4'' devices, and setting view's frames and positions for 4'' display manually in that method
Don't have separate nibs for this case. Use your normal 3.5 inch layout and use auto layout or springs and struts (under the dimensions tab for each object) to make the view resize correctly.
Check the Apple docs on Auto Layout and Springs and Struts for more info.