UISplitViewController 奇怪的行为

发布于 2024-09-10 15:45:57 字数 2568 浏览 1 评论 0原文

嗨,我有一个 splitViewController

mapViewController = [[MapViewController alloc] initWithManagedObjectContext:managedObjectContext startingRegion:startingRegion];

    distanceViewController = [[DistanceTableViewController alloc] initWithManagedObjectContext:managedObjectContext];
    distanceViewController.mapViewController = mapViewController;
    setupViewController = [[SetupTableViewController alloc] initWithStyle:UITableViewStyleGrouped map:mapViewController.map];   
    setupViewController.positionSwitch.on = savePosition;

    SearchTableViewController *searchViewController = [[SearchTableViewController alloc]  initWithStyle:UITableViewStylePlain managedObjectContext:managedObjectContext];   
    searchViewController.mapViewController = mapViewController;

    tabBarController = [[UITabBarController alloc] init];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        UINavigationController *mapNavigationController = [[[UINavigationController alloc] initWithRootViewController:mapViewController] autorelease];
        UINavigationController *searchNavigationController = [[[UINavigationController alloc] initWithRootViewController:searchViewController] autorelease];
        UINavigationController *distanceNavigationController = [[[UINavigationController alloc] initWithRootViewController:distanceViewController] autorelease];
        UINavigationController *setupNavigationController = [[[UINavigationController alloc] initWithRootViewController:setupViewController] autorelease];

        UISplitViewController* splitVC = [[UISplitViewController alloc] init];
        splitVC.viewControllers = [NSArray arrayWithObjects:searchNavigationController, mapNavigationController, nil];
        splitVC.title = @"iMetano";
        splitVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"Mappa" image:[UIImage imageNamed:@"mapIcon2.png"] tag:0] autorelease];

        NSArray *viewControllersArray = [NSArray arrayWithObjects: splitVC,setupNavigationController,nil];
        [splitVC release];

        tabBarController.viewControllers = viewControllersArray;
    }

当我以纵向启动我的应用程序时,一切正常。

当我在横向启动我的应用程序时,这是结果

替代文字

  1. 我只看到第一个 viewController SearchTableViewController 的视图,UINavigationController 和状态栏之间有一些像素
  2. 当我以纵向旋转并以横向返回后,我看到两个 viewController 的视图,但第二个 viewController 的视图在 statusBar 和 UINavigationController 之间有一些像素

我不明白为什么。

Hi i have a splitViewController

mapViewController = [[MapViewController alloc] initWithManagedObjectContext:managedObjectContext startingRegion:startingRegion];

    distanceViewController = [[DistanceTableViewController alloc] initWithManagedObjectContext:managedObjectContext];
    distanceViewController.mapViewController = mapViewController;
    setupViewController = [[SetupTableViewController alloc] initWithStyle:UITableViewStyleGrouped map:mapViewController.map];   
    setupViewController.positionSwitch.on = savePosition;

    SearchTableViewController *searchViewController = [[SearchTableViewController alloc]  initWithStyle:UITableViewStylePlain managedObjectContext:managedObjectContext];   
    searchViewController.mapViewController = mapViewController;

    tabBarController = [[UITabBarController alloc] init];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        UINavigationController *mapNavigationController = [[[UINavigationController alloc] initWithRootViewController:mapViewController] autorelease];
        UINavigationController *searchNavigationController = [[[UINavigationController alloc] initWithRootViewController:searchViewController] autorelease];
        UINavigationController *distanceNavigationController = [[[UINavigationController alloc] initWithRootViewController:distanceViewController] autorelease];
        UINavigationController *setupNavigationController = [[[UINavigationController alloc] initWithRootViewController:setupViewController] autorelease];

        UISplitViewController* splitVC = [[UISplitViewController alloc] init];
        splitVC.viewControllers = [NSArray arrayWithObjects:searchNavigationController, mapNavigationController, nil];
        splitVC.title = @"iMetano";
        splitVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"Mappa" image:[UIImage imageNamed:@"mapIcon2.png"] tag:0] autorelease];

        NSArray *viewControllersArray = [NSArray arrayWithObjects: splitVC,setupNavigationController,nil];
        [splitVC release];

        tabBarController.viewControllers = viewControllersArray;
    }

When i startup my app in portrait, all works fine.

When i startup my app in landscape this is the result

alt text

  1. I see only the view of the first viewController SearchTableViewController with some pixel between the UINavigationController and the status bar
  2. When i rotate in portrait and after i return in landscape i see both viewController's view, but the second have some pixel between the statusBar and the UINavigationControllor

I can't understand why.

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

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

发布评论

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

评论(4

芸娘子的小脾气 2024-09-17 15:45:57

苹果表示不要将分割视图控制器放在其他东西中,例如选项卡栏控制器

apple says not to put a split view controller inside something else, like a tab bar controller

迷乱花海 2024-09-17 15:45:57

在一次又一次地查看我的代码和IB之后。这是我能想到的最好的办法。不确定是否是最好的,但它对我有用。我正在加载默认的详细视图控制器。如果我直接在 viewDidLoad 中加载控制器,则会出现问题。如果我从选择器加载它,问题就会消失。我希望这有帮助。我在 RootViewController 中有这段代码。

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self performSelector:@selector(loadController) withObject:nil afterDelay:0];
    }

    -(void)loadController{
    UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
    WebViewController *newDetailViewController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
    [newDetailViewController setTitle:@"Home"];
    NewNavController <SubstitutableDetailViewController>*navController = [[NewNavController alloc] initWithRootViewController:newDetailViewController];

    detailViewController = navController;

    NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
    splitViewController.viewControllers = viewControllers;


}

After looking at my code and IB time after time. This is the best that I could come up with. Not sure if is the best one but it works for me. Im loading a default detail view controller. If I load the controller directly in the viewDidLoad then the problem occur. If I load it from the selector the problem goes away. I hope this helps. I have this code in the RootViewController.

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self performSelector:@selector(loadController) withObject:nil afterDelay:0];
    }

    -(void)loadController{
    UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
    WebViewController *newDetailViewController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
    [newDetailViewController setTitle:@"Home"];
    NewNavController <SubstitutableDetailViewController>*navController = [[NewNavController alloc] initWithRootViewController:newDetailViewController];

    detailViewController = navController;

    NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
    splitViewController.viewControllers = viewControllers;


}
木落 2024-09-17 15:45:57

当我尝试组合标签栏、分割视图和导航控制器时,我遇到了同样的问题。我注意到,仅当应用程序首次启动并且自动选择第一个选项卡时,才会出现对齐间隙,因为它是选项卡栏控制器的视图控制器数组中的第一个选项卡。切换选项卡,然后返回分屏视图中导航控制器未对齐的选项卡后,不存在对齐问题。因此,为了复制这种行为并消除首次渲染屏幕时的错位,我添加了:

[tabBarController setSelectedViewController:splitVC];

在选项卡栏控制器上设置视图控制器数组之后。现在就像冠军一样工作。

I had this exact same problem when attempting the combination of tab bar, split view and navigation controllers. I noticed that the alignment gap is only present when the application first fires up and the first tab is auto-selected because it's the first tab in the tab bar controller's array of view controllers. After switching tabs and then coming back to the one with the misaligned nav controller in a split view, there was no alignment problem present. So, to replicate this behavior and get rid of the misalignment when the screen is first rendered I added:

[tabBarController setSelectedViewController:splitVC];

right after setting the view controller array on the tab bar controller. Works like a champ now.

二货你真萌 2024-09-17 15:45:57

我知道这是一个老问题,但这是我刚刚用来为任何拥有像我这样的导航层次结构的人解决这个问题的技巧:

UITabBarController
    Tab0->UINavigationController->MGSplitViewController _or_ UISplitViewController
    Tab1->UINavigationController->SomeOtherViewController
    Tab2->Etc...

我尝试过的任何方法都无法消除仅在启动时出现一次的 20px 间隙,如果设备方向是除 UIInterfaceOrientationPortrait 之外的任何方向。 20px 间隙是由上面分割视图的 UINavigationController 的 UINavigationBar 具有非零 origin.y 值引起的;最有可能的是,您会发现它是 20。

此外,我发现只有当设备运行 iOS << 时这才是问题。 5.0。

我在 MGSplitViewController 的视图控制器代码中检查此问题(即 self = an MGSplitViewController):

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if(self.doIOS4OneTimeRotationHack == YES)
    {
        self.doIOS4OneTimeRotationHack = NO;
        for(UINavigationController *navController in [self viewControllers])
        {
            if(navController.navigationBar.frame.origin.y != 0.0f)
            {
                [UIView animateWithDuration:0.01
                                      delay:0.0
                                    options:UIViewAnimationOptionCurveEaseOut
                                 animations:
                 ^(void)
                 {
                     navController.navigationBar.frame = CGRectMake(navController.navigationBar.frame.origin.x,0.0f, navController.navigationBar.frame.size.width,navController.navigationBar.frame.size.height);
                 }
                                 completion:
                 ^(BOOL finished)
                 {
                     //NSLog(@"Shifted navbar 0x%x up!",navController.navigationBar);
                 }];
            }
        }
    }
}

将动画设置为仅在 0.01 秒内完成,它发生得如此之快,以至于当启动闪屏消失时,您甚至不会注意到它您的 MGSplitViewController 视图将出现在其位置。也许可以尝试一下,让它瞬间完成;我必须让它工作并继续我的下一个任务,所以我不会在那个时候再愚弄它。

我不喜欢诉诸这样的黑客,但这是我能够解决这个问题的唯一方法。 ScottS 下面的解决方案听起来不错,但不幸的是对我来说不起作用。

I know this is an old question, but here's the hack I just used to get around this problem for anyone who has a navigation hierarchy like mine:

UITabBarController
    Tab0->UINavigationController->MGSplitViewController _or_ UISplitViewController
    Tab1->UINavigationController->SomeOtherViewController
    Tab2->Etc...

Nothing I tried could get rid of that 20px gap that occurs only once, at bootup, if the device orientation is anything except UIInterfaceOrientationPortrait. The 20px gap is caused by the UINavigationBar for the split view's UINavigationController above having a non-zero origin.y value; most likely, you'll find it to be 20.

Also, I found that this is only a problem if the device is running iOS < 5.0.

I check for this issue in the view controller code of my MGSplitViewController (i.e. self = an MGSplitViewController):

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if(self.doIOS4OneTimeRotationHack == YES)
    {
        self.doIOS4OneTimeRotationHack = NO;
        for(UINavigationController *navController in [self viewControllers])
        {
            if(navController.navigationBar.frame.origin.y != 0.0f)
            {
                [UIView animateWithDuration:0.01
                                      delay:0.0
                                    options:UIViewAnimationOptionCurveEaseOut
                                 animations:
                 ^(void)
                 {
                     navController.navigationBar.frame = CGRectMake(navController.navigationBar.frame.origin.x,0.0f, navController.navigationBar.frame.size.width,navController.navigationBar.frame.size.height);
                 }
                                 completion:
                 ^(BOOL finished)
                 {
                     //NSLog(@"Shifted navbar 0x%x up!",navController.navigationBar);
                 }];
            }
        }
    }
}

With the animation set to finish in just 0.01 seconds, it happens so fast that you'll never even notice it as your bootup splash screen disappears and your MGSplitViewController view appears in its place. Maybe play around with it and make it instantaneous; I had to get it working and move onto my next task, so I didn't fool with it past that point.

I don't like resorting to hacks like this, but this was the only way I was able to get around this problem. ScottS' solution below sounded great, but unfortunately didn't work for me.

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