3 回答

TA貢獻1802條經驗 獲得超5個贊
您必須加載一個視圖,然后檢查方向并在需要時加載另一個視圖。shouldAutorotateToInterfaceOrientation:如果要旋轉,則返回“是”來檢查方向。
我使用導航控制器來管理過渡。如果我具有人像視圖并且設備旋轉,則我推動風景視圖,然后在返回肖像時彈出風景視圖。
編輯:
我應該在shouldAutorotateToInterfaceOrientation中為所有方向返回YES,但是在應用啟動時會調用此方法嗎?您是否將視圖推入此功能?
方向常數不是您查詢的全局變量,而是系統發送給控制器的消息的一部分。因此,在加載視圖控制器之前,您無法輕松檢測方向。相反,您可以硬接線該應用程序,使其以特定的方向(通常是縱向)開始,然后立即旋轉。(請參閱移動Safari。它始終以縱向開始,然后旋轉為橫向。)
這是我用來交換人像和風景視圖的兩種方法。
所有三個視圖控制器都具有此方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
肖像有:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
[self.nav pushViewController:rightLVC animated:NO];
}
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
[self.nav pushViewController:leftLVC animated:NO];
}
}
每個景觀控制器具有以下功能:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
[self.nav popViewControllerAnimated:NO];
}
該應用程序以縱向啟動。如果設備的方向為橫向,則按適當的橫向。當設備旋轉回縱向時,它將彈出風景。對于用戶而言,它看起來像是同一視圖將其自身重組為不同的方向。

TA貢獻1810條經驗 獲得超5個贊
我稍加修改,以允許在肖像或風景中初步裝入筆尖
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
? ? if( !nibNameOrNil )? ? ?nibNameOrNil = [self nibNameRotated:[[UIApplication sharedApplication] statusBarOrientation]];
? ? self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
? ? return self;
}
- (NSString*) nibNameRotated:(UIInterfaceOrientation)orientation
{
? ? if( UIInterfaceOrientationIsLandscape(orientation))? ? ?return [NSString stringWithFormat:@"%@-landscape", NSStringFromClass([self class])];
? ? return [NSString stringWithFormat:@"%@", NSStringFromClass([self class])];
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
? ? NSString *nibname = [self nibNameRotated:toInterfaceOrientation];
? ? [[NSBundle mainBundle] loadNibNamed:nibname owner:self options:nil];
? ? [self viewDidLoad];
}
- 3 回答
- 0 關注
- 529 瀏覽
添加回答
舉報