How to enable two view controllers in the same window - ios

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.

Related

How to set a view over a child view controller's view?

I am wondering if it is possible to set a view controller's view over one of its child view controller's view ?
Let's explain that with an example :
Let's say I have two UIViewControllers : parentVCand childVC. childVC is a child of parentVC.
Now, I have a UIButton ( called button ) which is a subview of parentVC.view.
I set childVC from parentVC like that :
self.addChildViewController(childVC)
childVC.didMoveToParentViewController(self)
childVC.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height
self.view.addSubview(childVC.view)
Now, I want button to show up even if childVC.view covers parentVC.view.
Do you guys think there's a way to do that ? I tried to play with button.layer.zPosition but I couldn't succeed.
Yes, you can, you just need to add the button to the view hierarchy or move the button up in the hierarchy after you add the child view controller.
yes you can do that first of all when you load a childview on a viewcontroller make a view like. There is a container on top covering almost 90% screen and at bottom there is a button,So when you load the view on container and sub view show's you on top if you tap a button then add a subview and bring that subview to front.

Create a submenu 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);
}];

how to show an overlapping view iOS

I have a view with some UI components and a button on it, upon touch of a button I want to show a half view with some textfields on it overlapping the initial view, the initial view should be visible partly , the overlapping view will cover only half screen from bottom. Is this possible ?
I don't have any code as I am unable to figure out what it needs to be done, as we show any view it covers the entire screen.
Thanks
there are several ways you can do this, here are two:
1) add a popover controller that gets displayed on your button press:
here's some apple documentation on popovers: https://developer.apple.com/Library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/Popovers.html
2) add the new view as a subview to your UIViewController
PROGRAMICALLY:
in the viewDidLoad function you can do the following to initialize the halfScreenView
GLfloat topOffset = self.view.frame.size.height/2;
UIView halfScreenView = [[UIView alloc] initWithFrame: CGRectMake(0, topOffset , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - topOffset)
[self.view addSubview:halfScreenView];
-more logic might be needed if you support Landscape orientation, you can always re-assign the location of the view with halfScreenView.frame.origin and halfScreenView.frame.size
-initially you can have this view be hidden
halfScreenView.hidden = YES;
-when you click the button it will show the overlaying view:
halfScreenView.hidden = NO;
USING STORYBOARD:
you can also set up your overlaying view in the storyboard if you have one
-drag a UIView into your UIViewController and set it up where you want it to be located
-initialize the view to be hidden by checking the hidden box in the attribute inspector
-add the view as a property to your view
-manage when to show this view with self.halfScreenView.hidden
-this technique allows you to customize what is inside the new view within the storyboard which is nice
FOR BOTH:
-be careful with layers, you don't want your view to show up behind the one you already present. In the storyboard the last thing inserted goes on top. With in the code you can always access/change the views z position with halfScreenView.layer.zPosition (higher z values are on top)
First create a new class subclassing UIViewController called SecondView (or whatever you want), then design the UI the way you want (in its .xib file)
Then go to your main view controller's file and make an IBAction for that button.
In that method, write:
SecondView* second = [[SecondView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height/2, height, width);
[self.view addSubview:second.view];
This will add it to the bottom half of the screen. Put your own parameters for its height and width. When you want to dismiss the view, you can do this inside your SecondView class
[self.view removeFromSuperview];
You can deal with the textFields from within the SecondView class and have them communicate with your other view by doing the following in SecondView.h
#property IBOutlet UITextField* textField;
Hope this helps!
Yes, assuming you are using Interface Builder, go ahead and build the overlapping view and hook up all of the IBOutlets and IBActions. Say this view is called myView. Set myView.hidden = YES and myView.enabled = NO. This hides and disables myView so that it isn't even there from the user's perspective. In the appropriate button's IBAction, change the hidden and enabled properties to YES. That will make the view visible and active again.

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.

Resources