2011-11-07 68 views
0

我做它同时支持横向模式和肖像模式的一个项目,但现在我打有我project.I没有orentation设置如何在iPhone中设置设备方向?

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

,但没有用,当我按命令+右 - 箭头为向右旋转它旋转到左边,但视图和控制器没有改变到orentation.Then一些谷歌上搜索后,我得到这个代码

if (self.interfaceOrientation == UIInterfaceOrientationPortrait) { 
    // do stuff 
} 

但问题是我不知道如何写代码/ /做东西。请帮助我做到这一点。 在此先感谢。 编辑: 我把这个代码,它是工作,但whenthe模拟器再次变为纵向它不会是在默认mode.my代码

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
     if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)) 
     { 
      NSLog(@"Csantos shouldAutorotateToInterfaceOrientation: left or right"); 
      // 
      table.transform = CGAffineTransformIdentity; 
      table.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); 
      table.bounds = CGRectMake(0.0, 0.0, 320, 402);//[self.view setFrame:CGRectMake(0.0, 0.0, 768, 90)]; 
     } 
     else 
     { 
      table.transform = CGAffineTransformIdentity; 
      table.transform = CGAffineTransformMakeRotation(degreesToRadian(360)); 
      table.bounds = CGRectMake(0.0, 51, 320, 402); 
     } 
       return YES; 
    }*/ 
+0

可能的重复:http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation – Dair

回答

1
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration; 
{ 
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) 
    { 
     //Handle portrait 
    } 
    else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) 
    { 
     //Handle landscape 
    } 
} 

这是更好的代码。

+0

我把这里面的风景取向,但没有做到自我。 view.transform = CGAffineTransformIdentity; self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); self.view.bounds = CGRectMake(0.0,0.0,480,320); – ICoder

+0

您不必旋转......所有控件都会自动更改。你只需要设置他们的框架。在“shouldAutorotateToInterfaceOrientation”方法中,您只需返回yes。并将框架设置为“willRotateToInterfaceOrientation” – Satyam

0

你可以这样做。

if (UIInterfaceOrientationPortrait == interfaceOrientation || UIInterfaceOrientationLandscapeLeft == interfaceOrientation || UIInterfaceOrientationLandscapeRight == interfaceOrientation || 
    UIInterfaceOrientationPortraitUpsideDown == interfaceOrientation) { 
    return YES; 
}