2012-06-19 83 views

回答

3

禁用取向该视图(假设第一视图是横向)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ 

    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)||(interfaceOrientation == UIInterfaceOrientationLandscapeRight); } 

然后将其添加到viewDidAppear

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

并添加此方法某处

- (void) orientationChanged:(NSNotification *)note 
{ 
    UIDevice * device = note.object; 
    switch(device.orientation) 
    { 
     case UIDeviceOrientationPortrait: 
      // Present View Controller here 
      break; 
     default: 
      break; 
    }; 
} 

在另一种观点上做同样的事情,但是对于风景画来说,这种做法是背道而驰的现在的肖像的肖像。

不要忘记取消注册通知。

(或者使用带有两个控件,但没有酒吧导航视图,只是表明你想要这取决于使用的方位之一)

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
             duration:(NSTimeInterval)duration 
+0

会在哪里我把代码注销了notifcations?在viewDidDisapear中? – Whyrusleeping

+0

是或willdissapear。我忘了提及这种方法是为了当你得到一个复杂的布局,它有点难以管理,但它不应该干扰你平时的应用旋转行为。 – Pochi

+0

我需要用来取消注册通知的唯一代码是[[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];'right? – Whyrusleeping

1

首先阅读UIViewControllers是如何工作的: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

然后,在你的UIViewController子类,利用willRotateToInterfaceOrientation:duration:优势,改变你的看法。

例如

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    // if portrait 
    [self.landscapeView removeFromSuperview]; 
    [self.view addSubview:self.portraitView]; 

    // if landscape 
    [self.portraitView removeFromSuperview]; 
    [self.view addSubview:self.landscapeView]; 
} 

,并在适当if陈述或switch情况下添加,以确定哪些做。