Calling view controller method from delegate class, having issues - iphone

I am calling view controller method from delegate class, the method is called but uitableview declared in that method does not call it delegate methods and a buttons and view also alloc in that view but nothing is shown.
Explaining from begining:
On navigation controller needed a button and to be shown on all views. So, i took that in delegate this way.
UIButton *btnMenuOpen = [UIButton buttonWithType:UIButtonTypeCustom];
btnMenuOpen.frame = CGRectMake(0, 15, 40, 56);
[btnMenuOpen setImage:[UIImage imageNamed:#"side_menu.png"] forState:UIControlStateNormal];
[btnMenuOpen addTarget:self action:#selector(menu) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btnMenuOpen];
-(void)menu
{
ViewController *viewC = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[viewC menu];
}
This is the view controller class:
-(void)menu
{
NSLog(#"menu opened");
self.mMenuView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 240, 480)];
self.mMenuView.backgroundColor = [UIColor grayColor];
self.mMenuView.autoresizingMask = 0;
[self.navigationController.view addSubview:self.mMenuView];
self.mSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 16, 220, 44)];
self.mSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.mSearchBar.backgroundImage = [UIImage imageNamed:#"slide_tab_button.png"];
self.mSearchBar.delegate = self;
[self.mMenuView addSubview:self.mSearchBar];
UIButton *btnMenuClose = [UIButton buttonWithType:UIButtonTypeCustom];
btnMenuClose.frame = CGRectMake(215, 19, 40, 44);
[btnMenuClose setImage:[UIImage imageNamed:#"side_menu.png"] forState:UIControlStateNormal];
[btnMenuClose addTarget:self action:#selector(menu_close) forControlEvents:UIControlEventTouchUpInside];
[self.mMenuView addSubview:btnMenuClose];
self.mMenuTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 61,
240, 420) style:UITableViewStylePlain];
self.mMenuTableView.delegate = self;
self.mMenuTableView.dataSource = self;
[self.mMenuView addSubview:self.mMenuTableView];
[self.mMenuTableView reloadData];
}
Now, nothing is displyed when this method is called, control goes through it but nothing happens, no delegates of table are called and neither other stuff works(button searchbar and uiview).
Please guide for the above. Thanks in advance.

Try to use of NSNotificationCenter like this
Register Notification in your ViewController.m
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(menu)
name: #"myMenuNotification"
object: nil];
}
And in your AppDelegate.m call that method using notification like
-(void)menu
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"myMenuNotification" object:nil];
}

I don't know how you are using. But I have checked code with some modification. it is working fine.
Your modified code is:
In AppDelegate method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[sampleViewController alloc] initWithNibName:#"sampleViewController" bundle:nil];
self.navController=[[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
UIButton *btnMenuOpen = [UIButton buttonWithType:UIButtonTypeCustom];
btnMenuOpen.frame = CGRectMake(0, 15, 40, 56);
[btnMenuOpen setImage:[UIImage imageNamed:#"5.jpg"] forState:UIControlStateNormal];
[btnMenuOpen addTarget:self action:#selector(menu) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btnMenuOpen];
[self menu];
return YES;
}
-(void)menu
{
[self.viewController menu];
}
//In view Controller............
-(void)menu
{
NSLog(#"menu opened");
UIView *mMenuView;
UISearchBar *mSearchBar;
UITableView *mMenuTableView;
mMenuView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 240, 480)];
mMenuView.backgroundColor = [UIColor grayColor];
mMenuView.autoresizingMask = 0;
[self.navigationController.view addSubview:mMenuView];
mSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 16, 220, 44)];
mSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
mSearchBar.backgroundImage = [UIImage imageNamed:#"5.jpg"];
// mSearchBar.delegate = self;
[mMenuView addSubview:mSearchBar];
UIButton *btnMenuClose = [UIButton buttonWithType:UIButtonTypeCustom];
btnMenuClose.frame = CGRectMake(215, 19, 40, 44);
[btnMenuClose setImage:[UIImage imageNamed:#"side_menu.png"] forState:UIControlStateNormal];
[btnMenuClose addTarget:self action:#selector(menu_close) forControlEvents:UIControlEventTouchUpInside];
[mMenuView addSubview:btnMenuClose];
mMenuTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 61, 240, 420) style:UITableViewStylePlain];
// mMenuTableView.delegate = self;
// mMenuTableView.dataSource = self;
[mMenuTableView setBackgroundColor:[UIColor blueColor]];
[mMenuView addSubview:mMenuTableView];
[mMenuTableView reloadData];
}
Screenshot

Related

Navigationbar button not showing

I have UINavigationController. Then i need to display navigationbar button. I used following code:
UINavigationController *nav=[[UINavigationController alloc] init];
[self.view addSubview:nav.view];
UIImage *info_iphone=[UIImage imageNamed:#"button.png"];
UIButton *infobtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 48, 30)];
[infobtn setBackgroundImage:info_iphone forState:UIControlStateNormal];
[infobtn addTarget:self action:#selector(show_info:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:infobtn];
Sample Code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.viewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
In YourViewController's -viewWillAppear :
UIImage *info_iphone=[UIImage imageNamed:#"button.png"];
UIButton *infobtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 48, 30)];
[infobtn setBackgroundImage:info_iphone forState:UIControlStateNormal];
[infobtn addTarget:self action:#selector(show_info:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:infobtn];
self.navigationItem.rightBarButtonItem = rightButton;
I think that the problem here is with the navigation controller. You can't add it as a subview of the UIViewControllers view. I am using story boards and there you can embed your View Controllers with Navigation Controllers.
Here, your "nav" does not know that it should care about your navigationItem. Your view controller is not in his viewControllers list.
I don't think adding UINavigationController as subview will work for you.
Rather you can add UINavigationController as subview(or as rootviewcontroller) to UIWindow in your AppDelegate file in didFinishLaunchingWithOptions method as follow :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[SampleViewController alloc] initWithNibName:#"SampleViewController" bundle:nil] autorelease];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Than in your SampleViewController's Viewdidload method add the BarButton as follow :
UIImage *info_iphone=[UIImage imageNamed:#"button.png"];
UIButton *infobtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 48, 30)];
[infobtn setBackgroundImage:info_iphone forState:UIControlStateNormal];
[infobtn addTarget:self action:#selector(show_info:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:infobtn]];
Hopefully this will help you.. :)
use this bellow code...
UIImage *info_iphone=[UIImage imageNamed:#"button.png"];
UIButton *infobtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 48, 30)];
[infobtn setBackgroundImage:info_iphone forState:UIControlStateNormal];
[infobtn addTarget:self action:#selector(show_info:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:infobtn];
[infobtn release];
UPDATE :
use bellow code
UIBarButtonItem *btnSave = [[UIBarButtonItem alloc]init];
btnSave.target = self;
btnSave.action = #selector(btnSave_Clicked:);
btnSave.title = #"Save";
btnSave.style=UIBarButtonItemStyleDone;
self.navigationController.topViewController.navigationItem.rightBarButtonItem = btnSave;

Custom view as a tabbar, pushViewController not working [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Using custom view as a tab bar, it is declared in delegate so that it ca be available on all views. But the issue is the pushViewController is not implementing there. I am using the custom view to get the navigation controller animation on bottom bar because this can not be implemented on tab bar. So, how can i perform pushViewController to the bar buttons.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.homeViewController = [[[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil] autorelease];
navController = [[UINavigationController alloc]initWithRootViewController:self.homeViewController];
navController.navigationBar.barStyle = UIBarStyleBlack;
[self.window addSubview:self.navController.view];
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; [appDelegate addCustomBottomBar];
[self.window makeKeyAndVisible];
return YES;
}
-(void)addCustomBottomBar{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.window cache:YES];
viewBotBar = [[UIView alloc]initWithFrame:CGRectMake(0, 402, 320, 79)];
viewBotBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"1aa.png"]];
[self.window addSubview:viewBotBar];
[self.window bringSubviewToFront:viewBotBar];
[UIView commitAnimations];
UIButton *btnHome = [UIButton buttonWithType:UIButtonTypeCustom];
btnHome.frame = CGRectMake(0, 25, 54, 50);
[btnHome setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[btnHome addTarget:self action:#selector(home) forControlEvents:UIControlEventTouchUpInside];
[viewBotBar addSubview:btnHome];
UIButton *btnLoc = [UIButton buttonWithType:UIButtonTypeCustom];
btnLoc.frame = CGRectMake(58, 25, 63, 50);
[btnLoc setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[btnLoc addTarget:self action:#selector(loc) forControlEvents:UIControlEventTouchUpInside];
[viewBotBar addSubview:btnLoc];
UIButton *btnSer = [UIButton buttonWithType:UIButtonTypeCustom];
btnSer.frame = CGRectMake(124, 25, 63, 50);
[btnSer setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[btnSer addTarget:self action:#selector(ser) forControlEvents:UIControlEventTouchUpInside];
[viewBotBar addSubview:btnSer];
UIButton *btnBook = [UIButton buttonWithType:UIButtonTypeCustom];
btnBook.frame = CGRectMake(190, 25, 63, 50);
[btnBook setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[btnBook addTarget:self action:#selector(book) forControlEvents:UIControlEventTouchUpInside];
[viewBotBar addSubview:btnBook];
UIButton *btnMore = [UIButton buttonWithType:UIButtonTypeCustom];
btnMore.frame = CGRectMake(256, 25, 62, 50);
[btnMore setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[btnMore addTarget:self action:#selector(more) forControlEvents:UIControlEventTouchUpInside];
[viewBotBar addSubview:btnMore];
btnDwn = [UIButton buttonWithType:UIButtonTypeCustom];
btnDwn.frame = CGRectMake(135, 0, 45, 28);
[btnDwn setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[btnDwn addTarget:self action:#selector(down) forControlEvents:UIControlEventTouchUpInside];
btnDwn.tag = 1;
[viewBotBar addSubview:btnDwn];
}
-(void)down
{
if (btnDwn.tag == 1) {
// perform your required functionality
btnDwn.tag = 2;
CGRect newFrameSize = CGRectMake(0, 452, 320, 28);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.0];
viewBotBar.frame = newFrameSize;
viewBotBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bottom-hidden.png"]];
// self.tabController.tabBar.hidden = YES;
[UIView commitAnimations];
}
else if (btnDwn.tag == 2) {
// perform your required functionality
btnDwn.tag = 1;
CGRect newFrameSize = CGRectMake(0, 402, 320, 79);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.0];
viewBotBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"1aa.png"]];
viewBotBar.frame = newFrameSize;
//self.tabController.tabBar.hidden = NO;
[UIView commitAnimations];
}
}
-(void)loc
{
LocationViewController *location = [[LocationViewController alloc]init];
[self.navigationController pushViewController:location animated:YES];
}
-(void)ser
{
ServicesViewController *services = [[ServicesViewController alloc]initWithNibName:#"ServicesViewController" bundle:nil];
[self.navigationController pushViewController:services animated:YES];
}
-(void)book
{
BookingViewController *book = [[BookingViewController alloc]initWithNibName:#"BookingViewController" bundle:nil];
[self.navigationController pushViewController:book animated:YES];
}
-(void)more
{
MoreViewController *more = [[MoreViewController alloc]initWithNibName:#"MoreViewController" bundle:nil];
[self.navigationController pushViewController:more animated:YES];
}
use
self.window.rootViewController = self.navController;
instead of
[self.window addSubview:self.navController.view];
see these custom UITabBar Demos Links..
Many Demos
PeekabooTabBarController

Adding a view to UIScrollview

I have created a UIView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIImage *image2 = [UIImage imageNamed:#"pinGreen_v1.png"];
UIImage *image1 = [UIImage imageNamed:#"PinDown1.png"];
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
[btn1 setImage:image2 forState:UIControlStateNormal];
[self addSubview:btn1];
for(int i =1; i<20; i++){
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(40 * i, 40, 40, 40)];
[btn2 setImage:image1 forState:UIControlStateNormal];
[btn2 addTarget:self
action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
}
return self;
}
With the above code I am drawing images in my view. I am loading this view from my view controller. I need to put this view inside a UIScrollview. Where should I create the scroll view? In the above view or the view controller where the above view is created.
The code inside my view controller is :
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[drawLines release];
I should be able to scroll through the images inside my view.
I would create the the scrollview in the viewcontroller and add the view to it.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[scroll addSubview:drawLines];
scroll.contentSize = CGSizeMake(self.view.frame.size.width*drawLines.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroll];
[drawLines release];
[scroll release];

How to disappear uiview by a button click

I have created a unbutton programatically and made an action
{
UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton1.frame = CGRectMake(20, 20, 80, 44);
[myButton1 setTitle:#"Click Me!" forState:UIControlStateNormal];
[myButton1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton1];
}
and in IBAction i have a UIView as subview like this
- (IBAction)buttonClicked:(id)sender
{
CGRect myFrame = CGRectMake(0, 300, 100, 50);
UIView *myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myView];
[myView release];
}
What i now want is subview have to disappear when the user second time press myButton1. How can i do that?
Do some correction in our code -
**Action like this -**
- (IBAction)buttonClicked:(id)sender
{
UIView *myView;
if([self.view viewWithTag:56])
{
[[self.view viewWithTag:56] removeFromSuperview];
}
else
{
CGRect myFrame = CGRectMake(0, 300, 100, 50);
myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor lightGrayColor];
[myView setTag:56];
[self.view addSubview:myView];
[myView release];
}
}
you can use [view removeFromSuperView] method to remove a view.
Change your buttonClicked: method to this.
- (void)buttonClicked:(id)sender
{
UIView *myView;
if(myView=[self.view viewWithTag:1234])
{
[myView removeFromSuperview];
}
else {
CGRect myFrame = CGRectMake(0, 300, 100, 50);
myView= [[UIView alloc] initWithFrame:myFrame];
[myView setTag:1234];
myView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myView];
[myView release];
}
[self.view setNeedsDisplay];
}
EDIT: It doesn't read well if you use IBAction and not use the Interface Builder.

How to add Date picker in Action Sheet?

- (IBAction) showCatPicker {
if (self.catList !=nil) {
self.catList=nil;
[catList release];
}
self.catList = [[NSMutableArray alloc] init];
self.actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
if(self.catPicker == nil) {
self.catPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
self.catPicker.showsSelectionIndicator = YES;
self.catPicker.dataSource = self;
self.catPicker.opaque = YES;
self.catPicker.multipleTouchEnabled = YES;
self.catPicker.userInteractionEnabled = YES;
self.catPicker.delegate = self;
}
[self.actionSheet addSubview:self.catPicker];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Select"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor colorWithRed:19.0/255 green:122.0/255 blue:53.0/255 alpha:1.0];
[closeButton addTarget:self action:#selector(dismissGenderPicker:) forControlEvents:UIControlEventValueChanged];
UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Cancel"]];
cancelButton.momentary = YES;
cancelButton.frame = CGRectMake(10, 7.0f, 50.0f, 30.0f);
cancelButton.segmentedControlStyle = UISegmentedControlStyleBar;
cancelButton.tintColor = [UIColor colorWithRed:19.0/255 green:122.0/255 blue:53.0/255 alpha:1.0];
[cancelButton addTarget:self action:#selector(cancelActionSheet) forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:cancelButton];
[self.actionSheet addSubview:closeButton];
[cancelButton release];
[closeButton release];
[self.actionSheet showInView:self.view];
[self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
[catPicker reloadComponent:0];
}
You should definitely NOT use UIActionSheet to hold an UIDatePicker because this functionality is deprecated!
From the Documentation:
Subclassing Notes
UIActionSheet is not designed to be subclassed, nor should you add
views to its hierarchy. If you need to present a sheet with more
customization than provided by the UIActionSheet API, you can create
your own and present it modally with
presentViewController:animated:completion:.
and
Important: UIActionSheet is deprecated in iOS 8. (Note that
UIActionSheetDelegate is also deprecated.) To create and manage action
sheets in iOS 8 and later, instead use UIAlertController with a
preferredStyle of UIAlertControllerStyleActionSheet.
What you could very easily do is to create an UIView to hold the UIDatePicker and animate the view as appropriate. You can even add an UIToolbar to it if you need to.
Here's an example:
Create two properties:
#property (strong, nonatomic) UIDatePicker *theDatePicker;
#property (strong, nonatomic) UIView *pickerView;
Create your picker, embed it into the UIView and show:
-(void) createDatePickerAndShow {
UIToolbar *controlToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, pickerView.bounds.size.width, 44)];
[controlToolbar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:#"Set" style:UIBarButtonItemStyleDone target:self action:#selector(dismissDateSet)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelDateSet)];
[controlToolbar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO];
[theDatePicker setFrame:CGRectMake(0, controlToolbar.frame.size.height - 15, theDatePicker.frame.size.width, theDatePicker.frame.size.height)];
if (!pickerView) {
pickerView = [[UIView alloc] initWithFrame:theDatePicker.frame];
} else {
[pickerView setHidden:NO];
}
CGFloat pickerViewYpositionHidden = self.view.frame.size.height + pickerView.frame.size.height;
CGFloat pickerViewYposition = self.view.frame.size.height - pickerView.frame.size.height;
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYpositionHidden,
pickerView.frame.size.width,
pickerView.frame.size.height)];
[pickerView setBackgroundColor:[UIColor whiteColor]];
[pickerView addSubview:controlToolbar];
[pickerView addSubview:theDatePicker];
[theDatePicker setHidden:NO];
[self.view addSubview:pickerView];
[UIView animateWithDuration:0.5f
animations:^{
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYposition,
pickerView.frame.size.width,
pickerView.frame.size.height)];
}
completion:nil];
}
And to dismiss the DatePicker:
-(void) cancelDateSet {
CGFloat pickerViewYpositionHidden = self.view.frame.size.height + pickerView.frame.size.height;
[UIView animateWithDuration:0.5f
animations:^{
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYpositionHidden,
pickerView.frame.size.width,
pickerView.frame.size.height)];
}
completion:nil];
}
self.view.userInteractionEnabled = NO;
self.blurView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.blurView setBackgroundColor:[UIColor lightGrayColor]];
[self.blurView setAlpha:0.3];
[[[UIApplication sharedApplication] delegate].window addSubview:self.blurView];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame];
pickerView.datePickerMode = UIDatePickerModeDate;
[pickerView setBackgroundColor:[UIColor whiteColor]];
[pickerView addTarget:self action:#selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
__block CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, 220);
self.viewForPicker = [[UIView alloc] initWithFrame:frame];
[self.viewForPicker setBackgroundColor:[UIColor whiteColor]];
[self.viewForPicker addSubview:pickerView];
[[[[UIApplication sharedApplication] delegate] window] addSubview:self.viewForPicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[button setTitle:#"Done" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(250, 15, 60, 30)];
[button.layer setCornerRadius:5.0];
[button.layer setBorderWidth:1.0];
[button.layer setBorderColor:[[IMAppStyle appColor] CGColor]];
[self.viewForPicker addSubview:button];
[self.viewForPicker.layer setCornerRadius:8.0];
float animationDuration = 0.25;
[UIView animateWithDuration:animationDuration animations:^{
frame.origin.y -= (180+40+35);
[self.viewForPicker setFrame:frame];
}];