当屏幕方向改变时如何管理应用程序?

3
我有一个应用程序,我正在更改屏幕方向。但是当我更改第一个屏幕的方向并转到下一个屏幕时,更改不会持续存在。
以下是更清晰理解的图片。
第一个屏幕以纵向模式显示的图像 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个回答

6
问题是当您旋转设备时,问题就出现了。
  (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

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

因此,如果您希望将下一个屏幕的方向更改为设备的当前方向,请使用viewDidLoad方法或ViewWillApper()方法检查设备的当前方向。然后根据该设置您的当前视图。


它自iOS6起已被弃用。 - Darius Miliauskas

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

编程愉快..


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

谢谢你的建议,但是当我从第一个屏幕到第二个屏幕时,如果我将屏幕旋转到右横向,则右侧将会保留一定的空白。那么如何将该间隙从屏幕中移除? - ios
因此,设置图像视图和所有其他控件的框架。 - alok chauve

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接