Changing UIViews during UIInterfaceOrientation on iPad

拜拜、爱过 提交于 2019-12-11 11:03:54

问题


I am trying to change views on rotation because my views have to be significantly different from portrait to landscape. Now the code I am using works once then the app freezes when trying to rotate back. Either direction does not make a difference. For example: If I am in Landscape and rotate to portrait everything works great until I rotate back to landscape then it freezes and does absolutely nothing.

Here is the code I am using to achieve this

In my "viewDidLoad" method

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

Then I call this for the rotation:

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

- (void)didRotate:(NSNotification *)notification  
{          
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];  

    if ((orientation == UIDeviceOrientationLandscapeLeft) ||
        (orientation == UIDeviceOrientationLandscapeLeft))
    {  
        // present the other viewController, it's only viewable in landscape  
        [self.view addSubview:landScapeView];
    }

    if ((orientation == UIDeviceOrientationLandscapeRight) ||
        (orientation == UIDeviceOrientationLandscapeRight))
    {  
        // present the other viewController, it's only viewable in landscape  
        [self.view addSubview:landScapeView];
    }  
    else if ((orientation == UIDeviceOrientationPortrait ||
             (orientation == UIDeviceOrientationPortrait))
    {
        // get rid of the landscape controller  
        [self.view addSubview:portrait];
    }  
    else if ((orientation == UIDeviceOrientationPortraitUpsideDown ||
             (orientation == UIDeviceOrientationPortraitUpsideDown))
    {
        // get rid of the landscape controller  
        [self.view addSubview:portrait];
    }  
}

回答1:


You are adding your orientation specific view at the rotate but never removing the other one so you need.

// get rid of the landscape controller  
    ((orientation == UIDeviceOrientationPortraitUpsideDown || orientation == 
UIDeviceOrientationPortraitUpsideDown)) {

[landScapeView removeFromSuperview];      
[self.view addSubview:portrait];
    }  

And similar in the other direction.

Bear in mind that removing from superview releases so you need to have both views externally retained.



来源:https://stackoverflow.com/questions/6464569/changing-uiviews-during-uiinterfaceorientation-on-ipad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!