2012-08-07 17 views
3

可以說我有三個UI控制器(A,B,C)。父UIViewController方向不應改變後關閉子

A是我的根控制器,在ShouldAutoRotate方法中返回YES。 我做presentModalView從A到B(B =>在ShouldAutoRotate方法內我返回肖像),然後從B我presentModal C(C應該能夠旋轉到任何方向)。

現在在C裏面我可以將模擬器旋轉到任意方向,並且整個視圖可以完美旋轉。下面是問題,當C是橫向並且我忽略它時,B裏面的所有對象都會變得混亂! !同樣的事情發生在A.

我只需要有C上的旋轉!

感恩。

+0

我猜旋轉應視每個ViewControllers,一個視圖控制器的方向不應該影響其他人的,因爲他們是完全地不同的代碼之前。你確定你的shouldRotate方法寫得很好嗎 – doNotCheckMyBlog 2012-08-07 13:00:13

+0

我只是在裏面有「Return YES」。 – Danialzo 2012-08-07 13:11:25

回答

1

在應用程序委託

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
@property (nonatomic) BOOL shouldRotate; 
@end 

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ 
    if (self.shouldRotate == YES) { 
     return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait; 
    } 
    return UIInterfaceOrientationMaskPortrait; 
} 

在的viewController A,B

- (void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    ((AppDelegate *)[[UIApplication sharedApplication] delegate]).shouldRotate = NO; 
    [self supportedInterfaceOrientations]; 

    [self shouldAutorotate:UIInterfaceOrientationPortrait]; 

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; 
} 

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

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

-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

在的viewControllerÇ

- (void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    ((AppDelegate *)[[UIApplication sharedApplication] delegate]).shouldRotate = YES; 

} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

- (BOOL)shouldAutorotate:(UIInterfaceOrientation)interfaceOrientation{ 
    return YES; 

} 

-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; 
} 

- (IBAction)closeVC:(id)sender { 
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; 
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    appDelegate.shouldRotate = NO; 

    [self supportedInterfaceOrientations]; 

    [self shouldAutorotate:UIInterfaceOrientationPortrait]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

希望這能解決你的問題

0

你需要讓所有orientat從項目信息離子

enter image description here

,然後覆蓋所有實施這些方法的目標的iOS 6 +中的每個視圖控制器啓用和禁用取向。

supportedInterfaceOrientations 

shouldAutorotate 

shouldAutorotateToInterfaceOrientation 
0

力旋轉C到人像你關閉它

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; 
相關問題