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

时间:2011-11-11 05:31:34

标签: iphone uiinterfaceorientation

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

附上图片以便更清楚地理解。

first screen of view in portrait mode first screen of view in landscape mode

second screen of view in landscape mode second screen of view in portrait mode


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

我该如何解决?

3 个答案:

答案 0 :(得分:6)

问题在于旋转设备时

  (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

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

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

答案 1 :(得分:1)

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

快乐编码..

答案 2 :(得分:0)

调用此方法。

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