Create a submenu iOS - ios

I want to create a submenu that will appear right next to the first menu and that will contain more options. The first menu is hidden and only appears when a button its clicked on the navigation bar, i used the SWRevealViewController from github for the first menu, but i can't make the second one appear . Can somone help ?
Thanks for any help in advance

Basically you add another SWRevealController as front view controller of the root SWRevealController.
UIViewController *secondRearVC = // your second level menu controller
UIViewController *secondVC = // your second level front view controller
SWRevealViewController *childRevealController =
[[SWRevealViewController alloc] initWithRearViewController:secondRearVC frontViewController:secondVC];
[rootRevealController setFrontViewController:childRevealController animated:YES];
You can find example of what I've suggested here.
Other option is using other side menu controller, JASidePanels, if I'm not wrong it does what you want. Anyway you'll end up using some UIViewController containers recursively, so it's just a matter of choice.

For this effect i have created a separate view say reveal view with UITableView init, at first i am giving zero width and full height for that view. If the navigation button is pressed , i am moving the main view to right like about 100px and change the width of the reveal view to 100px, place that code in animation block.
[UIView animateWithDuration:0.5 animations:^{
//Move frame or transform view
revealView.frame = CGRectMake(0,0,1,screenHeight);
mainView.frame = CGRectMake(0, 0, 320, screenHeight);
}];

Related

How to enable two view controllers in the same window

I'm trying to develop a slide menu in my application which should open the menu view from the left (or the right) depending on the swipe direction.
What I did till now:
1- Create a main View Controller A
2- Create left (or right) View Controller B
3- When Slide button tapped, add B ViewController as a sub View for the A View Controller.
4- here's my code:
CGRect currentLeftFrame = self.leftVC.view.frame;
currentLeftFrame.origin = CGPointMake(-1.0f * self.view.frame.size.width, 0.0f);
self.leftVC.view.frame = currentLeftFrame;
[self addChildViewController:self.leftVC];
[self.view addSubview:self.leftVC.view];
[self.leftVC didMoveToParentViewController:self];
The problem is that buttons in the B View controller are not clickable.
Do one know how to solve this problem?
Thanks in advance.
Use UIView for slide panel and add the slide view as subview on viewController's view or on app window.

Xcode 5 - Swipe switch view with constant background

I understand how to implement UISwipeGestureRecognizer, but I do not understand how to use multiple views properly in collaboration with it.
My ultimate goal is to provide a sleek interface where buttons or swiping may be used to change views. I am hoping for a constant background rather than a background image show it is transferring to the exact same image again. I am using storyboarding currently where each view has its own controller, and I would like (if possible) to keep it as close to this format for visual clarity of what is going on.
I have read that subviews may be my best hope for this sort of scenario (keeping them to the right until a swipe is called), but that would break up the current storyboarding structure I am using, and most certainly ruin any visual clarity.
Does anyone have any suggestions?
I did same programmatically, but you can achieve same using storyboard
1. You need base view which will keep same background
2. Add 3 Views (or 2 depending on ur requirement) Left, Middle, Right with transparent backgrounds as follows in viewDidLoad of base view controller
[self addChildViewController:myChildViewController];
[myChildViewController view].frame = CGMakeRect(x,y,width,height);//Left one will have x = -320, y=0 ,Middle x= 0, y= 0, Right x = 32O etc.
/* Add new view to source view */
[self.view addSubview:[myChildViewController view]];
/* Inform 'didMoveToParentViewController' to newly added view controller */
[myChildViewController didMoveToParentViewController:self];
3. In handle Swipe method change frames of view with animation which will make it look like swiping page
e.g
//swipe left, get right view in middle
[UIView animateWithDuration:0.2 animations:^{
CGRect frame = rightViewController.view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
rightViewController.view.frame = frame;
CGRect middleViewFrame = middleViewController.view.frame;
middleViewFrame.origin.x = -320;
middleViewController.view.frame = middleViewFrame;
}];
You could use a navigation to do this. If you don't want the navigation bar, you can hide it. You can connect the left swipe gesture recognizer to a segue to the next controller, and connect the right swipe gesture recognizer to an unwind segue (or call popViewControllerAnimated: from its action method). If you want a constant background, then you need to add that to the navigation controller's view (as a background color), and have the child view controller's views be transparent (or partially so). This code in the navigation controller's root view controller will add the background:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"img.tiff"]];
}
I know exactly what you're talking about and I'm actually doing the same with my current project.
I accomplished this by using Interface Builder and a single view controller. Basically I placed a UIImageView in the view and set it to the background you wish, then I put a transparent UIScrollView on top of it. Inside that scroll view I put the two views I want to scroll horizontally through.
Don't forget to enable UIScrollView paging. In my case, I had two table views. Starting from the first one, if you swiped from right to left it would scroll to the next table view, and if you swiped left to right it would scroll back to the first.

Off by 20 pixels (y axis) when adding subview myNavigationController.view

I have a navigation controller with a menu button. When the menu button is pressed, the current view (complete with its navigation bar) should slide to the right, and a menu should simultaneously and contiguously slide in from the left. The details are explained in the comments in the code, below.
It works except in one regard. The current view (in this case, "Conversations"), when added back into the container view I implement, has to have its frame set to "y.min"=-20, or else there are 20 pixels above its navigation bar. However, when I set its y.min to -20, this shifts everything within its view up by 20 pixels. So when the menu button is pressed, everything in the current view suddenly jumps upwards by 20 pixels.
I can't figure out why this is happening or how to correct this. It is also possible that there's an easier way to go about all of this, but I'm not aware of one.* (*I'm not interested in third-party files. I want to learn to do this myself.)
Here is the code:
- (IBAction) showMenu:(id)sender
{
// get pointer to app delegate, which contains property for menu pointer
AppDelegate *appDelegate = getAppDelegate;
//create container view so that current view, along with its navigation bar, can be displayed alongside menu
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 548.0f)];
[container setBackgroundColor:[UIColor blueColor]];
//before presenting menu, create pointer for current view, to be used in completion routine
UIView *presenter = self.navigationController.view;
//present menu's VC. it is necessary to present its (table) VC, not just add its view, to retain the functionality of its cells
[self presentViewController:appDelegate.menuVC animated:NO completion: ^{
//obtain a pointer to the menu VC's view
UIView *menuTemp = appDelegate.menuVC.view;
//replace menu VC's view with the empty container view
appDelegate.menuVC.view = container;
//add the menu view to the container view and set its frame off screen
[container addSubview:menuTemp];
menuTemp.frame = CGRectMake(-260.0f, 0.0f, 260.0f, 548.0f);
//add the the view that was displayed when the user pressed the menu button. set its frame to fill the screen as normal
[appDelegate.menuVC.view addSubview:presenter];
presenter.frame = CGRectMake(0.0f, 0.0f, 320.0f, 548.0f);
//animate the 2 subviews that were just added; "PRESENTER"'S FRAME IS THE ISSUE
[UIView animateWithDuration:25.3f delay:0.0f options:nil animations:^{
[menuTemp setFrame:CGRectMake(0.0f, 0.0f, 260.0f, 548.0f)];
[presenter setFrame:CGRectMake(260.0f, -20.0/* or 0 */f, 320.0f, 548.0f)];
} completion:nil];
}];
}
Here is an illustration of my two options (click for higher resolution):
screen 1) The first screen shows the view before clicking the menu button.
The next two screens show two possibilities as the menu transition animation begins.
screen 2) This is what the transition looks like when I set presenter.frame.y.min to -20. As you can see, the button in the view has jumped up by 20 pixels.
screen 3) This is what the transition looks like when I set presenter.frame.y.min to 0. As you can see, a bar 20 pixels tall is present at the top. The blue color indicates the container view.
I had this problem some time ago when I didn't create my view controller tree correctly. My 20px off was caused by adding a UINavigationController view as a subview. I talked with the Apple Engineers in the labs at WWDC that year. They said I was using the navigation controller incorrectly, it needs to be a top level construct and you shouldn't put it in another UIViewController. That said you can put them in a container view controller like UITabBarController and UISplitViewController.
Your issue is not in the code you've posted it is in the architecture of your view controllers. I've uploaded a sample app to GitHub showing how to create a "Slide Menu" App like the FaceBook and StackOverflow iPhone Apps. See https://github.com/GayleDDS/TestNavBarOffBy20.git
The sample app uses a storyboard with Container Views in the root view controller to manage a UINavigationController (main view) and UITableViewController (menu view).
Now show Menu
See commit message cfb2efc for creation details. I started with the Single View Application template and you need to add the QuartzCore framework.

Move navigation controller and add a label

I want to move the navigation controller and add an small title between the default iphone bar and the navigation controller bar. Something like that:
https://www.dropbox.com/s/6an4ywp5h1dnxy2/iphone.png
I have not idea how to do it. I'm trying to move the navigation controller with this code
self.navigationController.view.frame = CGRectMake(0.0, 54.0, 220.0, 556.0);
self.navigationController.navigationBar.frame = CGRectMake(0.0, 0.0, 210.0, 64.0);
but then i can't add the label.
Thanx
Acturely , navigationcontroller can not finish you job , you can use uiviewcontroller and uitoolbar,uinavigationbar to do that , hope it can help you .
and this is tutorial
navigationbar tutorial
My understanding is that you cannot move the default navigation bar of your navigationController. That said, you could hide your navigationController's navBar, and then add a custom navBar (via xib or code) to your view (and set it however many pixels below the top you desire) and add a label above that. Then just have the actions for button presses call the navigationController's button calls (or do it yourself).
That having been said, this whole UI would be an abnormal appearance and would probably feel weird...Not sure that I'd recommend it.
To hide the navigationController's bar:
[[self navigationController] setNavigationBarHidden:YES animated:NO];

Is it correct to move the navigation bar frame?

I have a navigation bar based ipad app.
At some point I want to push another view controller into the views controller hierarchy. Then, when the users tabs some button I want to show a leftMenu controller. To do so I have two views:
A content view which has all the content
And a not visible view which is the leftMenu. This one is under the content view.
So when the user presses the button, what Im doing right now is moving the content view and the navigation bar to the right to make the leftMenu visible:
self.navigationController.navigationBar.frame = CGRectMake(271.0, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, self.self.navigationController.navigationBar.frame.size.height);
self.contentView.frame = CGRectMake(271.0, self.contentView.frame.origin.y, self.contentView.frame.size.width, self.contentView.frame.size.height);
This is working, but the first row in the left menu is not "clickable" where the nav bar is supossed to be. Its like the navigation bar is still there capturing the tab events.
Is it correct to do?:
self.navigationController.navigationBar.frame = CGRectMake(271.0, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, self.self.navigationController.navigationBar.frame.size.height);
If not, whats the propper way to achieve what I want?
Heres and image ilustrating what the problem is:
I think it's best to use a custom container controller to do this kind of thing, rather than moving a navigation bar. In IB, this can be set up quite easily. Start with a UIViewController, add a container view to it, and size how you want. Then in the inspector, set its x value to minus its width, which will put it off screen to the left. Then add another container view and size it to be full screen. You can then delete the view controller that you got with that container view, and right drag from the container view to your initial navigation controller (of your already setup UI) to connect it up with an embed segue. The UIViewController that you started with should be made the initial view controller of the storyboard. To move in the side view, I use this code in that custom container controller:
-(void)slideInLeft {
if (isRevealed == NO) {
[UIView animateWithDuration:.6 animations:^{
leftView.center = CGPointMake(leftView.center.x + 100, leftView.center.y);
mainView.center = CGPointMake(mainView.center.x + 100, mainView.center.y);
} completion:^(BOOL finished) {
isRevealed = YES; ;
}];
}else{
[UIView animateWithDuration:.6 animations:^{
leftView.center = CGPointMake(leftView.center.x - 100, leftView.center.y);
mainView.center = CGPointMake(mainView.center.x - 100, mainView.center.y);
} completion:^(BOOL finished) {
isRevealed = NO;
}];
}
}
leftView and mainView are IBOutlets to the 2 container views. I call this method from a button in the main view controller (the root view controller of the navigation controller that's embedded in the large container view):
-(IBAction)callSlideIn:(id)sender {
[(ViewController *)self.navigationController.parentViewController slideInLeft];
}
I found a "fast" way to achieve this (and a bit hacky imo)
I added the leftMenu view to the top view in the views hierachy:
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window)
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[[[window subviews] objectAtIndex:0] addSubview:self.leftMenu.view];
Now it is les deep than the navigation bar and, of course, its clickable

Resources