开发者

UISplitViewController in a TabBar ( UITabBarController )?

开发者 https://www.devze.com 2022-12-23 16:38 出处:网络
I am in kind of situation that I need to start with a tab based application and in that I need a split view for one or more tabs. But it seems that split view controller object can not be added to the

I am in kind of situation that I need to start with a tab based application and in that I need a split view for one or more tabs. But it seems that split view controller object can not be added to the tabbarController. (Although tabbar object 开发者_运维技巧can be added to the splitviewcontroller).

The problem can be seen otherways: I have a full screen in the left part I have a table view when any row is selected in the table a popover should come out pointing that row. Now when any row in the popover is selected the rows in this popover comes to the left under the selected row (only this row would be visible) and another popover comes out from the selected row. (Breadcrumb navigation type)

I think I am clear in what I explained. So guys any ideas or work arounds?

Please let me know if I am not clear in my question.

Thanks,

Madhup


Using the interface builder, create a split view controller and a tab bar controller and link them to your outlets:

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet UISplitViewController *splitViewController;

In your app delegate didFinishLaunchingWithOption, assign your split view controller to the tab bar controller:

splitViewController.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"Title" image:nil tag:0] autorelease];
NSArray *controllers = [NSArray arrayWithObjects:splitViewController,  /* other controllers go here */ nil];
tabBarController.viewControllers = controllers;
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];

This will create a tab bar controller (with only 1 tab in this case), which is displayed correctly in all orientations.


I've written up a subclass for the UISplitViewController that will listen for changes to device orientation and orient itself accordingly. With this class, I can now place split views within a UITabBarController and each split view will behave correctly upon rotation, even if it's not the frontmost tab. I've successfully deployed this in TexLege and it was approved for use in the App Store, but your mileage may vary. Please see the repository at Github.

Feel free to fork and modify it, and I'm always interested in hearing comments (or complaints) about it. https://github.com/grgcombs/IntelligentSplitViewController


I made a sample application. and found we can do it programmatically like:

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

        NSMutableArray *array = [NSMutableArray array];

        NSMutableArray *tabArray = [NSMutableArray array]; 

        UISplitViewController *splitViewConntroller = [[UISplitViewController alloc] init];

        MainViewController *viewCont = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
        [array addObject:viewCont];
        [viewCont release];

        viewCont = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
        [array addObject:viewCont];
        [viewCont release];




        [splitViewConntroller setViewControllers:array];

        [tabArray addObject:splitViewConntroller];

        [splitViewConntroller release];

        array = [NSMutableArray array];

        splitViewConntroller = [[UISplitViewController alloc] init];

        viewCont = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
        [array addObject:viewCont];
        [viewCont release];

        viewCont = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
        [array addObject:viewCont];
        [viewCont release];

        [splitViewConntroller setViewControllers:array];

        [tabArray addObject:splitViewConntroller];

        [splitViewConntroller release];

        // Add the tab bar controller's current view as a subview of the window
        [tabBarController setViewControllers:tabArray];

        [window addSubview:tabBarController.view];
        [window makeKeyAndVisible];

        return YES;
    }

Hope this helps.


To let a tabbarcontroller appear as a master view for splitviewcontroller you should rewrite tabbarcontroller so that it will support or orientations(so say, using a category for the class UITabBarController)


See my post about retrofitting split view controllers to an existing tab bar interface: http://markivsblog.blogspot.com/2010/04/retrofitting-ipad-uisplitviewcontroller.html


I created a UITabBarController subclass which properly propagates the rotation messages to all UISplitViewControllers it contains. This maintains the correct internal state of the UISplitViewControllers. However, one of the SplitViewController delegate methods is not called if the SplitViewController is not visible, so I account for this in the detail view controller viewWillAppear method. I've confirmed this works in iOS5.0 - iOS6.1

OSTabBarController.m

#import "OSTabBarController.h"

@implementation OSTabBarController

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    for(UIViewController *targetController in self.viewControllers){
        if(targetController != self.selectedViewController && [targetController isKindOfClass:[UISplitViewController class]]){
            [targetController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
        }
    }
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    for(UIViewController *targetController in self.viewControllers){
        if(targetController != self.selectedViewController && [targetController isKindOfClass:[UISplitViewController class]]){
            [targetController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
        }
    }
}

@end

DetailViewController

@implementation OSDetailViewController

-(void)viewWillAppear:(BOOL)animated{
    //the splitViewController:willHideViewController:withBarButtonItem:forPopoverController: may not have been called
    if(!UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
        self.navigationItem.leftBarButtonItem = nil;
    }
}

#pragma mark - UISplitViewControllerDelegate Methods

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];

}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
}

@end


Keep in mind that OS 3.2 does not provide proper support for a splitview as a tabbar view.

You can make it "work" but it will have bugs - the biggest is that an orientation change made on another tab's view will often not propagate to the splitview tab view properly, making the view go wacky when you go back to it (left side view takes over the screen, or the barbutton item is missing, etc.).

I've reached the conclusion that I have to create my own splitview for use in a tabBarController because of this issue.

I had heard rumors that Apple was working on a fix but it's been months now and no iPad OS updates have occurred - maybe OS 4 for the iPad will address it.


You can use IB to build tabtab and modify tabs to splitviewcontroller.

-(void) makeSplitViewController {
NSMutableArray *controllers = [NSMutableArray arrayWithArray:tabBarController.viewControllers];
int index = 0;

for (UIViewController *controller in tabBarController.viewControllers) {
    if ([controller.tabBarItem.title isEqualToString:@"Stock"]) {
        stockDetailController = [[StockDetailController alloc] initWithNibName:@"StockDetailController" bundle:nil];

        stockMasterController = [[StockMasterController alloc] initWithStyle:UITableViewStylePlain]; 
        stockMasterController.navigationItem.title = date;
        stockMasterController.stockDetailController = stockDetailController;

        UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:stockMasterController] autorelease];

        splitViewController = [[UISplitViewController alloc] init];
        splitViewController.tabBarItem = controller.tabBarItem;
        splitViewController.viewControllers = [NSArray arrayWithObjects:nav, stockDetailController, nil];
        splitViewController.delegate = stockDetailController;

        [controllers replaceObjectAtIndex:index withObject:splitViewController];
    }

    index++;
}

tabBarController.viewControllers = controllers;

}


We succeeded in having a UISplitViewController inside a UITabViewController on iPad with iOS5+.

to make a long story short: it works:

  • out of the box if you accept a split also in portrait;
  • with a bit of work, if you want to have the master view hidden in portrait, and have it appear only upon tapping a button.

The trick in the second case is to use the IntelligentSplitViewController (see a few posts up, thanx Greg Combs) or similarly extend a UISplitVC, and be careful that the delegate of the subclass of the splitview controller is always a live object.

We have detailed the process on:

https://devforums.apple.com/message/763572#763572

0

精彩评论

暂无评论...
验证码 换一张
取 消