سؤال

I am having an iPad app in which I want to implement a Side Bar functionality like we have in facebook app.

I am using this Demo for this.

With this I have successfully implemented the Side Bar functionality and its working well but with that my first view doesn't show me well.

Below is the screenshot.

enter image description here

As you can see from the screenshot, there is a black background and my whole view is not showing in full screen when the app launches.

It should be like below.

enter image description here

Also on clicking the button the Side View is showing like below.

enter image description here

It should be small as I have taken the view size with width = 300 and height = 768.

But it is showing bigger than that.

Here is my code which I change in my appdelegate.

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

    [self createEditableCopyOfDatabaseIfNeeded];

    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:self.viewController];

    SlidingViewController *slidingView = [[SlidingViewController alloc]initWithNibName:@"SlidingViewController" bundle:nil];

    self.slideMenuController = [[SlideMenuController alloc] initWithCenterViewController:navController];
    self.slideMenuController.leftViewController = slidingView;

    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
   return YES;
}

- (IBAction)sideBarPressed:(id)sender
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (appDelegate.slideMenuController.position == TKSlidePositionCenter) {
        [appDelegate.slideMenuController presentLeftViewControllerAnimated:YES];
    } else {
        [appDelegate.slideMenuController presentCenterViewControllerAnimated:YES];
    }
}

I want this for my iPad for Landscape mode only.

Please tell me What is wrong here?

I am stuck here for quite a while.

Any help will be appreciated.

Thanks in advance.

هل كانت مفيدة؟

المحلول 2

I successfully implemented this side bar menu functionality with this Demo code and its working perfectly well in my case.

Hope it works to someone else also.

Thanks for your help and suggestions.

نصائح أخرى

So better u need to use UISplitViewController

 //in app delegate do like this
 //in appDelegate.h file 
 #import <UIKit/UIKit.h>

 @interface AppDelegate : UIResponder <UIApplicationDelegate>

 @property (strong, nonatomic) UIWindow *window;
 @property (nonatomic, retain) UISplitViewController *splitViewCOntroller; 
 @end


 //in appDelegate.m file
 #import "AppDelegate.h"
 #import "SplitMasterViewController.h"  //create a UITableviewController 
 #import "SplitViewDetailController.h"  //create a UIViewController

  @implementation AppDelegate
  @synthesize splitViewCOntroller = _splitViewCOntroller;

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   {
      // Override point for customization after application launch.
      SplitMasterViewController *masterController  = [[SplitMasterViewController alloc]initWithNibName:@"SplitMasterViewController" bundle:nil]; //this is the master menu controller

     UINavigationController *masterNavController = [[UINavigationController alloc]initWithRootViewController:masterController];

     SplitViewDetailController *detailViewController = [[SplitViewDetailController alloc]initWithNibName:@"SplitViewDetailController" bundle:nil]; //this is the master detail controller


     UINavigationController *detailNavController = [[UINavigationController alloc]initWithRootViewController:detailViewController];
masterController.detailViewController = detailViewController;

    _splitViewCOntroller = [[UISplitViewController alloc]init]; //initilise split controller
    _splitViewCOntroller.delegate = detailViewController;      //set the delegate to detail controller
    _splitViewCOntroller.viewControllers = [NSArray arrayWithObjects:masterNavController,detailNavController, nil]; //set the splitview controller
     self.window.rootViewController = _splitViewCOntroller; //finally your splitviewcontroller as the root view controller
     [self.window makeKeyAndVisible];

     return YES;
 }


 //in SplitMasterViewController.h this must be a table that contains your side bar menu items 
 #import "ViewController.h" //comment this if it shows any error
 #import "SplitViewDetailController.h"

 @interface SplitMasterViewController :        UITableViewController<UITableViewDataSource,UITableViewDelegate>
 @property (nonatomic, retain) SplitViewDetailController *detailViewController; //to get the detailview from masterMenu controller 
 @property (nonatomic, retain) NSArray *Names;

 @end 

 // in SplitMasterViewController.m file
 #import "SplitMasterViewController.h"
 #import "SplitViewDetailController.h"


 @interface SplitMasterViewController ()

 @end

 @implementation SplitMasterViewController

 @synthesize Names;
 @synthesize detailViewController;


 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
     // Custom initialization
      }
     return self;
  }

  - (void)viewDidLoad
  { 
    [super viewDidLoad];
    //  Do any additional setup after loading the view from its nib.
    Names = [[NSArray alloc] initWithObjects:@"apple", @"banana",
             @"mango", @"grapes", nil];

    [self.tableView selectRowAtIndexPath:
    [NSIndexPath indexPathForRow:0 inSection:0]
                            animated:NO
                      scrollPosition:UITableViewScrollPositionMiddle];
   }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
       return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
      return [Names count];
    }

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
     static NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
     cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:CellIdentifier];
        }

    // [self configureCell:cell atIndexPath:indexPath];

    cell.textLabel.text = [Names objectAtIndex:indexPath.row];
    return cell;
   }

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     SplitViewDetailController *detailController =  self.detailViewController;
     detailController.myLabel.text = [Names objectAtIndex:indexPath.row]; //set the name from the array
 }

  - (void)didReceiveMemoryWarning
  {
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
  }

  @end


  //in SplitViewDetailController.h
  #import "ViewController.h"

  @interface SplitViewDetailController : UIViewController<UISplitViewControllerDelegate>
  @property (strong, nonatomic) id detailItem;
  @property (strong, nonatomic) IBOutlet UILabel *myLabel;
  @property (nonatomic, retain) UIBarButtonItem *leftBarButtonItem; //to show a button on left side

  @end

 //in SplitViewDetailController.m 

 #import "SplitViewDetailController.h"

 @interface SplitViewDetailController ()
 {
   UIPopoverController *masterPopoverController;
 }

 @end

  @implementation SplitViewDetailController

  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
      // Custom initialization
     }
     return self;
  }

  - (void)viewDidLoad
  {
    [super viewDidLoad];
     // _leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Menu" style:UIBarButtonItemStyleBordered target:self action:nil];
  }

  - (void)didReceiveMemoryWarning
  {
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
   }


  //these are the call back to detail view controller to hide or show the button
  - (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController  withBarButtonItem:(UIBarButtonItem *)barButtonItem  forPopoverController:(UIPopoverController *)popoverController
 {
   _leftBarButtonItem = barButtonItem;
   _leftBarButtonItem.style = UIBarButtonItemStyleBordered;
   _leftBarButtonItem.title = @"Menu";
   [self.navigationItem setLeftBarButtonItem:_leftBarButtonItem animated:YES];
  }

  // Called when the view is shown again in the split view, invalidating the button
  - (void)splitViewController:(UISplitViewController *)splitController       willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
   {
      [self.navigationItem setLeftBarButtonItem:nil animated:YES];
   }

  @end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top