2017-04-20 45 views
5

我的應用程序支持縱向和橫向方向。我從位於的UITabBarController使用以下代碼一個UIViewController模態呈現一個UIViewController:UIViewController以橫向模式呈現時無法正常顯示

self.modalViewController = [[ModalViewController alloc] init]; 

[self presentViewController:self.modalViewController animated:YES completion:^{ 

}]; 

的ModalViewController是應該僅通過在景觀用戶看到的控制器。它應該能夠將LandscapeRight旋轉到LandscapeLeft。這是它的樣子:

@implementation ModalViewController 

#pragma mark - UIViewController - Orientation 

- (UIInterfaceOrientationMask)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

#pragma mark - NSObject 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor greenColor]; 
} 

#pragma mark - UIViewController - Status Bar 

- (BOOL)prefersStatusBarHidden 
{ 
    return YES; 
} 

@end 

控制器從左側向上滑動,並在95%的時間內完全覆蓋屏幕。但有5%的時間,它從底部向上滑動,只覆蓋屏幕的上半部分。

下面是當它工作正常,它的外觀:

enter image description here

而這裏的時候它不做工精細它的外觀:

enter image description here

我創建了一個示例項目,可以在這裏找到:https://github.com/TitouanVanBelle/Bug

有一個用戶界面測試目標,他lp重現了這個問題,但它很少有效。

有誰知道爲什麼會發生這種情況?

+1

感謝張貼這個,也尋求解決方案 – sage444

回答

0

您的應用程序支持縱向,左側橫向和橫向右側方向。

在ModalViewController.m更新下面的方法 -

- (UIInterfaceOrientationMask)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationMaskPortrait; 
} 

你ModelViewController只支持橫向模式,因此這個問題。

而不是UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait可以使用UIInterfaceOrientationMaskAllButUpsideDown爲好。

編輯1

按照您的要求,您可以通過在ModalViewController.m更新下面的代碼強制旋轉當前方向。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor greenColor]; 

    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; 
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
} 
+0

但是,然後用戶將能夠在橫向和肖像顯示ModalViewController,我不想這樣。我只希望他們能夠在橫向模式下看到它 –

+0

啊......從你的問題中不清楚。給我一些時間我會更新我的答案 –

+0

好的。將編輯該問題。 –

0

這應該是一個評論,但我沒有足夠的聲望,所以回答是。

我想我看過類似的錯誤。

問題的原因可能是在轉換過程中方向會發生變化,以至於當調用viewDidLoad時它仍然是縱向,但隨後更改爲橫向並且適當的偵聽器未能正確響應(可能是因爲過渡動畫狀態爲「進行中」?)。

關於如何解決它的一些想法:

  • 聽取向更改通知,並明確調用self.view.setNeedsLayout當電流方向是應該自轉返回false,如果目前的方向是縱向
  • 景觀

希望它有幫助。

+0

我已經嘗試在shouldAutorotate中返回NO,但它不能解決問題。我會嘗試第一個解決方案。謝謝 –

+0

即使調用[self.view setNeedsLayout],也能夠重現該問題; –

0

我只是試圖實現相同。我從你的問題中瞭解到,你只想在橫向模式下顯示模型VC。這是解決方案

1)添加演示文稿樣式到你的模型VC,所以它不會顯示從底部或半屏幕。更換FirstViewController代碼以下列

-(void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated];  
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), 
    dispatch_get_main_queue(), ^{ 
     self.modalViewController = [[ModalViewController alloc] init]; 
     self.modalViewController.modalPresentationStyle = UIModalPresentationFullScreen; 

     NSAssert([NSThread currentThread] == [NSThread mainThread], @"FAIL"); 

     [self presentViewController:self.modalViewController animated:YES completion:^{}]; 
    }); 
} 

2)在模型VC改變ovveriden方法supportedInterfaceOrientations此

-(UIInterfaceOrientationMask)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; 
} 

3)爲了得到旋轉的清晰的概念的模型VC添加視圖所以這將是清楚視圖是否旋轉。添加以下代碼型號VC

-(void)viewDidLoad  
{ 
    [super viewDidLoad]; 
    self.view.backgroundColor = [UIColor greenColor]; 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 100)]; 
    view.backgroundColor = [UIColor redColor]; 
    [self.view addSubview:view]; 
} 

現在做的這一切,如果你試圖打開該應用會在橫向模式下打開模型VC只可旋轉無論是在景觀左或右,但potrait模式會後禁用。

+0

該解決方案仍然可以重現問題。 –

相關問題