简体   繁体   English

如何在方向改变时管理应用程序?

[英]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. 因此,如果要将下一个屏幕方向更改为设备当前方向,请使用viewDidLoad方法或ViewWillApper()方法检查设备的当前方向。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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