簡體   English   中英

方向鎖定的ViewController

[英]ViewController with locked orientation

我遇到以下情況的問題:

ViewController A應該鎖定在橫向(左或右)上。 VC A的設置如下:

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotate {
    return NO;
}

ViewController B應該鎖定在肖像上。 其設置如下:

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate {
    return NO;
}

沒有導航控制器或選項卡欄。 它只是一個普通的視圖控制器,然后按一下按鈕,我從V​​iewController A轉到ViewController B

[self performSegueWithIdentifier:@"ViewControllerB" sender:self];

到目前為止一切都很好。 但是當我關閉ViewController B時,ViewController A出現在肖像上。 這就是我解雇VC B的方式:

[self dismissViewControllerAnimated:YES completion:nil];

另一個問題是:當我在ViewController B上並且人像可以時,我開始快速將設備從人像旋轉到橫向幾次,並且盡管上面有方向規則,但在某個點它還是旋轉到橫向。 我有什么想念的嗎?

viewDidAppear:嘗試使用此方法旋轉到所需的方向,以防控制器加載時方向錯誤:

NSNumber *value = @(UIInterfaceOrientationLandscapeRight); // Or whatever else orientation you need.
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

還要重新定義您的shouldAutorotate方法,如下所示:

- (BOOL)shouldAutorotate {
    // Change the YES or NO order depending on your viewController.
    return [UIDevice currentDevice].orientation == UIDeviceOrientationPortrait ? YES : NO;
}

並同時實現此方法:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait; // Or whatever orientation you need in this viewController.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM