[英]IOS: lock orientation in a UINavigationController
在我的應用中,我使用縱向導航控制器啟動了第二視圖控制器,然后在第四視圖控制器中完成了操作,在該控制器中,我應該旋轉iPhone以顯示圖像。 現在的問題是:如果在目標中以這種方式設置方向:
我可以使用此方法旋轉圖像:
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
並且它工作正常,但我無法鎖定所有屬於navigationcontroller的視圖,因為它們全部旋轉il縱向和橫向。
第二種選擇是以這種方式設置目標:
但是此結構無法檢測方向:
在視圖中將出現:
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
[self willRotateToInterfaceOrientation:orientation duration:1];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
}
else
{
}
}
else{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
}
else
{
}
}
}
最好的解決方案是什么? 謝謝
我禁不住覺得,如果我閱讀了文檔,那可以節省一些時間,但是對我來說,解決方案是將UINavigaionController 子類化並添加以下內容:
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
而且,由於這里實際上沒有發生任何實質性的事情,因此無需使用編碼器或其他任何方法覆蓋init。 您可以在以前引用導航控制器(包括Interface Builder)的任何地方引用該子類。
進行第一個配置。 並實施方法
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
適用於所有視圖控制器。 只允許所需的視圖控制器使用橫向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.