2011-11-07 65 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; 
}