I've to develop an iPad app that should have a layout for portrait and a layout for landscape. This app should be able to work with iOS 8 and iOS 9. To design two different layout should I use 2 storyboards: one for the portrait layout and one for landscape layout or I can use only one storyboard with size classes?
I found this solution over stackoverflow, but I'm not sure if it's the right way to work with 2 different layout. Does anyone have an idea about how to work with a portrait layout and with a landscape layout with iPad? So you think it's better to use size classes or to use 2 different storyboard: one for landscape and one for portrait
As per your question you need to use size class for creating application. But in traitCollection you will only get wRegular-hRegular for both landscape and portrait.
result of log value of trait collection with statusBarOrientation.
All you can do is add make constraints active and inactive in
-(void)updateViewConstraints {} method based on [UIApplication sharedApplication].statusBarOrientation.
First you need to bind the constraints you have given to the view and make their objects in .h or .m file.
Then you need to change the constant value of the constraint or make constraints active and inactive based on your requirement.
Result:
1.) iPad Portrait View
2.) iPad Landscape View
This is one way by which you can layout in iPad. Hope it helps you solving your problem.
Related
I suppose this is a more basic question. I am stuck with this app I am coding for the iPhone X. I have it set to display a lot of buttons on the Main.storyboard file. Everything looks how I want to look but the problem is I do not know how to make the screen change with landscape rotation left and landscape rotation right. The screens looks the same but I want to style the page differently.
Xcode 9.1
iOS 11
Swift 4.03
If you want a an actually different layout for landscape (and not simply an adaptive one) you should use size classes:
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/Size-ClassSpecificLayout.html
This will enable you to make completely different UI for different orientations.
Interface Builder lets you customize many of your layout’s features based on the current size class. The layout then automatically adapts as the size class changes.
...
As the view’s size class changes (for example, when you rotate an iPhone or switch an iPad app between full-screen and Split View), the system automatically adds items to or removes them from the view hierarchy. The system also animates any changes to the view’s layout.
If the only reason you want different layouts in landscape right and landscape left is to avoid the “notch” at the top of the screen and the gesture area at the bottom, then what you really should be doing is laying out your views relative to the “safe area”, which exists for just this reason. If your views are laid out relative to the safe area layout guides, then they’ll automatically adjust to avoid the notch and the gesture area automatically, regardless of orientation.
https://developer.apple.com/documentation/uikit/uiview/positioning_content_relative_to_the_safe_area
You could make two xib files for each specific orientation and then using what user dfd suggested, load the xibs respectively in the following code:
if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
//Load xib for landscapeLeft
} else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight
//Load xib for landscapeRight
{
This would allow you to style the orientations differently.
Good Luck,
Arnav
Source:
https://stackoverflow.com/a/38629833/8503080
How can i make a view like this. I am done with first portrait mode and confuse to to achieve the landscape mode.? can anybody suggest me how to make the view in landscape mode. I have done portrait mode in AnyHeight AnyWidth format.
Although the question might be a little broad, I'll try to mention a generic answer.
Assuming that your are already familiar with Auto Layout and Size Classes, in Xcode 8, Interface Builder you can change the current view of what are you working on:
By tapping the "View as:" button, you will be able to see the desired device orientation to setup the desired layout via device configuration bar:
For example, Consider the following View:
As yo can see, both of the views have the desired constraints to be displayed in portrait mode, for now, you should add add new constraints for the landscape mode. Make sure to deinstall the constraints for portrait mode:
As you can see, some of the constraints appears faded, which means that they are uninstalled on this size class. You can determine installing the constraint by selecting it and from the attribute inspector, check the desired size class(es):
For more details about how you can achieve this, I would suggest to watch Making Apps Adaptive, Part 1 Session.
Hope this helped.
I am trying to wrap my head around the new size classes in iOS8 and XCode6. I am attempting to create a nib, without a storyboard, and do something really simple--center a UIView on the screen.
Starting with a nib in the default size: w:Regular h:Regular, I place a 200x200 UIView onto the parent view and center it, then add contraints to pin it. In my preview pane, the UIView only shows up on the iPad view.
I need for this view to show up on iPads in lanscape and iPhones in portrait. I suppose some day i will understand why Apple thinks this is so much easier, but at the moment I'm bewildered.
When I switch my design view to any of the iPhone supported modes, the UIView disappears.
Even when I unpin the UIView and move it around in the design view, it never shows up in the iPhone view. In other words, I've tried everything to get this view to appear on an iPhone and nothing works.
Here is my UIView and settings:
Obviously there is something I'm missing, but I'm getting really frustrated trying to figure it out. Can anyone offer a clue? Thanks!
It's because you defined your view only for the size classes Regular & Regular. For them to show up on the iPhone you will have to configure the view also for the appropriate size classes, which in case of the iPhone is Regular & Compact where (<height> & <width>).
UPDATE:
The views generally only show up in the size classes for which they are configured. You currently have Regular & Regular selected, this can be seen from your screenshot. This means you defined it only for the devices with size classes Regular & Regular, which is the iPad in both orientations.
You need to add the views also in the size class that you are targeting, which are the size classes of the iPhone.
I built a keyboard for iOS 8 using one xib file and multiple subviews. However this keyboard only works in portrait mode and when the device rotates, the keyboard doesn't change its size or length. I figured I need to create another xib file with subviews for landscape and when the device rotates have the xib files switch. My question is how would I go about accomplishing this? How would I make it so that the program knows when to switch xib files. I was thinking I should use something like what is proposed in here: https://stackoverflow.com/a/25222353/2057171 but I do not know how I would implement it. Any help is appreciated.
One xib for both portrait and landscape
It's possible to have only one xib for both portrait and landscape, if you enable autolayout on your views in your xib file. These are the steps I did in my own test keyboard.
Enable and setup autolayout on the views in the xib file
Load and add that subview to the "inputView"
Programmatically setup the constraints for this subview in relation to inputView
"inputView" automatically changes sizes when switching between portrait and landscape, so when you have constraints setup for your view in relation to "inputView", your view will adjust automatically.
Separate xib for portrait and landscape
If you do decide to have separate xib files for your portrait and landscape views, you might need to load and add the correct subview to inputView whenever the orientation changes. I haven't done this personally but I believe updateViewConstraints() is called whenever the orientation changes so you can implement your logic there.
So the problem here is I need a totally different layout in landscape and portrait. For instance, I have a view that in portrait is on top but in landscape is on right of the screen.
I am currently using hard coded frames and I will change views' frames in "viewWillLayoutSubviews" but I don't like this hard coding, is there any way to achieve the same thing in Interface Builder ?
Thanks,
Try implementing updateViewConstraints to first removeConstraints then addConstraints for either portrait or landscape depending on the orientation.