模态视图控制器无法在横向模式下启动

发布于 2024-09-24 16:17:30 字数 1184 浏览 4 评论 0原文

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

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

我有另一个按钮可以打开具有相同行为的邮件编辑器。这是我的 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];
}

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

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.

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 open correctly in landscape mode.

I have another button that brings up the mail composer which has the identical behavior. Here is the code in my 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

九命猫 2024-10-01 16:17:30

模态框并不总是获取有关旋转的信息,它们从状态栏获取信息,这并不总是能正常工作。将其放入您的 viewWillAppear 中进行修复: [UIApplication sharedApplication].statusBarOrientation = self.interfaceOrientation 并且,如果您希望在模态中添加导航控制器,则需要创建一个。

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

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.interfaceOrientation And, if you want a navigation controller inside your modal, you need to create one.

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

微暖i 2024-10-01 16:17:30

答案在这里:

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];

Answer is here:

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];
和影子一齐双人舞 2024-10-01 16:17:30

哇,我在这个问题上损失了好几天……但我找到了解决方案!

我遇到了与您相同的问题:方法“presentModalViewController:animated:”仅适用于纵向模式。

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

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

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

问题只是“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

因此,每当您将子视图控制器的视图添加到父视图控制器时,请在执行此操作后调用“setParent:”方法。然后就可以了!

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.

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.)

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!

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

@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!

红尘作伴 2024-10-01 16:17:30

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

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

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

Got the same issue when presenting modally a navigation controller. Be sure to have correctly implement : 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.

梦毁影碎の 2024-10-01 16:17:30

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

显示pushviewcontroller动画看起来像presentModalViewController

,您可以关闭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];

希望有帮助..

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

Showing pushviewcontroller animation look like presentModalViewController

and you can close the viewController as below;

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..

不再见 2024-10-01 16:17:30

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

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];

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];
峩卟喜欢 2024-10-01 16:17:30

您不需要新的 导航控件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];
}

You don't need a new Navigation Controller.

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文