2012-01-10 59 views
1

我创建了一个支持所有方向的应用程序。当在横向模式下启动应用程序时,UIOrientation不起作用

另外我在viewDidLoad方法创建的表和搜索栏(不使用XIB)。我还写了下面的代码来调整表格视图和搜索栏的位置。

它可以正常工作,当我在纵向视图启动我的应用程序,当我旋转到横向视图,它完美地调整意见。

但是,当我启动我在横向模式下的应用程序,视图不作调整,相反,他们采取纵向视图的定位。但再次当我旋转肖像,然后风景它工作正常。

也保证了正确的方向被捕获后,我写了NSLog这个方法里面,它让我看到正确的值。

为了确保明确调用此方法(adjustViewsForOrientation),我也调用了viewDidLoad中的方法。

[self adjustViewsForOrientation:self.interfaceOrientation]; 

下面是代码片段的休息:

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation { 
    if (orientation == UIInterfaceOrientationLandscapeLeft || 
      orientation == UIInterfaceOrientationLandscapeRight) { 
     aTableView.frame = CGRectMake(705, 220, 320, 280); 
     sBar.frame=CGRectMake(805, 0, 220, 40); 
     NSLog(@"inside landscape");   
    } 
    else if (orientation == UIInterfaceOrientationPortrait || 
       orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     aTableView.frame = CGRectMake(450, 220, 320, 280); 
     sBar.frame=CGRectMake(550,3,220,40); 
    } 
} 

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
           duration:(NSTimeInterval)duration { 
    // [self adjustViewsForOrientation] 
    [self adjustViewsForOrientation:toInterfaceOrientation]; 
    NSLog(@"landscap"); 
} 
+0

可以打印出的NSLog(@ “来定位:%d”,toInterfaceOrientation);并向我们​​展示结果? – 2012-01-10 15:08:17

+0

A的副本 - http://stackoverflow.com/questions/8775911/landscapeorientation-on-start-of-didload-method-in-objective-c/8775924#8775924 – rishi 2012-01-10 16:02:28

回答

0

试试这个办法,它可以帮助。在viewDidLoad方法注册为UIDeviceOrientationDidChange:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adjustViewsForOrientation:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

,改变-(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation

-(void) adjustViewsForOrientation:(NSNotification *)notification

和通过获取取向值

例如获得新的定向UIDeviceOrientation newDeviceOrientation = [notification orientation];

相关问题