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

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

Related

Add Button on my View

I'm creating a hidden form containing 3 buttons at the bottom of the parent view, by pressing another button, the view becomes visible, but those 3 buttons are not pressed, I don `t know what the problem is.
I add a button via code. Here is a method that is called when the button is pressed.
- (IBAction) addData:(id)sender
{
if(attachInfo==nil){
float screenHeight = self.view.frame.size.height;
attachInfo=[[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, 216)];
UIButton *attachImage = [UIButton buttonWithType:UIButtonTypeCustom];
//attachImage.enabled = YES;
[attachImage addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
[attachImage setFrame:CGRectMake(20, 20, 0, 0)];
[attachImage setImage:[UIImage imageNamed:#"add_photos"] forState:UIControlStateNormal];
[attachImage sizeToFit];
[attachInfo addSubview:attachImage];
UIButton *attachDoc = [UIButton buttonWithType:UIButtonTypeCustom];
//[pushButton addTarget:self action:#selector(pushViewController) forControlEvents:UIControlEventTouchUpInside];
[attachDoc setFrame:CGRectMake(120, 20, 80, 80)];
[attachDoc setImage:[UIImage imageNamed:#"add_docs.png"] forState:UIControlStateNormal];
[attachDoc sizeToFit];
[attachInfo addSubview:attachDoc];
UIButton *attachPlace = [UIButton buttonWithType:UIButtonTypeCustom];
//[pushButton addTarget:self action:#selector(pushViewController) forControlEvents:UIControlEventTouchUpInside];
[attachPlace setFrame:CGRectMake(220, 20, 80, 80)];
[attachPlace setImage:[UIImage imageNamed:#"add_place.png"] forState:UIControlStateNormal];
[attachPlace sizeToFit];
[attachInfo addSubview:attachPlace];
//[self.view addSubview:attachInfo];
[self.view addSubview:attachInfo];
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y -= 216;
attachInfo.superview.frame=frame;
}];
}
else
{
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y += 216;
attachInfo.superview.frame=frame;
}];
attachInfo=nil;
}
}
you need to add Action of buttons.
- (IBAction)first:(id)sender;
{
firstViewController * fvc=[FirstViewController alloc]init];
[FirstButton setHidden:False];
[self.navigationController pushViewController:fvc animated:YES];
}
- (IBAction)Second:(id)sender;
{
SecondViewController * svc=[SecondViewController alloc]init];
[SecondButton setHidden:False];
[self.navigationController pushViewController:svc animated:YES];
}
- (IBAction)Third:(id)sender;
{
ThirdViewController * tvc=[ThirdViewController alloc]init];
[ThirdButton setHidden:False];
[self.navigationController pushViewController:fvc animated:YES];
}
In the first button (attachImage), the height and the width is zero. So it should not be visible. For the other two buttons you haven't set the target. So those are not clickable.
for the first button set
[attachImage setFrame:CGRectMake(20, 20, 80, 80)];
and for the other two buttons add
//for the second button
[attachDoc addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
//for the third button
[attachPlace addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
-(void)createHiddenForm
{
hiddenView = [[UIView alloc] initWithFrame:CGRectMake(0,100,320,300)];
[hidden setHidden:TRUE];
[self.view addSubview:hiddenView];
float posY = 50.0;
for (int i=0 ; i<3 ; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(50,posY, 100, 50)];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[hiddenView addSubview:button];
posY=posY+60;
}
}
-(void)showHiddenView
{
hidden.hidden = FALSE;
[self.view bringSubviewToFront:hiddenView];
}
-(void)buttonClicked:(id)sender
{
UIButton *senderBtn = (UIButton *)sender;
NSLog(#"Sender Button's tag = %#",senderBtn.tag);
}
Hope it will help you.
float screenHeight = self.view.frame.size.height;
attachInfo=[[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, 216)];
From the above, u r adding the attachInfo Y position outside the view boundaries.
Button onclick won't work when added outside the view.
To make it work replace below line.
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y -= 216;
attachInfo.superview.frame=frame;
Instead of moving superview frame move attachInfo frame like this.
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.frame;
frame.origin.y -= 216;
attachInfo.frame=frame;
So that attachInfo view will be added to the super view boundaries with animation.

Calling view controller method from delegate class, having issues

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

how to remove a view?

I have an iphone application in which i am adding a view controllers view with an animation like flipfrom left.I was doing it with navigationbarbutton item.when clicking on it i was adding a view controllers view to my rootview.and changing the button amd in that action i am removing that view and adding the previous button like that.but the pbm i need to remove that info view with in a button action inside that view.here is my code for the flipanimation.`
- (void)backPressed1
{
InfoViewController *infoviewcontroller = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.95];
//[UIView beginAnimations:nil context:NULL];
//[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
//CGRect frame=CGRectMake(0,0,320,480);
[rootview removeFromSuperview];
//[infoviewcontroller.view setFrame:frame];
//selfhelpscoresAppDelegate *appdelegate=[[UIApplication sharedApplication] delegate];
[infoviewcontroller viewWillAppear:YES];
[self.view addSubview:infoviewcontroller.view];
[UIView commitAnimations];
UIImage *buttonImage = [UIImage imageNamed:#"information_btn.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 42, 32);
[button addTarget:self action:#selector(backpressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = customBarItem;
//UILabel *label =[[[UILabel alloc]init]autorelease];
self.navigationItem.title=#"Tutorial";
//label.text = #"";
//self.navigationController.title = #"";
[customBarItem release];
}
-(IBAction)backpressed
{
InfoViewController *infoviewcontroller = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.95];
//[UIView beginAnimations:nil context:NULL];
//DailypercentageViewController *pieChart = [[DailypercentageViewController alloc] initWithNibName:#"DailypercentageViewController" bundle:nil];
//[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
//CGRect frame=CGRectMake(0,0,320,370);
[infoviewcontroller.view removeFromSuperview];
[self.view addSubview:rootview];
[UIView commitAnimations];
//[rearView setFrame:frame];
//[pieChart viewDidAppear:YES];
UIImage *buttonImage1 = [UIImage imageNamed:#"information_btn.png"];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:buttonImage1 forState:UIControlStateNormal];
button1.frame = CGRectMake(0, 0, 42, 32);
[button1 addTarget:self action:#selector(backPressed1) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
self.navigationItem.rightBarButtonItem = customBarItem1;
[customBarItem1 release];
//UILabel *label1 =[[[UILabel alloc]init]autorelease];
self.navigationItem.title=#"";
//label1.text = #"Home";
//self.title= #"Home";
}
`my problem is how i can remove this infoview and came back to the rootview from with in the button action from the infoview?

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];
}];