iOS best way to change the UI when orientation is changed - ios

Which is the best way to change the UI when orientation is changed?
For example, use two different UIView one portrait and a landscape and show one of both if orientation is changed, or use one UIView and change the UI control sizes and positions?
Any other ideas?

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
NSLog(#"Change to custom UI for landscape");
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(#"Change to custom UI for portrait");
}
}

I always recommend using the autoresizingmask for all the subviews in the view controller view. With this being set correctly all the views will resize automatically from the orientation and you don't need the extra rotation specific subviews (one portrait view and one landscape view).

Related

View drawing correctly when changing orientation, but fails on first display

I have a UIViewController in iOS 9 that does not draw correctly when the view appears in landscape mode.
However if the view appears in portrait mode, and then is changed to landscape everything is fine.
This happens both on the simulator and devices. I am using Interface Builder to set the heights and constraints of the subviews. I do not do any resizing of frame sizes or updates to constraints in code.
So I tried to force a redraw similar to a change of orientation, but I could not get it to work and I am not sure if this is the best approach.
Does anyone have an idea how to solve this?
In your ViewControllers, you will need to override the shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) method to return YES when the app should rotate:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

Set different constraints for iPad in portrait and landscape mode

When the device is in portrait orientation I have tableView and graphView in a viewController. When i change to landscape mode, i want to display only graphView. I am using size classes to do it. It works for all iPhones.
But for iPad, Size classes for portrait and landscape are same (regular width and height).
I need to set different constraints for portrait and landscape mode.How do i remove tableView when the device is in landscape.
This code may help you:
- (void) handleOrientation:(UIInterfaceOrientation) orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//handle the portrait view
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//handle the landscape view
//write your code for removing Table
}
}

How to hide a TextView in iOS-landscape

I am a newbie in iOS-programming (trying to convert my android-app to iOS).
I am using the storyboard and want to create a view, which has on the top a scrollview (with an ImageView inside) and on the bottom there is a TextView. This should be the View, if the users use the portrait-orientation.
Now I want that the ViewCOntroller in the landscape-orientation only shows the scrollview and the imageviews (that are inside the scrollview). The textview should be hidden in landscape.
Is it possible to make the images bigger, but hold the aspect ratio ( in android:adjustViewBounds) ?
How can I handle this? I've tried it by using the "autosizing", but the result wasn't good.
Apple provides a method that is called when the app will change its orientation.
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[textview setHidden:YES];
} else {
[textview setHidden:NO];
}
}
Just add it to your view controller. Check more on Apple's reference

lock and unlock status bar rotation based on a condition

I simply want to lock rotation for the status bar when some conditions are fulfilled:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
if(imageView.hidden == NO){
//lock the status bar rotation
}else{
//unlock the status bar rotation
}
}
}
Anyway to lock/unlock the status bar programmatically? Thanx.
You need to override the proper methods in your view controller class. In iOS 6+ you need to override supportedInterfaceOrientations. If you also need to support iOS 5 also override shouldAutorotateToInterfaceOrientation:.
Both implementations should return appropriate values on the currently allowed orientations.

Why won't my views rotate?

I have a Tab Bar application for iPad, created using the basic Tab Bar template. I have added some custom view controllers (one for each tab, each with a corresponding NIB) and also some extra view controllers with NIBs to be used as modal views. Everything works great until I rotate the device.
My app only supports portrait orientation, so I had this in all my view controllers:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIDeviceOrientationLandscapeLeft) &&
(interfaceOrientation != UIDeviceOrientationLandscapeRight);
}
However, the app would not rotate in the simulator or the device when turned upside down. I double and triple checked that all my view controllers had the above code.
I went through all my NIBs and checked that they all have "Rotate Subviews" ticked. I haven't changed any of the NIB settings from the defaults anyway, apart from the basic things needed to get them showing in the tab views.
I tried changing the code in all my view controllers to this:
- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
It made no difference. I have made absolutely sure that the same method is being used in all the view controllers. I don't know what else I can do. I can see no reason why it shouldn't rotate to the upside down view.
Any help with this would be much appreciated.
Got it! One of my View Controllers was not hooked up to the relevant tab in IB. As I hadn't added the images or written the code for that View Controller yet, I didn't notice that it wasn't associated in IB. I had done the shouldAutorotateToInterfaceOrientation method, but it seems that didn't take effect until the connection was made in IB.
Thanks very much for suggestions on this. That's a highly frustrating problem now dealt with!
Also, this is Apple's very helpful guide: http://developer.apple.com/library/ios/#qa/qa1688/_index.html
In my case - I forgot to call self = [super initWithNibName ....]!
Does "all your view controllers" include the Tab Bar Controller?
In tab bar apps that is the only view controller who's shouldAutoRotateToInterfaceOrientation is called and evaluated at all.
The first snippet you have is logically incorrect:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return (orientation != UIDeviceOrientationLandscapeLeft) &&
(orientation != UIDeviceOrientationLandscapeRight);
}
Here, orientation is an instance of UIInterfaceOrientation whereas UIDeviceOrientationLandscapeLeft is an instance of UIDeviceOrientation. The two are not the same type and so should not be compared.
Instead, you should use the UIInterfaceOrientation options:
typedef enum {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeLeft,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeRight
} UIInterfaceOrientation;
Change the method to
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return (orientation == UIInterfaceOrientationLandscapeLeft ||
orientation == UIInterfaceOrientationLandscapeRight);
}
(the code seems to me more readable when put in the affirmative rather than negative)

Resources