繁体   English   中英

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

[英]How to manage application when orientation changes?

我有一个应用程序,我正在改变屏幕方向。 但是当我更改第一个屏幕的方向并转到下一个屏幕时,更改不会持续存在。

随附图像以便更清楚地理解。

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

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


主要问题是当设备旋转时,第一个屏幕旋转但第二个屏幕没有以新方向启动。
只有在屏幕启动后我们改变方向时它才会旋转。

我该如何解决这个问题?

问题是当你旋转设备时

  (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

方法被调用但是当你进入下一个屏幕时,这个方法不会被调用。 如果再次旋转设备,则会调用该方法。

因此,如果要将下一个屏幕方向更改为设备当前方向,请使用viewDidLoad方法或ViewWillApper()方法检查设备的当前方向。 然后根据那个设置你当前的观点。

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;
}

快乐的编码..

叫这个方法。

-(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