I am beginner in iOS. In my ParentViewController I am adding one ChildViewController. In that controller I have added one Next button. When I click that button I am pushing First ChildViewController to Second ChildViewController. That's fine.
But here I have added another Button in Second ChildViewController. When i click that button I want to push back from Second ChildViewController to First ChildViewController. But with my code that's not working. Please help me someone
My code:
ParentViewController:
#import "ParentViewController.h"
#interface ParentViewController ()
#end
#implementation ParentViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
ChildViewController1 *ViewController1 = [self.storyboard instantiateViewControllerWithIdentifier:#"ChildViewController1"];
ViewController1.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
ViewController1.view.backgroundColor = [UIColor cyanColor];
[ViewController1 willMoveToParentViewController:self];
[self.view ViewController1.view];
[self ViewController1];
}
#end
ChildViewController1:
#import "ChildViewController1.h"
#interface ChildViewController1 ()
{
}
#end
#implementation ChildViewController1
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)next:(id)sender{
ChildViewController2 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:#"ChildViewController2"];
middleVC.view.hidden=FALSE;
[middleVC.view setFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self addChildViewController:middleVC];
[self.view addSubview:middleVC.view];
[middleVC didMoveToParentViewController:self];
[UIView animateWithDuration:0.5 animations:^{
[middleVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}];
}
ChildViewController2:
#import "ChildViewController2.h"
#interface ChildViewController2 ()
{
}
#end
#implementation ChildViewController2
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)back:(id)sender {
ChildViewController2 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:#"ChildViewController2"];
middleVC.view.hidden=FALSE;
[middleVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self addChildViewController:middleVC];
[self.view addSubview:middleVC.view];
[middleVC didMoveToParentViewController:self];
[UIView animateWithDuration:0.5 animations:^{
[middleVC.view setFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
}];
}
Here when I click this back button it's not pushing it back.
Inside the ChildViewController2 back button action you have:
ChildViewController2 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:#"ChildViewController2"];
Don't you mean:
ChildViewController1 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:#"ChildViewController1"];
Related
I have one viewcontroler with one button.When i press that one pop up viewcontroller will show up.And when i touch anywhere outside it dismiss the viewcontroller .And return to my main viewcontroller.
Note
I used my popup through one viewcontroller and i use identifier name .Then i show pop up using that storyboard identifier and declared in my viewcontroller.m in btnSelectDatePressed
Needed
in that pop up view controller i have one button when user press also it should dismiss the viewcontroller and should return to my main viewcontroller.My example image is like this sample image
This is my viewcontroller.m file
#import "ViewController.h"
#import "UIViewController+ENPopUp.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)btnSelectDatePressed:(id)sender
{
UIViewController *vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"PopUp"];
vc.view.frame = CGRectMake(0, 0, 309.0f, 531.0f);
[self presentPopUpViewController:vc];
}
This is my popup method declared in #import "UIViewController+ENPopUp.h"
#import <UIKit/UIKit.h>
#interface UIViewController (ENPopUp)
#property (nonatomic, retain) UIViewController *en_popupViewController;
- (void)presentPopUpViewController:(UIViewController *)popupViewController;
- (void)presentPopUpViewController:(UIViewController *)popupViewController completion:(void (^)(void))completionBlock;
- (void)dismissPopUpViewController;
- (void)dismissPopUpViewControllerWithcompletion:(void (^)(void))completionBlock;
//- (IBAction)disMe:(id)sender;
#end
This is my popup method declared in `#import "UIViewController+ENPopUp.m"`
#import "UIViewController+ENPopUp.h"
#import "JWBlurView.h"
#import <objc/runtime.h>
static void * ENPopupViewControllerPropertyKey = &ENPopupViewControllerPropertyKey;
static CGFloat const kAnimationDuration = .4f;
static CGFloat const kRotationAngle = 70.f;
static NSInteger const kENPopUpOverlayViewTag = 351301;
static NSInteger const kENPopUpViewTag = 351302;
static NSInteger const kENPopUpBluredViewTag = 351303;
#implementation UIViewController (ENPopUp)
#pragma mark - Public Methods
- (void)presentPopUpViewController:(UIViewController *)popupViewController
{
[self presentPopUpViewController:popupViewController completion:nil];
}
- (void)presentPopUpViewController:(UIViewController *)popupViewController completion:(void (^)(void))completionBlock
{
self.en_popupViewController = popupViewController;
[self presentPopUpView:popupViewController.view completion:completionBlock];
}
- (void)dismissPopUpViewController
{
[self dismissPopUpViewControllerWithcompletion:nil];
}
- (void)dismissPopUpViewControllerWithcompletion:(void (^)(void))completionBlock
{
UIView *sourceView = [self topView];
JWBlurView *blurView = (JWBlurView *)[sourceView viewWithTag:kENPopUpBluredViewTag];
UIView *popupView = [sourceView viewWithTag:kENPopUpViewTag];
UIView *overlayView = [sourceView viewWithTag:kENPopUpOverlayViewTag];
[self performDismissAnimationInSourceView:sourceView withBlurView:blurView popupView:popupView overlayView:overlayView completion:completionBlock];
}
//- (IBAction)disMe:(id)sender {
// UIViewController *vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"Pop"];
// [self dismissViewControllerAnimated:YES completion:Nil];
//}
#pragma mark - Getters & Setters
- (UIViewController *)en_popupViewController
{
return objc_getAssociatedObject(self, ENPopupViewControllerPropertyKey);
}
- (void)setEn_popupViewController:(UIViewController *)en_popupViewController
{
objc_setAssociatedObject(self, ENPopupViewControllerPropertyKey, en_popupViewController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark - View Handling
- (void)presentPopUpView:(UIView *)popUpView completion:(void (^)(void))completionBlock
{
UIView *sourceView = [self topView];
// Check if source view controller is not in destination
if ([sourceView.subviews containsObject:popUpView]) return;
// Add overlay
UIView *overlayView = [[UIView alloc] initWithFrame:sourceView.bounds];
overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
overlayView.tag = kENPopUpOverlayViewTag;
overlayView.backgroundColor = [UIColor clearColor];
// Add Blured View
JWBlurView *bluredView = [[JWBlurView alloc] initWithFrame:overlayView.bounds];
bluredView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
bluredView.tag = kENPopUpBluredViewTag;
[bluredView setBlurAlpha:.0f];
[bluredView setAlpha:.0f];
[bluredView setBlurColor:[UIColor clearColor]];
bluredView.backgroundColor = [UIColor clearColor];
[overlayView addSubview:bluredView];
// Make the background clickable
UIButton * dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
dismissButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
dismissButton.backgroundColor = [UIColor clearColor];
dismissButton.frame = sourceView.bounds;
[overlayView addSubview:dismissButton];
[dismissButton addTarget:self action:#selector(dismissPopUpViewController)
forControlEvents:UIControlEventTouchUpInside];
// Customize popUpView
popUpView.layer.cornerRadius = 3.5f;
popUpView.layer.masksToBounds = YES;
popUpView.layer.zPosition = 99;
popUpView.tag = kENPopUpViewTag;
popUpView.center = overlayView.center;
[popUpView setNeedsLayout];
[popUpView setNeedsDisplay];
[overlayView addSubview:popUpView];
[sourceView addSubview:overlayView];
[self setAnimationStateFrom:popUpView];
[self performAppearAnimationWithBlurView:bluredView popupView:popUpView completion:completionBlock];
}
#pragma mark - Animation
- (void)setAnimationStateFrom:(UIView *)view
{
CALayer *layer = view.layer;
layer.transform = [self transform3d];
}
- (CATransform3D)transform3d
{
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, 0, 200.f, 0);
transform.m34 = 1.0/800.0;
transform = CATransform3DRotate(transform, kRotationAngle*M_PI/180.f, 1.f, .0f, .0f);
CATransform3D scale = CATransform3DMakeScale(.7f, .7f, .7f);
return CATransform3DConcat(transform, scale);
}
- (void)performAppearAnimationWithBlurView:(JWBlurView *)blurView popupView:(UIView *)popupView completion:(void (^)(void))completionBlock
{
CATransform3D transform;
transform = CATransform3DIdentity;
[UIView animateWithDuration:kAnimationDuration
animations:^ {
[self.en_popupViewController viewWillAppear:NO];
[blurView setAlpha:1.f];
popupView.layer.transform = transform;
}
completion:^(BOOL finished) {
[self.en_popupViewController viewDidAppear:NO];
if (completionBlock != nil) {
completionBlock();
}
}];
}
- (void)performDismissAnimationInSourceView:(UIView *)sourceView
withBlurView:(JWBlurView *)blurView
popupView:(UIView *)popupView
overlayView:(UIView *)overlayView
completion:(void (^)(void))completionBlock
{
CATransform3D transform = [self transform3d];
[UIView animateWithDuration:kAnimationDuration
animations:^ {
[self.en_popupViewController viewWillDisappear:NO];
[blurView setAlpha:0.f];
popupView.layer.transform = transform;
}
completion:^(BOOL finished) {
[popupView removeFromSuperview];
[blurView removeFromSuperview];
[overlayView removeFromSuperview];
[self.en_popupViewController viewDidDisappear:NO];
self.en_popupViewController = nil;
if (completionBlock != nil) {
completionBlock();
}
}];
}
#pragma mark - Getters
- (UIView*)topView {
UIViewController *recentView = self;
while (recentView.parentViewController != nil) {
recentView = recentView.parentViewController;
}
return recentView.view;
}
#end
I am beginner in ios.Some one please give some solution for my problem.Thanks in advance !
I changed the code of UIViewController+ENPopUp.m for have a visible button.
In code existe a button with screen size for dismiss view. I changed this button and now he is visible.
In presentPopUpView UIViewController+ENPopUp.m where have this comment " // Make the background clickable" I change the code to this:
CGFloat buttonSize = 10;
UIButton * dismissButton = [[UIButton alloc] init];
dismissButton.backgroundColor = [UIColor redColor];
dismissButton.frame = CGRectMake((self.view.frame.size.width/2) - (buttonSize/2),(self.view.frame.size.height/2) + (self.view.frame.size.height/4) , buttonSize, buttonSize);
[overlayView addSubview:dismissButton];
[dismissButton addTarget:self action:#selector(dismissPopUpViewController)
forControlEvents:UIControlEventTouchUpInside];
This create a red button 10x10 like in figure:
Now, you can change the color and the size of button for what you want.
I hope this can help you.
I saw a few other similar questions but could find any answers... I have a shared iAd banner. The problem is when I load it in a modally presented NavigationController with a TableViewController, when I dismiss the TableViewController the Ad Banner in the MainViewController loses the ad and I receive the following error:
ADBannerView: Unhandled error (no delegate or delegate does not implement didFailToReceiveAdWithError:): Error Domain=ADErrorDomain Code=7 "The operation couldn’t be completed. Ad was unloaded from this banner" UserInfo=0x5b60e283649 {ADInternalErrorCode=7, NSLocalizedFailureReason=Ad was unloaded from this banner, ADInternalErrorDomain=ADErrorDomain}
The View setup us like this:
MainViewController --> NavigationController --> TableViewController
At another point in the app I modally present a ViewController with shared ad and I don't experience this issue. (MainViewController --> ViewController)
Here is the code from the TableViewController:
.h
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import "AppDelegate.h"
#interface TableViewController : UITableViewController <ADBannerViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView *table;
#property (strong, nonatomic) ADBannerView *adView;
#end
.m
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
// Insert Ad Bar
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication ]delegate];
_adView = [appdelegate adView];
_adView.delegate = self;
self.canDisplayBannerAds = true;
if (IPAD) {
[_adView setFrame:CGRectMake(0, self.view.frame.size.height - 65, 320, 65)];
}
else {
[_adView setFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
}
if (!_adView.bannerLoaded) {
[_adView setAlpha:0];
}
[self.view addSubview:_adView];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
Any tips would be appreciated.
Thanks
The problem was because I was using:
self.canDisplayBannerAds = true;
as well as:
if (IPAD) {
[_adView setFrame:CGRectMake(0, self.view.frame.size.height - 65, 320, 65)];
}
else {
[_adView setFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
}
if (!_adView.bannerLoaded) {
[_adView setAlpha:0];
}
[self.view addSubview:_adView];
and thus was created 2 instances of the ad bar.
created a class,
GSSFeatureScreenLocationTabView : UIView,
to add multiple view into a scroll view,
the view comes up with buttons which are linked to a selector that is supposed to push to another view controller, It sends the placeID back up but from some reason doesn't push to the next view controller
GSSFeatureScreenLocationTabView.h
#import "featureScreenViewController.h"
#interface GSSFeatureScreenLocationTabView : UIView
{
UIImageView* imageView;
UILabel* titleLabel;
UILabel* distanceLabel;
UILabel* descriptionLabel;
UIButton* pushButton;
}
#property (nonatomic) NSString* placeID;
-(void)setLocationInfo:(NSString*)ID title:(NSString*)title distance:(double)distance description:(NSString*)description imageURL:(NSString*)imageURL;
#end
GSSFeatureScreenLocationTabView.m
#import "GSSFeatureScreenLocationTabView.h"
#import "GSSLocationDetailViewController.h"
#implementation GSSFeatureScreenLocationTabView
#synthesize placeID = _placeID;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 144, 140)];
titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 148, 107, 21)];
distanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 177, 107, 21)];
descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 206, 107, 91)];
pushButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 144, 317)];
[titleLabel setText:#""];
[distanceLabel setText:#""];
[descriptionLabel setText:#""];
[descriptionLabel setLineBreakMode:NSLineBreakByWordWrapping];
[pushButton setTitle:#"" forState:UIControlStateNormal];
[pushButton addTarget:self action:#selector(pushToLocation) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:imageView];
[self addSubview:titleLabel];
[self addSubview:distanceLabel];
[self addSubview:descriptionLabel];
[self addSubview:pushButton];
}
return self;
}
-(void)setLocationInfo:(NSString *)ID title:(NSString *)title distance:(double)distance description:(NSString *)description imageURL:(NSString *)imageURL
{
_placeID = ID;
[titleLabel setText:title];
[distanceLabel setText:[NSString stringWithFormat:#"%f", distance]];
[descriptionLabel setText:description];
UIImage* image = [UIImage imageNamed:imageURL];
[imageView setImage:image];
}
-(void) pushToLocation
{
NSLog(#"%s", __FUNCTION__);
featureScreenViewController* fsvc = [[featureScreenViewController alloc]init];
[fsvc pushToLocationWithID:_placeID];
}
#end
in featureScreenViewController.m
- (void)viewDidLoad
{
#define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
if (isiPhone5)
{
// this is iphone 4 inch
[scroller setFrame:CGRectMake(0, 251, 320, 317)];
}
else
{
[scroller setFrame:CGRectMake(0, 251, 320, 229)];
}
[self addLocationViews];
}
-(void)addLocationViews
{
int count = (int)[_placeArray count];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(152*count, 317)];
for (int i = 0; i<count; i++) {
_placeObject = [_placeArray objectAtIndex:i];
GSSFeatureScreenLocationTabView * view = [[GSSFeatureScreenLocationTabView alloc]initWithFrame:CGRectMake((152*i), 0, 144, 317)];
[view setLocationInfo:[_placeObject placeID] title:[_placeObject placeName] distance:[_placeObject distacne] description:#"description" imageURL:#"noImage.png"];
[scroller addSubview:view];
}
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.navigationController.navigationBarHidden = YES;
}
-(void) pushToLocationWithID:(NSString*)placeID
{
NSLog(#"\n%s \nplaceID:%#\n\n", __FUNCTION__, placeID);
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
GSSLocationDetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"GSSLocationDetailViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
the function -(void) pushToLocationWithID:(NSString*)placeID is called and the log shows the function and placeID, However it doesn't push to the location view controller.
tried creating a button just to link a segue to it and called the performSegue with identifier, doesn't reach prepare for segue and still doesn't push.
However using the same code from another page in an IBAction and it works. Is there someway to call super or something so it goes into the navController and pushes it ?
If i do presentViewController just to test modal like shown above it shows
Warning: Attempt to present <GSSLocationDetailViewController: 0xab85110> on <featureScreenViewController: 0xab9c230> whose view is not in the window hierarchy!
but if i do
[self.navigationController pushViewController:vc animated:YES];
it doesn't show anything in the log and doesn't push.
made a new project with same concept at http://www.narula-tech.com/testScrollPush.zip
You are in a UIView (GSSFeatureScreenLocationTabView) and you are allocating a new viewController (VC2: featureScreenViewController) from there and you are pushing another viewController (VC3: GSSLocationDetailViewController) from there.
But actually your view is in another viewController VC1 and you need to push VC3 from there and not from VC2 which you are allocating but is not "in the scene" or better, in the window hierarchy.
So you should control touches from the viewController and not from the UIView..for this reason the name: view**CONTROLLER**.
Another thing but not related:
name of class should began with the first letter capitol:
So not:
featureScreenViewController* fsvc;
but:
FeatureScreenViewController* fsvc;
I'd like to make MenuView display with animation.
When I call -showSideMenu, MenuView does not appear.
How do I fix it?
MenuView.h
#import <UIKit/UIKit.h>
#interface SettingView : UIView
#end
MenuView.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.frame = CGRectMake(320, 0, 240, 568);
self.backgroundColor = [UIColor turquoiseColor];
}
return self;
}
MasterViewController.m
#property (nonatomic, strong) MenuView* sideMenuView;
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem* menuButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(showSideMenu:)];
self.navigationItem.rightBarButtonItem = menuButton;
self.sideMenuView = [[MenuView alloc]initWithFrame:self.view.frame];
[self.view addSubview:self.sideMenuView];
}
- (IBAction)showSideMenu:(id)sender
{
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.sideMenuView.frame = CGRectMake(80, 0, 240, 568);
}
completion:^(BOOL finished) {
}];
}
Couple of things:
Put an NSLog in the showSideMenu method. Does it actually fire?
You animate the frame. Do you have AutoLayout turned off for this view controller?
EDITED:
I had created a slide in menu for my view and it works as i wanted, this was the original guide that I used.
http://www.youtube.com/watch?v=79ZQDzzOHLk
My goal was to get this programmed once in a class and get it to work on any view controller that i decided to call it in.
Thanks to #harsh.prasad and some additional research I have managed to get this to work to a point where it works as I want apart apart from linking buttons on to it.
So to update this question in the hope it may help someone else.
This is what I did:
I created a UIView class and called it MenuOne.
MenuOne.h
#import <UIKit/UIKit.h>
#interface TFMenuOne : UIView {
// Pop Up Menu
IBOutlet UIScrollView *scrollView;
IBOutlet UIButton *openMenu;
int draw1;
IBOutlet UIButton *backButton;
}
// Pop Up Menu
- (IBAction)openMenu_clicked:(id)sender;
// Reset draw1 to 0
- (void) resetView: (id) sender;
#property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
#end
MenuOne.m
#import "TFMenuOne.h"
#implementation TFMenuOne
#synthesize scrollView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
draw1 = 0;
scrollView = [[UIScrollView alloc] init];
[scrollView setBackgroundColor:[UIColor whiteColor]];
[scrollView setFrame:CGRectMake(0, 315, 568, 5)];
[scrollView setContentSize:CGSizeMake(568, 5)];
backButton = [[UIButton alloc] init];
[backButton setBackgroundColor:[UIColor greenColor]];
backButton.frame = CGRectMake(224, 350, 120, 30);
openMenu = [[UIButton alloc] init];
[openMenu setBackgroundImage:[UIImage imageNamed:#"menu-button-#2.png"]
forState:UIControlStateNormal];
openMenu.adjustsImageWhenHighlighted = NO;
[openMenu addTarget:self
action:#selector(openMenu_clicked:)
forControlEvents:UIControlEventTouchUpInside];
openMenu.frame = CGRectMake(256, 269, 64, 46);
[self addSubview:scrollView];
[self addSubview:backButton];
[self addSubview:openMenu];
}
return self;
}
// Allow for touch even through transparent View class
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
for (UIView *view in self.subviews) {
if (!view.hidden && view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event])
return YES;
}
return NO;
}
- (void) resetView: (id) sender {
draw1 = 1;
[self openMenu_clicked:sender];
}
- (IBAction)openMenu_clicked:(id)sender {
if (draw1 == 0) {
draw1 = 1;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
scrollView.frame = CGRectMake(0, 260, 568, 60);
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
openMenu.frame = CGRectMake(256, 214, 64, 46);
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
backButton.frame = CGRectMake(224, 275, 120, 30);
} completion:^(BOOL finished) {
}];
} else {
draw1 = 0;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
scrollView.frame = CGRectMake(0, 315, 568, 5);
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
openMenu.frame = CGRectMake(256, 269, 64, 46);
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
backButton.frame = CGRectMake(224, 350, 120, 30);
} completion:^(BOOL finished) {
}];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
After a lot of trial and error, in order to get this UIView class to appear on multiple ViewControllers I call the view like this in the m file of the view controller. The obstacles I ran in to was that the menu would open but when I left the view controller to go to another view controller the menu would be in the state it was in when i left, it wouldn't reset back to closed. The code below covered that thanks again to #harsh.prasad. I also managed to get the menu to animate in.
#interface TFMapViewController ()
{
TFMenuOne *menuView;
}
#end
#implementation TFMapViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[menuView resetView:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
menuView = [[TFMenuOne alloc] initWithFrame:CGRectMake(0, 51, 568, 320)];
[self.view addSubview:menuView];
}
- (void) viewDidAppear:(BOOL)animated
{
[UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
menuView.frame = CGRectMake(0, 0, 568, 320);
} completion:^(BOOL finished) {
}];
}
- (void) viewDidDisappear:(BOOL)animated
{
[menuView resetView:nil];
[UIView animateWithDuration:0.0 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
menuView.frame = CGRectMake(0, 51, 568, 320);
} completion:^(BOOL finished) {
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// Allows for Exit button to work
- (IBAction)returned:(UIStoryboardSegue *)segue {
}
#end
I you want you can use tabsController if that kind of feature you want, otherwise create a class of your slide in menu view and then you can use it in all of your views as you like. No same piece of code is to be rewritten.
EDIT:
Suppose you have created a class CustomMenuView which basically creates your menu view. So now you can use this in every view controller you want to use it in in the following way:
CustomMenuView *menuView = [CustomMenuView alloc] initWithFrame:CGRectMake(0, 200, 320, 100);
menuView.<properties you want to pass> = <set properties here>;
[self.view addSubView:menuView];
This will set the view with the custom menu and then you can handle the actions in this menu.
1)Here is an old tutorial for creating menu - http://www.raywenderlich.com/32054/how-to-create-a-slide-out-navigation-like-facebook-and-path/projectlayout
2)A better way is to create Drawer menu With container views. You can read more about container views from WWDC videos.
3)Or if you are lazy to d it yourself, try this library http://cocoacontrols.com/platforms/ios/controls/jtrevealsidebar
P.S. No , you do not havev to repeat the code.
You can try this,
First make a view on the origin point x = 0 y = 480 (for iphone4) and then run this code.
CGRect theFrame = self.viewMenuShare.frame;
theFrame.origin = CGPointMake(0, 480);
self.viewMenuShare.frame = theFrame;
theFrame.origin = CGPointMake(0,480 - 187);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
self.viewMenuShare.frame = theFrame;
[UIView commitAnimations];