简体   繁体   中英

How to manage application when orientation changes?

I have an application in which I am changing screen orientation. But when I change orientation for the first screen and go to the next screen, the change does not persist.

Attached are images for clearer understanding.

纵向模式下的第一个视图屏幕横向模式下的第一个视图屏幕

横向模式下的第二个视图屏幕纵向模式下的第二个视图屏幕


The main problem is that when device is rotated then first screen rotates but second does not launch in the new orientation.
It will rotate only when we change orientation after the screen is launched.

How do I fix that?

The problem is that when you rotate the device then

  (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

Method gets called but when you go to the next screen then this method does not get called. if you rotate your device again then that method gets called.

So if you want to change next screen orientation as device current orientation, then check the device's current orientation with viewDidLoad method or ViewWillApper() method. Then according to that set your current view.

Use this buddy...

If suppose, you are adding UIImageView to your self.view as

**in .h**, 

UIImageView *topView

**in .m**,

    topView = [[UIImageView alloc] initWithImage:
                [UIImage imageNamed:@“anyImage.png"]];
    topView.userInteractionEnabled = YES;

   UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
       topView.frame = // set your dimensions as per landscape view
    }
else if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
       topView.frame = // set your dimensions as per portrait view

}


    [self.view addSubView : topView];

**Add this lines at end of your viewDidLoad.**

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                    selector:@selector(didRotate:)
                    name:@"UIDeviceOrientationDidChangeNotification"
                               object:nil];



**And the method**

     - (void)didRotate:(NSNotification *)notification 
    {
    UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    CGRect screenBounds = [[UIScreen mainScreen] bounds];

    switch (orientation) 
    {
        case UIInterfaceOrientationLandscapeLeft:
        {
            topView.frame = // set your dimensions as per landscape view
            break;
        }
        case UIInterfaceOrientationLandscapeRight:
        {
            topView.frame = // set your dimensions as per landscape view
            break;
        }
        case UIInterfaceOrientationPortraitUpsideDown:
        {
            topView.frame = // set your dimensions as per portrait view
            break;
        }
        case UIInterfaceOrientationPortrait:
        {
            topView.frame = // set your dimensions as per portrait view
            break;
        }        
    }
}


    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

Happy coding..

call this method.

-(void) viewWillAppear:(BOOL)animated{
    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

    [self willAnimateRotationToInterfaceOrientation:orientation duration:0];

}

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown )
    {


    enter code here
    set frame.........
    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight )
    {

        set frame.........

    }


}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM