How to make sliding menu with tabs in ios

落爺英雄遲暮 提交于 2020-01-03 06:17:10

问题


I am using ZUUIRevealController Library.

Appdelegate.h file

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window = window;

     UITabBarController *tabBarController=[[UITabBarController alloc]init];

    FrontViewController *frontViewController = [[FrontViewController alloc] init];
    RearViewController *rearViewController = [[RearViewController alloc] init];
    MapViewController *frontViewController2 = [[MapViewController alloc] init];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
    UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:frontViewController2];


    RevealController *revealController = [[RevealController alloc] initWithFrontViewController:navigationController rearViewController:rearViewController];

    RevealController *revealController2 = [[RevealController alloc] initWithFrontViewController:navigationController2 rearViewController:rearViewController];


    [revealController.tabBarItem setTitle:@"Home"];
    [revealController2.tabBarItem setTitle:@"Absent note"];



    revealController.tabBarItem.image=[[UIImage imageNamed:@"home_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    revealController2.tabBarItem.image=[[UIImage imageNamed:@"absent_note_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];





    [tabBarController setViewControllers:[NSArray arrayWithObjects:revealController,revealController2,nil]];



    [self.window setRootViewController:tabBarController];

    [self.window makeKeyAndVisible];
    return YES;
}

FrontViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = NSLocalizedString(@"Front View", @"FrontView");

    if ([self.navigationController.parentViewController respondsToSelector:@selector(revealGesture:)] && [self.navigationController.parentViewController respondsToSelector:@selector(revealToggle:)])
    {
        UIPanGestureRecognizer *navigationBarPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self.navigationController.parentViewController action:@selector(revealGesture:)];
        [self.navigationController.navigationBar addGestureRecognizer:navigationBarPanGestureRecognizer];
        [self.view addGestureRecognizer:navigationBarPanGestureRecognizer];

        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Slide", @"Slide") style:UIBarButtonItemStylePlain target:self.navigationController.parentViewController action:@selector(revealToggle:)];

        }








}

RearViewController is a Table ViewController

MapViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = NSLocalizedString(@"Map View", @"MapView");

    if ([self.navigationController.parentViewController respondsToSelector:@selector(revealGesture:)] && [self.navigationController.parentViewController respondsToSelector:@selector(revealToggle:)])
    {
        UIPanGestureRecognizer *navigationBarPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self.navigationController.parentViewController action:@selector(revealGesture:)];
        [self.navigationController.navigationBar addGestureRecognizer:navigationBarPanGestureRecognizer];
        [self.view addGestureRecognizer:navigationBarPanGestureRecognizer];

        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Slide", @"Slide") style:UIBarButtonItemStylePlain target:self.navigationController.parentViewController action:@selector(revealToggle:)];
    }
}

Tabs working Properly, but the sliding menu not working properly.

First I am click the Home tab and Slide menu to view the slide Viewcontroller.Next I clicked the Second tab and sliding menu to view the slide Viewcontroller. Again I've clicked the Home tab and sliding menu to view the Slide Viewcontroller. It cannot displayed only display a black screen.


回答1:


I found the solution for that problem. in Appdelegate file i have written the swreavealtoggle call for navigation.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;

HomeViewController *frontViewController = [[HomeViewController alloc] init];
SlideViewController *rearViewController = [[SlideViewController alloc] init];


UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
revealController.delegate = self;
self.viewController = revealController;

[self.window setRootViewController:self.viewController];
[self customizeInterface];
[self.window makeKeyAndVisible];
return YES;}

Then create a viewController to add a Tabbar item components. Myviewcontroller name is HomeViewController.h after i include the tab item button click action

#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController<UITabBarDelegate>{
UITabBar *mainTabBar;
UIViewController *tab1vc;  // view controller of first tab
UIViewController *tab2vc;  // view controller of second tab
UIViewController *tab3vc;  // view controller of first tab
UIViewController *tab4vc;  // view controller of second tab
}
@property (nonatomic, retain) IBOutlet UITabBar *mainTabBar;
@property (nonatomic, retain) UIViewController *tab1vc;
@property (nonatomic, retain) UIViewController *tab2vc;
@property (nonatomic, retain) UIViewController *tab3vc;
@property (nonatomic, retain) UIViewController *tab4vc;

@end

HomeViewController.m

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
// item is the selected tab bar item
switch (item.tag) {
    case 1:
        if (tab1vc == nil) {
            self.tab1vc =[[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];

        }
        [self.view insertSubview:tab1vc.view belowSubview:mainTabBar];
         self.title = @"Home";
        label.text = self.title;
        //[tab1vc release];
        NSLog(@"1st");

        break;
    case 2:
        if (tab2vc == nil) {
            self.tab2vc =[[Tab2 alloc] initWithNibName:@"Tab2" bundle:nil];

        }
        [self.view insertSubview:tab2vc.view belowSubview:mainTabBar];
        //[tab2vc release];

        self.title = @"Tab2";
        label.text = self.title;
         NSLog(@"2st");


        break;
    case 3:
        if (tab3vc == nil) {
            self.tab3vc =[[Tab3 alloc] initWithNibName:@"Tab3" bundle:nil];

        }
        [self.view insertSubview:tab3vc.view belowSubview:mainTabBar];
        //[tab2vc release];

        self.title = @"Tab3";
        label.text = self.title;
        NSLog(@"3rd");
        break;

    case 4:
        if (tab4vc == nil) {
            self.tab4vc =[[Tab4 alloc] initWithNibName:@"Tab4" bundle:nil];

        }
        [self.view insertSubview:tab4vc.view belowSubview:mainTabBar];
        //[tab2vc release];

        self.title = @"Tab4";
        label.text = self.title;
        NSLog(@"4th");
        break;

    default:
        break;
}

}

Now its working good



来源:https://stackoverflow.com/questions/23698697/how-to-make-sliding-menu-with-tabs-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!