How to rotate a single view when iPad rotate - ios

I work on an app on iPad with iOS 7. The main UIView (viewA) of this app is by construction always landscape. Inside the main view there is another small UIView (viewB). Both are controlled by the same mainViewController, that has this method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Starting with iPad in landscape orientation, when I rotate the iPad in the portrait position, viewA and view B remains as they are. All right.
Now I want to make a change. To be precise, I want that, when iPad rotate from landscape to portrait, viewA remains as before, but viewB rotate automatically. How can I do? Have I to make a separate ViewController for viewB?
Thank you.

You need to extend/inherit the ViewController and override shouldAutorotateToInterfaceOrientation method to return the proper orientation for view B
Example ViewControllerB.h:
#interface ViewControllerB : ViewControllerA {
}
ViewControllerB.m:
#implementation ViewControllerB
- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation {
return true;
}
#end

Related

Rotate to Landscape for a view - iOS

I have used the below code to force the view to landscape.
objc_msgSend([UIDevice currentDevice], #selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);
Is apple approve this for orientation or have any chance to reject my app.Please provide your solution.
I do not think so. Apple documentation state that orientation is read-only property.
To force landscape into ViewController, you can do:
- (NSUInteger)supportedInterfaceOrientations;
{
return UIInterfaceOrientationMaskLandscape;
}
To present the ViewController in a specified orientation mode:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
{
return UIInterfaceOrientationLandscapeLeft;
}
For all these to happen, the plist of the project must support the orientation and:
- (BOOL)shouldAutorotate;
{
return YES;
}
All these is assuming the view controller is not presented under same UINavigationController. For such case, you should refer to this discussion on work around of forcing rotation on UINavigationController: UINavigationController Force Rotate
For views that you want to force into landscape, you can always use transform:
self.view.transform = CGAffineTransformMakeRotation(M_PI/2.0);

return back from B VIewcontroller(Fixed Portrait), to A , A is not rotating to landscape mode automatically(Device is in Landscape Mode)

I hold device in landscape mode and moved into second view controller, which only support portrait mode. when return back from second view controller(Support all orientation), First view controller is not rotating to landscape mode automatically.
if i did below code then its work in IOS6. but not working for IOS7.
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
[UIViewController attemptRotationToDeviceOrientation];
}
in IOS7 viewController is rotating but status bar is not rotating
Implement the following in VC A:
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
The solution presented by AMayes is not quite correct.
The UIViewController docs state the following:
Typically, the system calls this method only on the root view
controller of the window or a view controller presented to fill the
entire screen; child view controllers use the portion of the window
provided for them by their parent view controller and no longer
participate directly in decisions about what rotations are supported.
Therefore, put the following code in the UINavigationController subclass:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}

iOS 6 controlling autorotation of view controllers behind UIImagePickerController

My iPad app has a UIViewController called MainViewController which presents a modal page sheet UIViewController called ModalViewController. ModalViewController then presents a UIImagePickerController.
Here is MainViewController.m:
#implementation MainViewController {
...
- (BOOL)shouldAutorotate
{
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
...
}
So it rotates to either of the landscape orientations, and when ModalViewController is presented it rotates that correctly also.
However when the UIImagePickerController is presented, it allows rotation to landscape or portrait orientations, and this allows MainViewController and ModalViewController to rotate to un-desired orientations.
How can I prevent MainViewController and ModalViewController from rotating when the UIImagePickerController is on the screen?
Is this the same problem asked here Stop a modal UIViewController from rotating ? If you're using a UINavigationController in the end you can subclass it and control the mechanism for rotation

ios 6.0 rotation for only some viewController in a UINavigationController?

I have a UINavigationController with a first ViewController that is a UITabBarController, that should not be rotating...
Then pushed UIViewController should rotate...
So far I have subclassed the UINavigationController and implemented those method :
- (BOOL) shouldAutorotate {
return [self.visibleViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
return [self.visibleViewController supportedInterfaceOrientations];
}
So it is the child controller that choose if it should autorotate...
I have so far managed to block rotation for UITabBarController and allow rotation for the pushed UIViewController.
The Only thing is, if the UIViewController is in landscape mode, and when I pop it, the UITabBarController will be in Landscape mode too, until the phone is put on the portrait mode, it will come back to normal and not rotate anymore...
I would like that when I pop the Landscape UIViewController, that the UITabBarController is already on portrait mode.
This new iOS 6.0 UI rotation management seems to be a pain !
As you rightly say, the device has not been rotated, so the revealed view controller does not insist on rotating the orientation when the old view controller is popped. If that's the behavior you want, use a presented view controller instead.
You can force interface rotation by rotating the status bar, but only if supportedInterfaceOrientations returns 0.

iOS 6 Orientation with NavigationController

I am currently developing an application that works with iOS 5 and iOS 6.
Most of my views are only on Portrait orientation except for 1.
RotationNavigationController : Main UINavigationController that overrides supportedInterfaceOrientation and shouldAutorotate.
PageViewController : Pushed in RotationNavigationController and is displayed in Portrait orientation only.
ImageViewController : Pushed after PageViewController. Is displayed with UIInterfaceOrientationMaskAllButUpsideDown.
Here's what I have in the ImageViewController's ViewDidLoad
- (void)viewDidLoad
{
// currentMask is the value returned by supportedInterfaceOrientation
[(RotationNavigationController*)self.navigationController setCurrentMask:UIInterfaceOrientationMaskAllButUpsideDown];
[(RotationNavigationController*)self.navigationController setShouldAutorotate:YES];
}
And when I popViewController from ImageViewController in landscape, I get back to PageViewController in landscape mode too whereas PageViewController only supports Portrait orientation.
Of course, I reset the mask in the ImageViewController's viewWillDisappear to Portrait.
Is there a way for PageViewController to remain in Portrait orientation ?
Thanks for the link emrys57. I found out the solution down there, and it was quite trivial in the end :
In RotationNavigationController, just add the following method :
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}

Resources