使用 [window insertSubview] 时出现问题

发布于 2024-10-31 05:27:29 字数 4604 浏览 4 评论 0原文

当我使用以下代码在分割视图之上插入视图时,我遇到了方向问题。

这是我使用的代码,

[window addSubview:aSplitViewController.view];
[window insertSubview:aViewController.view aboveSubview:aSplitViewController.view];

这里发生的情况是视图控制器(包含标签和按钮)以横向模式加载,而其组件以纵向模式加载......

我觉得 window insertSubview 是创建这个问题是因为当我使用 [window addSubview:aViewController.view] 时,视图在横向模式下正确显示,其组件也在横向模式下......

这是我觉得的代码 给我带来了问题

在我的应用程序代理中

- (void) makeSplitViewController {

    NSMutableArray *controllers = [NSMutableArray arrayWithArray:tabBarController.viewControllers];

    // First tabbbar item
    // detail view
    detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
    UINavigationController *navDetailView = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
    navDetailView.hidesBottomBarWhenPushed = YES;


    // root view
    rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
    rootViewController.detailViewController = detailViewController;
    rootViewController.navigationItem.title = @"List";

    UINavigationController *navRootView = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
    navRootView.hidesBottomBarWhenPushed = YES;
    navRootView.navigationBar.barStyle = UIBarStyleBlackTranslucent;

    splitViewController = [[UISplitViewController alloc] init];
    splitViewController.tabBarItem.title = @"Face Sheet";
    splitViewController.tabBarItem.image = [UIImage imageNamed:@"gear1.png"];
    splitViewController.navigationItem.title = @"Face Sheet";
    splitViewController.viewControllers = [NSArray arrayWithObjects:navRootView, navDetailView, nil];
    splitViewController.delegate = detailViewController;
    splitViewController.hidesBottomBarWhenPushed = YES;
    [controllers addObject:splitViewController];

    // Second tabbbar item
    scoreViewController = [[ScoreCardViewController alloc] initWithNibName:@"TableViewController" bundle:nil];
    scoreViewController.tabBarItem.title = @"Score Card";
    scoreViewController.tabBarItem.image = [UIImage imageNamed:@"gear1.png"];
    scoreViewController.navigationItem.title = @"Score Card";
    [controllers addObject:scoreViewController];

    tabBarController.viewControllers = controllers;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.
    // Create tabbar
    tabBarController = [[UITabBarController alloc] init];
    //tabBarController.delegate = self;

    // Set window
    [window addSubview:splashController.view];
    [window insertSubview:tabBarController.view belowSubview:splashController.view];
    [self.window makeKeyAndVisible];

    application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;

    return YES;
}

,这是我的 SplashScreenView 中的代码

- (IBAction) proceedButtonClick:(id)sender
{
    // Initialize loginpopview
    PhysicianLoginViewController *loginViewController = [[PhysicianLoginViewController alloc] init];

    popOverController = [[UIPopoverController alloc] initWithContentViewController:loginViewController];
    popOverController.popoverContentSize = CGSizeMake(350, 200);
    popOverController.delegate = self;

    // Set a notification to dismiss it later
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginViewControllerDone:) name:@"loginViewControllerDone"  object:popOverController.contentViewController];

    // Present popover
    if ([popOverController isPopoverVisible])
    {
        [popOverController dismissPopoverAnimated:YES];
    }
    else 
    {

        [popOverController presentPopoverFromRect:CGRectMake(485, 600, 100, 100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    }

}

// Dismiss popview controller and setup the tabbar
- (void)loginViewControllerDone:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    // Button in content view controller was tapped, dismiss popover...
    [self.popOverController dismissPopoverAnimated:YES];

    // remove subview
    [self.view removeFromSuperview];

    // set tabbar
    i3EAppDelegate *appDelegate = (i3EAppDelegate *) [[UIApplication sharedApplication]delegate];
    [appDelegate makeSplitViewController];

}

如果有人能指出我哪里出错了,那就太好了。我已经被这个问题困扰了好几天了,我已经尝试了我想到的一切......

When I use the following code to insert a view on top of a split view, I am getting orientation problems.

Here is the code I use,

[window addSubview:aSplitViewController.view];
[window insertSubview:aViewController.view aboveSubview:aSplitViewController.view];

What happens here is that the view controller ( which contains labels and buttons) loads in landscape mode while its components load in portrait mode...

I feel that the window insertSubview is creating this problem because when I used [window addSubview:aViewController.view] the view is getting displayed properly in landscape mode with its components in landscape mode as well...

Here is the code which I feel is giving me the problem

In my App Delegate

- (void) makeSplitViewController {

    NSMutableArray *controllers = [NSMutableArray arrayWithArray:tabBarController.viewControllers];

    // First tabbbar item
    // detail view
    detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
    UINavigationController *navDetailView = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
    navDetailView.hidesBottomBarWhenPushed = YES;


    // root view
    rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
    rootViewController.detailViewController = detailViewController;
    rootViewController.navigationItem.title = @"List";

    UINavigationController *navRootView = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
    navRootView.hidesBottomBarWhenPushed = YES;
    navRootView.navigationBar.barStyle = UIBarStyleBlackTranslucent;

    splitViewController = [[UISplitViewController alloc] init];
    splitViewController.tabBarItem.title = @"Face Sheet";
    splitViewController.tabBarItem.image = [UIImage imageNamed:@"gear1.png"];
    splitViewController.navigationItem.title = @"Face Sheet";
    splitViewController.viewControllers = [NSArray arrayWithObjects:navRootView, navDetailView, nil];
    splitViewController.delegate = detailViewController;
    splitViewController.hidesBottomBarWhenPushed = YES;
    [controllers addObject:splitViewController];

    // Second tabbbar item
    scoreViewController = [[ScoreCardViewController alloc] initWithNibName:@"TableViewController" bundle:nil];
    scoreViewController.tabBarItem.title = @"Score Card";
    scoreViewController.tabBarItem.image = [UIImage imageNamed:@"gear1.png"];
    scoreViewController.navigationItem.title = @"Score Card";
    [controllers addObject:scoreViewController];

    tabBarController.viewControllers = controllers;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.
    // Create tabbar
    tabBarController = [[UITabBarController alloc] init];
    //tabBarController.delegate = self;

    // Set window
    [window addSubview:splashController.view];
    [window insertSubview:tabBarController.view belowSubview:splashController.view];
    [self.window makeKeyAndVisible];

    application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;

    return YES;
}

and here is the code in my SplashScreenView

- (IBAction) proceedButtonClick:(id)sender
{
    // Initialize loginpopview
    PhysicianLoginViewController *loginViewController = [[PhysicianLoginViewController alloc] init];

    popOverController = [[UIPopoverController alloc] initWithContentViewController:loginViewController];
    popOverController.popoverContentSize = CGSizeMake(350, 200);
    popOverController.delegate = self;

    // Set a notification to dismiss it later
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginViewControllerDone:) name:@"loginViewControllerDone"  object:popOverController.contentViewController];

    // Present popover
    if ([popOverController isPopoverVisible])
    {
        [popOverController dismissPopoverAnimated:YES];
    }
    else 
    {

        [popOverController presentPopoverFromRect:CGRectMake(485, 600, 100, 100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    }

}

// Dismiss popview controller and setup the tabbar
- (void)loginViewControllerDone:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    // Button in content view controller was tapped, dismiss popover...
    [self.popOverController dismissPopoverAnimated:YES];

    // remove subview
    [self.view removeFromSuperview];

    // set tabbar
    i3EAppDelegate *appDelegate = (i3EAppDelegate *) [[UIApplication sharedApplication]delegate];
    [appDelegate makeSplitViewController];

}

It would be great if someone could point out where I am going wrong. I have been stuck with this problem for quite a few days and I have tried everything that comes to my mind...

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

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

发布评论

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

评论(2

゛时过境迁 2024-11-07 05:27:29

您的问题是 UIWindow 和 UIViewController 的旋转处理并非设计为以这种方式工作。引用 文档

在 iOS 应用程序中,窗口对象执行与更改当前方向相关的大部分工作。然而,它与应用程序的视图控制器一起工作,以确定是否应该发生方向更改,如果是,应该调用哪些附加方法来响应更改。具体来说,它与最近添加到或呈现在窗口中的根视图的视图控制器一起使用。换句话说,窗口对象仅适用于最前面的视图控制器,该控制器的视图是使用“呈现视图控制器的视图”中描述的机制之一显示的。

这一段有点模糊和矛盾(它是最近添加的视图控制器,还是最顶层视图的控制器?),并且在实践中似乎不一定与观察结果相符。最重要的是,向 UIWindow 添加多个视图会搞砸自动旋转处理。

您应该更改代码以使用 presentModalViewController:animated: (可能将 modalPresentationStyle 设置为 UIModalPresentationFormSheet)或 UIPopoverController,而不是向窗户。

Your problem is that the rotation handling of UIWindow and UIViewController just isn't designed to work that way. Quoth the documentation:

In an iOS application, the window object does much of the work associated with changing the current orientation. However, it works in conjunction with the application’s view controllers to determine whether an orientation change should occur at all, and if so, what additional methods should be called to respond to the change. Specifically, it works with the view controller whose root view was most recently added to, or presented in, the window. In other words, the window object works only with the frontmost view controller whose view was displayed using one of the mechanisms described in “Presenting a View Controller’s View.”

This paragraph is somewhat vague and contradictory (is it the most recently added view controller, or the controller for the topmost view?), and in practice doesn't seem to necessarily match observations. The bottom line is that adding multiple views to a UIWindow will screw up the automatic rotation handling.

You should change your code to use presentModalViewController:animated: (maybe with modalPresentationStyle set to UIModalPresentationFormSheet) or a UIPopoverController instead of adding multiple subviews to the window.

揽清风入怀 2024-11-07 05:27:29

尝试:

[aViewController.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];

Try:

[aViewController.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文