簡體   English   中英

固定視圖控制器的方向

[英]Fixing the orientation of view controllers

我的RootViewController中有一個按鈕....當我單擊該按鈕時,它將導航到其他otherViewController。 但是我希望當我單擊按鈕時otherViewController的方向應該是風景&當我從此頁面返回時(otherViewController)我應該再次獲得人像模式。

非常感謝您的幫助。 Plz幫助我在Iphone中是新手,因此無法處理此問題。

請注意,在iOS 6 shouldAutorotateToInterfaceOrientation不建議使用 shouldAutorotateToInterfaceOrientation 相反,您應該使用supportedInterfaceOrientations並返回UIInterfaceOrientationMask

因此,例如:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

這是您可能返回的所有UIInterfaceOrientationMask選項:

UIInterfaceOrientationMaskAll
UIInterfaceOrientationMaskAllButUpsideDown
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskPortraitUpsideDown

您還應該考慮preferredInterfaceOrientationForPresentation

通常,只有當用戶旋轉設備時,方向才應從縱向更改為橫向(反之亦然)。

但是,如果您希望RootViewController始終以縱向顯示其視圖,而OtherViewController始終以橫向顯示其視圖,則很簡單。

在您的RootViewController的.h中:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

在您的OtherViewController的.h中:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

盡管這樣做可能會更好,因為它將允許橫向向左旋轉和橫向向右旋轉:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

我知道可以做到的唯一方法是通過模態呈現otherViewController。

在這里查看我的答案: UIViewController加載旋轉為橫向,但僅支持縱向

也許有幫助。

在viewWillAppear中添加新的視圖控制器

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscape]

並在根viewController的viewWillAppear中添加

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

它將引起警告,但它可以工作。

暫無
暫無

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

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