xcode 模式视图控制器不会在横向模式下启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3772016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:46:28  来源:igfitidea点击:

Modal View Controller Won't Start in Landscape Mode

iphonexcodemodalviewcontroller

提问by Glasswing

I have a navigation based app that has a detail view (UIWebView) with action buttons across the bottom in a UIToolbar. I want to add 'notes' when the 'notes' button is pushed. Everything works fine when the webview is in portrait mode. I press the notes button, the modal view opens fine and works great.

我有一个基于导航的应用程序,它有一个详细信息视图 (UIWebView),在 UIToolbar 的底部有操作按钮。我想在按下“注释”按钮时添加“注释”。当 webview 处于纵向模式时,一切正常。我按下注释按钮,模态视图打开得很好,效果很好。

The problem occurs when the webview is in landscape mode. If I press the notes button, all the code to open the modal view gets called but all I get is a white screen. One comment: If I open the modal view in portrait and then rotate the device, it rotates fine into landscape mode. It just won't opencorrectly in landscape mode.

当 webview 处于横向模式时会出现问题。如果我按下注释按钮,所有打开模态视图的代码都会被调用,但我得到的只是一个白屏。一条评论:如果我以纵向打开模态视图,然后旋转设备,它会很好地旋转到横向模式。它只是无法在横向模式下正确打开

I have another button that brings up the mail composer which has the identical behavior. Here is the code in my UIWebViewController:

我有另一个按钮可以调出具有相同行为的邮件编辑器。这是我的 UIWebViewController 中的代码:

- (IBAction)addNotes:(id)sender 
{
    NotesViewController *notesViewController;

    // create the view controller and set it as the root view of a new navigation
    // controller

    notesViewController = [[NotesViewController alloc] initWithPrimaryKey:self.record.primaryKey];
    UINavigationController *newNavigationController =  
    [[UINavigationController alloc] initWithRootViewController:notesViewController];

    // present the navigation controller modally

    [self presentModalViewController:newNavigationController animated:YES];
    [notesViewController release]; 
    [self.view setNeedsDisplay]; // not sure if I need this!  I was trying different things...
    [self.devotionText setNeedsDisplay]; // ditto...
    [newNavigationController release];
}

Any ideas? I've tried all sorts of different things to no avail. I just get a white screen with no navigation bar (although there is a status bar at the top).

有任何想法吗?我尝试了各种不同的方法都无济于事。我只是得到一个没有导航栏的白屏(尽管顶部有一个状态栏)。

回答by MishieMoo

Modals don't always get information about rotations, and they get their info from the status bar, which doesn't always work right. Put this in your viewWillAppear to fix: [UIApplication sharedApplication].statusBarOrientation = self.interfaceOrientationAnd, if you want a navigation controller inside your modal, you need to create one.

模态并不总是获得有关旋转的信息,它们从状态栏获取它们的信息,这并不总是正常工作。把它放在你的 viewWillAppear 中来修复:[UIApplication sharedApplication].statusBarOrientation = self.interfaceOrientation而且,如果你想要一个导航控制器在你的模态中,你需要创建一个。

Also, you don't need the setNeedsDisplay. That only effects the current views, not the modal you are presenting.

此外,您不需要 setNeedsDisplay。这仅影响当前视图,而不影响您呈现的模式。

回答by Jimmy_m

Answer is here:

答案在这里:

https://stackoverflow.com/a/10250747/1449618

https://stackoverflow.com/a/10250747/1449618

Use the window's root view controller to present:

[self.view.window.rootViewController presentViewController:masterView
                                                  animated:YES
                                                completion:NULL];

回答by PrimaryFeather

Wow, I lost days over that issue ... but I found a solution!

哇,我在那个问题上浪费了好几天……但我找到了解决方案!

I had the same problem you had: the method "presentModalViewController:animated:" only worked in portrait mode.

我遇到了与您相同的问题:方法“presentModalViewController:animated:”只能在纵向模式下工作。

After a lot of trial and error, I found out that the reason was that I had several view controllers active at the same time. I implemented a navigation system which switched between different view controllers, with one parent handling the children. (I could not use UINavigationController, because I needed a different look.)

经过大量的反复试验,我发现原因是我同时有多个视图控制器处于活动状态。我实现了一个导航系统,可以在不同的视图控制器之间切换,一个家长处理孩子。(我无法使用 UINavigationController,因为我需要不同的外观。)

So, my root view controller had a root view object, and several child view controllers. When a child view controller was activated, its view object was added as subview to the view of the root view controller.

所以,我的根视图控制器有一个根视图对象和几个子视图控制器。当子视图控制器被激活时,它的视图对象作为子视图添加到根视图控制器的视图中。

The "presentModalViewController" method didn't like that. However, as soon as I set the "parentViewController" property of the child view controllers, it worked!

“presentModalViewController”方法不喜欢那样。但是,一旦我设置了子视图控制器的“parentViewController”属性,它就起作用了!

The problem is only that "parentViewController" is a read-only property. You have to extend the UIViewController class so you can access it.

问题仅在于“parentViewController”是只读属性。您必须扩展 UIViewController 类才能访问它。

@interface UIViewController (HelperExtension)

@property (nonatomic, assign) UIViewController *parent;

@end

@implementation UIViewController (HelperExtension)

- (UIViewController *)parent
{
    return self.parentViewController;
}

- (void)setParent:(UIViewController *)parent
{
    [self setValue:parent forKey:@"_parentViewController"];
}

@end

So, whenever you add the view of a child view controller to your parent view controller, call the "setParent:" method after doing it. Then it will work!

因此,每当您将子视图控制器的视图添加到父视图控制器时,请在完成后调用“setParent:”方法。然后它会起作用!

回答by ??u??

Got the same issue when presenting modally a navigation controller. Be sure to have correctly implement : shouldAutorotateToInterfaceOrientation

以模态方式呈现导航控制器时遇到了同样的问题。确保正确实现: shouldAutorotateToInterfaceOrientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    BOOL shouldAutorotate = NO;
    if( isControllerMangingAllOrientations )
    {
        shouldAutorotate = YES;
    }
    else
    {
        shouldAutorotate = (toInterfaceOrientation == UIInterfaceOrientationPortrait);
    }
    return shouldAutorotate;
}

I was setting the boolean in the viewDidLoad method, not a good idea. Putting it in the initWithNibName:bundle: method is the right place.

我在 viewDidLoad 方法中设置了布尔值,这不是一个好主意。把它放在 initWithNibName:bundle: 方法中是正确的地方。

回答by seymatanoglu

If you use presentModalViewController just for animation like me, you can use pushViewController with animation as below answer;

如果您像我一样仅将presentModalViewController 用于动画,则可以将pushViewController 与动画一起使用,如下所示;

Showing pushviewcontroller animation look like presentModalViewController

显示 pushviewcontroller 动画看起来像 presentModalViewController

and you can close the viewController as below;

你可以关闭 viewController 如下;

CATransition* transition = [CATransition animation];
transition.duration = 0.3;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromTop;

[self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.navigationController popViewControllerAnimated:NO];

Hope it helps..

希望能帮助到你..

回答by David

I had the task to show a video player in landscape mode.

我的任务是在横向模式下显示视频播放器。

AVPlayerViewController *playerViewController = [AVPlayerViewController new];
//Player init code goes here....

// #define degreesToRadian(x) (M_PI * (x) / 180.0) - was defined previously in a class header

playerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
playerViewController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
playerViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

[self presentViewController:playerViewController animated:YES completion:nil];

回答by Stephen Furlani

You don't need a new Navigation Controller.

您不需要新的导航控制器r。

- (IBAction)addNotes:(id)sender {

 NotesViewController *notesViewController;

 // create the view controller and set it as the root view of a new navigation
 // controller

 notesViewController = [[NotesViewController alloc] initWithPrimaryKey:self.record.primaryKey];

 [self.navigationController pushViewController: notesViewController animated: YES];
 [notesViewController release];
}