1

我在旋转设备时尝试更改视图布局时遇到了一些麻烦,我有一个名为 OverviewViewController 的主视图,它由 2 个视图组成,当设备处于某个方向时,它们将被隐藏/显示。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{    
     // do some layout stuff
     NSLog(@"OverviewViewController shouldAutorotateToInterfaceOrientation");
     return YES;
}

每次我旋转我的设备时,这个方法都会执行两次,但是在任何子视图中它都不会被调用,例如;

// In OverviewViewController

_landscapeView = [[LandscapeOverviewViewController alloc] initWithNibName:nil bundle:nil];
[self.view addSubview:_landscapeView.view];


// In LandscapeOverviewViewController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{    
    NSLog(@"LandscapeOverviewViewController: shouldAutorateToInterfaceOrientation");

    // Only change orientation when it's landscape
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) return YES;
    return NO;
}

子视图是否有理由不响应设备方向?该方法从未在那里被调用。

4

2 回答 2

3

As I can see you are adding LandscapeOverviewViewController view as a subView to self.view.
And you want your LandscapeOverviewViewController's shouldAutorotateToInterfaceOrientation to be called which will never happen.
As these are called to that controller which are pushed or presented not like you are doing.

Solution: You can do two things
1.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{    
     // do some layout stuff
     NSLog(@"OverviewViewController shouldAutorotateToInterfaceOrientation");
     //Call explicitly shouldAutorotateToInterfaceOrientation of LandscapeOverviewViewController
     [_landscapeView shouldAutorotateToInterfaceOrientation:interfaceOrientation];
     return YES;
}


2. You can register the orientation notification in LandscapeOverviewViewController

于 2012-07-30T03:36:12.547 回答
0

is it required for you to use two separate viewcontrollers (one for each orientation) within your overviewViewController?

if you are just altering the frame values based on orientation, then make sure to use autoresizingmasks.

if not, just use separate views for each of the orientations, and hide unhide each view as required.

currently the problem is, your lanscapeViewController has no one sending it the viewControllerEvents.

with the current setup, you can present these view controllers modally, with this code in the OverviewViewController

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration{
{    
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        [self presentModalViewController:LandscapeOverviewViewController animated: NO];
        }
    else{
       [self presentModalViewController:PortraitOverviewViewController animated: NO];
       }
}
于 2012-07-30T03:36:57.883 回答