Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

iphone - iOS7 / iOS6 Conditional Rotation Portrait / Landscape for different sections of App

Problem: A have an App that uses both Landscape mode (locked) and Portrait Mode (locked) for different parts of the app. Now I have a working solution however it doesn't seem correct and does have it's own problems.

Optimally I would love to force a orientation change. Thinking even about doing a view transformation if needed.

Basic flow of App:

  • HomeView (Portrait) (which has a few sub pushed views that are also portrait and locked to that).
  • LandscapeView (Landscape) (which has 5 pushed subviews that are also landscape)

Note:

  • HomeView has a link to LandscapeView
  • LandscapeView can go back to HomeView
  • At the end of the LandscapeView subviews it returns to the HomeView

Basic Image showing how this looks with the different view orientations. (The lines indicate flow of app, orientation of the images indicate how each screen should be )

current view changes

Currently using the below implementation to call / set if the view is in portrait mode or landscape mode by [setLockedToPortait:YES] (for portrait view) etc.

This in term makes the query for what interface orientation to use from iOS if the device is rotated.

Now for the case of going to the LandscapeView, I show a temporary view over the top of the normal view asking to use to rotate their phone to landscape. (A temporary view is also shown when returning to the HomeView from a landscape view)

So once the user has rotated their device, it will trigger the correct orientation and then the temporary view will hide.

If the user then rotates their phone back to portrait at this point it will still be locked to landscape so will not trigger another view rotation (also no temp view will appear or anything)


Current Implementation Code::

// ---------------------- NavigationController (subclass of UINavigationController)
@interface NavigationController () {
BOOL isOrientationPortrait;
}
@end


@implementation NavigationController {
       UIDeviceOrientation lastAccepted;
       UIDeviceOrientation lastKnown;
 }

-(void)setLockedToPortait:(BOOL)isLocked {
    isOrientationPortrait = isLocked;
}

-(UIDeviceOrientation) getCurrentOrientation {
UIDeviceOrientation orientate = [[UIDevice currentDevice] orientation];
if(orientate == 0) { // needed for simulator
    orientate = (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation;
}
return orientate;
}

// Deprecated in iOS6, still needed for iOS5 support.
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
{
UIDeviceOrientation orientation = [self getCurrentOrientation];
[self setLastKnownOrientation:orientation];

if(isOrientationPortrait == YES) {
    if([self isLastKnownPortrait] == YES) {
        [self setLastAcceptedOrientation:orientation];
        return YES;
    } else {
        return NO;
    }
} else {
    if([self isLastKnownLandscape] == YES) {
        [self setLastAcceptedOrientation:orientation];
        return YES;
    } else {
        return NO;
    }
}
}

// iOS6/7 support
- (BOOL)shouldAutorotate
{
    // find out the current device orientation
UIDeviceOrientation orientation = [self getCurrentOrientation];
[self setLastKnownOrientation:orientation];
return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
if(isOrientationPortrait == YES) {
    if([self isLastKnownPortrait] == YES)
    {
        UIDeviceOrientation orientation = [self getCurrentOrientation];
        [self setLastAcceptedOrientation:orientation];
    }
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
} else {
    if([self isLastKnownLandscape] == YES)
    {
        UIDeviceOrientation orientation = [self getCurrentOrientation];
        [self setLastAcceptedOrientation:orientation];
    }
    return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight );
}
}
-(void)setLastAcceptedOrientation:(UIDeviceOrientation)orient  {
lastAccepted = orient;
}

-(void)setLastKnownOrientation:(UIDeviceOrientation)orient {
    lastKnown = orient;
}

-(BOOL)isLastKnownPortrait {
    return UIDeviceOrientationIsPortrait(lastKnown);
}

-(BOOL)isLastKnownLandscape {
    return UIDeviceOrientationIsLandscape(lastKnown);
}

-(BOOL)isLastAcceptedPortrait {
    return UIDeviceOrientationIsPortrait(lastAccepted);
}

-(BOOL)isLastAcceptedLandscape {
    return UIDeviceOrientationIsLandscape(lastAccepted);
}

Current Problems:

  • Device rotations are always required after a view has loaded for the user going to Landscape mode from Portrait and vice versa.
  • If the user has the device orientation locked, this will not work at all.
  • When transitioning back from Landscape mode, and the user has already rotated their device to Portrait (in the last landscape view), the Portrait view's interface will be locked to a 'Landscape' layout until the user re-rotates their device (so currently I am just showing the overlay to rotate the device, but it is already rotated… very annoying for the user). Massive issue right now with the above implementation.

Would love to be able to:

  • Force an orientation change on the phone for the current view.
  • Set a preferred layout for a view which is forced between push/pops of views.

I've looked a lot at the other solutions on here and on the Apple Dev forums, however none seem to cover this problem, or still this orientation bug between the two views exists as well.

Thanks for any help or pointers! No advice will be discounted :D

-- Edit::

Solution Found thanks to @leo-natan!!

So instead of trying to force a change of orientation on the views. Just push a new modal view. This forces a change. You still need to above orientation code for managing rotations.

So what I have now in my HomeViewController:

LandscapeViewController * viewController = [[[LandscapeViewController ViewController alloc] init] autorelease];
UINib * nib = [UINib nibWithNibName:@"NavigationController" bundle:nil];
NavigationController *navController = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
[navController initWithRootViewController:viewController];
[self presentViewController:navController animated:YES completion:^{
    // completion
}];

So it is necessary to re-add a new navigation controller for this modal view. Also note above 'presentViewController' is the new way of pushing Modal views.

Implemented this overloaded method for the managing of the view controller:

-(id)initWithRootViewController:(UIViewController *)rootViewController {

self = [super initWithRootViewController:rootViewController];
    if(self){

    }
return self;
}

Note: The above is not using storyboards. The problem may be solved by using storyboards and modally showing a view in the same fashion.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See my answer here, including a test project.

Basically, orientation can only be forced to change when presenting a view controller modally. For example, media playback in some apps. If you wish to transition from a view controller that can only be presented in portrait to a view controller that is only presented in landscape, you will need a modal presentation. Push will not work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...