I am trying to display action sheet in iPad which will contain 2 buttons and date pickerview.
I displayed the action sheet in iphone properly but when I try to display the action sheet in iPad its just displaying one thick black line.
Currently its looking like :
and code for displaying action sheet is
UIButton *btnClicked=(UIButton *)sender;
menu = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
/* menu=[[UIActionSheet alloc] initWithTitle:#"This will remove this recipe from all synced devices as well. Are you sure?" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Confirm Delete Recipe" otherButtonTitles:nil];*/
[menu setTag:121];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,5,300,20)];
[titleLabel setFont:[UIFont fontWithName:#"Trebuchet MS" size:18.0f]];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setNumberOfLines:1];
[menu addSubview: titleLabel];
UIButton *cancelButton = [UIButton buttonWithType: UIButtonTypeCustom];
cancelButton.frame = CGRectMake(30, 30, 124, 43);
[cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel_button.png"] forState: UIControlStateNormal];
[cancelButton addTarget:self action:#selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
cancelButton.adjustsImageWhenHighlighted = YES;
cancelButton.tag=33;
[menu addSubview: cancelButton];
UIButton *okButton = [UIButton buttonWithType: UIButtonTypeCustom];
okButton.frame = CGRectMake(170, 30, 124, 43);
[okButton setBackgroundImage:[UIImage imageNamed:#"continue_button.png"] forState: UIControlStateNormal];
[okButton addTarget:self action:#selector(okButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
okButton.adjustsImageWhenHighlighted = YES;
[menu addSubview: okButton];
datePickerView = [[UIDatePicker alloc] init];
CGRect pickerRect = datePickerView.bounds;
pickerRect.origin.y = 75;
datePickerView.frame = pickerRect;
//---Picker view for from date selection
if (btnClicked.tag==51)
{
[titleLabel setText:SELECT_FROM_DATE];
NSDate *now = [Global todayDate];
int daysToAdd = 1;
NSDate *newDate = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
// NSLog(#"%#",strDtFromDate);
datePickerView.minimumDate = newDate;
if (holidayType==2)
{
NSDate *holidayFromDate=[dateFormat dateFromString:objectSelectedGroup.fromDate];
// newDate=[holidayFromDate dateByAddingTimeInterval:60*60*24*daysToAdd];
if ([[Global todayDate] compare:holidayFromDate] == NSOrderedAscending)
{
datePickerView.minimumDate = holidayFromDate;
}
else if([[Global todayDate] compare:holidayFromDate] == NSOrderedDescending)
{
datePickerView.minimumDate = newDate;
}
NSDate *holidayToDate=[dateFormat dateFromString:objectSelectedGroup.toDate];
NSLog(#"%#",objectSelectedGroup.fromDate);
NSLog(#"%#",objectSelectedGroup.toDate);
datePickerView.maximumDate = holidayToDate;
}
okButton.tag=34;
}
else if(btnClicked.tag==52)
{
[titleLabel setText:SELECT_TO_DATE];
datePickerView.minimumDate=stringDate;
NSDate *holidayToDate=[dateFormat dateFromString:objectSelectedGroup.toDate];
datePickerView.maximumDate=holidayToDate;
okButton.tag=35;
}
datePickerView.datePickerMode = UIDatePickerModeDate;
stringDate=[datePickerView date];
[datePickerView addTarget:self action:#selector(selectedDateChanged:) forControlEvents:UIControlEventValueChanged];
[menu addSubview:datePickerView];
//menu.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
[menu showInView:self.view];
[menu setFrame:CGRectMake(0,self.view.frame.size.height - 290,320,290)];
And same code is working fine for iphone.
How to solve this?
Use PopOver Controller to display action sheet on ipad. To use actionsheet on Popover controller refer the following links,
UIDatePicker in UIActionSheet on iPad
UIActionSheet on iPad frame too small
UIActionaSheet is not fit for IPad and they most time behave unpredicted around frame
I would recommend a UIPopoverController with a custom UIViewController for doing this kinda user experience.
you can try replacing last two line with following lines,
[menu setFrame:CGRectMake(0,self.view.frame.size.height - 290,320,290)];
[menu showInView:[[[UIApplication sharedApplication] delegate] window]];
Related
Just a few points before I start:
I have just taken over this code
I have little to no experience in iOS code
Consider me a noob at this so go easy on me :P
I have some code below, which used to show a UIActionSheet. In iOS 8 UIActionSheet has been removed (Thanks apple!) so I have started to change it all to use UIAlertController...
I ran into many problems which I have solved, now I am completely confused and need some experienced help from you guys and girls....!
Here is a screenshot
Now as you can see the UIAlertController is being displayed but the UIView is places incorrectly and because of this the controls are impossible to use! You can drag the numbers as expected but it's not as easy...
There are three controllers as you can see, one for choosing a date time, one for number of passengers and the other for choosing a vehicle type.
I just need a nice simple fix that will work on all iOS8 phones and it can be slightly dodgy as we are about to hire someone to re-write the entire code as the code is pretty bad overall and we want to redesign the app from the bottom up.
If you want any more information do let me know, once again my iOS coding experience is very small ( I Do want to learn as I am enjoying it, albeit not as much as Android)
else if (selectedTableField == travelTimeText || selectedTableField == noPassengers || selectedTableField == carType) {
// setup actionsheet to contain the UIPicker
if (version < 8)
{
actionSheet = [[UIActionSheet alloc] initWithTitle:#" "
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet showInView:self.view];
}
if (version >= 8)
{
alertController = [UIAlertController alertControllerWithTitle:#"title"
message:#"messages"
preferredStyle:UIAlertControllerStyleAlert];
}
//CGRect actionSheetBounds = CGRectMake(0,0,320 + offset, 570);
CGRect actionSheetBounds;
if (selectedTableField == travelTimeText)
// actionSheetBounds = CGRectMake(0,0,320 + offset, 655);
if (version >= 8)
{
[alertController.view setBounds:actionSheetBounds];
}
else
{
[actionSheet setBounds:actionSheetBounds];
}
UIView *bgView = [[UIView alloc] initWithFrame:actionSheetBounds];
if (version < 7)
bgView.backgroundColor = [[[ConfigManager sharedInstance].skin valueForKey:#"popupBackgroundColour"] toUIColor];
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// swap asap and select around if on travel time picker
if (selectedTableField == travelTimeText) {
doneBtn.frame = CGRectMake(10+bOffset, 305, 300, 40);
} else {
doneBtn.frame = CGRectMake(10+bOffset, 250, 300, 40);
}
[doneBtn addTarget:self action:#selector(pickerDone:) forControlEvents:UIControlEventTouchUpInside];
[doneBtn setBackgroundImage:[UIImage imageNamed:#"button_large.png"] forState:UIControlStateNormal];
[doneBtn setTitle:#"Select" forState:UIControlStateNormal];
doneBtn.titleLabel.font = [UIFont boldSystemFontOfSize:19];
doneBtn.titleLabel.textColor = [[[ConfigManager sharedInstance].skin valueForKey:#"popupButtonFontColour"] toUIColor];
[bgView addSubview:doneBtn];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320 + offset, 20)];
title.font = [UIFont boldSystemFontOfSize:12];
title.backgroundColor = [UIColor clearColor];
title.textAlignment = UITextAlignmentCenter;
title.textColor = [[[ConfigManager sharedInstance].skin valueForKey:#"popupLabelFontColour"] toUIColor];
if (selectedTableField == travelTimeText) {
title.text = #"Booking Time";
UIButton *asapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
asapBtn.frame = CGRectMake(10 +bOffset, 250, 300, 40);
[asapBtn addTarget:self action:#selector(pickerAsap:) forControlEvents:UIControlEventTouchUpInside];
[asapBtn setTitle:#"As soon as possible" forState:UIControlStateNormal];
[asapBtn setBackgroundImage:[UIImage imageNamed:#"button_large.png"] forState:UIControlStateNormal];
asapBtn.titleLabel.font = [UIFont boldSystemFontOfSize:19];
asapBtn.titleLabel.textColor = [[[ConfigManager sharedInstance].skin valueForKey:#"popupButtonFontColour"] toUIColor];
[bgView addSubview:asapBtn];
} else if (selectedTableField == noPassengers)
title.text = #"Passengers";
else
title.text = #"Vehicle";
[bgView addSubview:title];
[title release];
if (version < 8)
{
[actionSheet addSubview:bgView];
}
else
{
[alertController.view addSubview:bgView];
}
[bgView release];
// travel time uses a different picker
if (selectedTableField == travelTimeText) {
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0 + bOffset, 20, 0.0, 0.0)];
[datePicker addTarget:self action:#selector(travelTimeChanged:) forControlEvents:UIControlEventValueChanged];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
datePicker.minuteInterval = 5;
if (travelTime == nil)
datePicker.date = [[NSDate date] addTimeInterval:15*60];
else
datePicker.date = travelTime;
if (version < 8)
{
[actionSheet addSubview:datePicker];
}
else
{
[alertController.view addSubview:datePicker];
}
// update table field to show now
[self travelTimeChanged:datePicker];
[datePicker release];
if (version >= 8)
{
[self presentViewController:alertController animated:NO completion:nil];
}
} else {
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0 + bOffset, 20, 0.0, 0.0)];
picker.dataSource = self;
picker.delegate = self;
if (selectedTableField == noPassengers) {
picker.showsSelectionIndicator = YES;
[picker selectRow:[passengerOptions indexOfObject:noPassengers.text] inComponent:0 animated:NO];
} else if (selectedVehicle != nil) {
[picker selectRow:[filteredCarOptions indexOfObject:selectedVehicle] inComponent:0 animated:NO];
}
if (version < 8)
{
[actionSheet addSubview:picker];
}
else
{
[alertController.view addSubview:picker];
}
[picker release];
if (version >= 8)
{
[self presentViewController:alertController animated:NO completion:nil];
}
}
} else if (selectedTableField == bookButton) {
// validate the form
[self validateForm];
}
I am also struggling with the same problem. I need to have a UIPickerView inside a UIAlertController. After some hours of investigations I distinguished that there is no way to customize the view of UIAlertController. In fact it is written explicitly on apple documentations.
The UIAlertController class is intended to be used as-is and does not support subclassing.
The view hierarchy for this class is private and must not be modified.
By the way you can try alternative solutions - uipickerview-programatic-example.
I'm trying to put a UIButton inside UITextfield, to act as a "Done" button to resign the responder. I've tried with the following code, when the field starts editing the done button in the UITextField leftView is visible, however if I select a value from the datePicker or make any text input with the keyboard (in simulator), the done button disappears and the right clearButton is shown.
It seems like it toggles between them both, I changed textFld.LeftViewMode to display always the button but that is not what I'd like...
Thanks in advance!.
- (void)viewDidLoad
{
[super viewDidLoad];
txtFld = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 310, 25)];
txtFld.placeholder = #"__/__/____";
txtFld.font = [UIFont fontWithName:#"HelveticaNeue" size:11];
txtFld.textAlignment = NSTextAlignmentCenter;
txtFld.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
txtFld.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter;
txtFld.clearButtonMode = UITextFieldViewModeWhileEditing;
txtFld.borderStyle = UITextBorderStyleRoundedRect;
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
doneButton.bounds = CGRectMake(0, 0, 40, 20);
doneButton.frame = CGRectMake(0.0, 0.0, 40.0, 20.0);
doneButton.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:10];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
doneButton.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[doneButton addTarget:self action:#selector(resignResponder:) forControlEvents:UIControlEventTouchUpInside];
doneButton.userInteractionEnabled = YES;
// Add button to the UITextField
txtFld.leftView = doneButton;
txtFld.leftViewMode = UITextFieldViewModeWhileEditing;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[txtFld setInputView:datePicker];
[self.view addSubview:txtFld];
}
-(void)resignResponder:(UIButton *)sender{
UITextField *associatedTxtFld = (UITextField *) sender.superview ;
if ([associatedTxtFld isFirstResponder]) {
[associatedTxtFld resignFirstResponder];
}
}
-(void)datePickerValueChanged:(id)sender{
UIDatePicker *picker = (UIDatePicker *)sender;
NSDateFormatter *formater = [[NSDateFormatter alloc]init];
[formater setDateFormat:#"dd-MM-yyyy"];
txtFld.text = [formater stringFromDate:picker.date];
}
The below code works fine for me . This could be a solution to resign your pickerview
UITextField *dispayNameField = [UITextField alloc]init];
UIButton *userStatusButton = [UIButton buttonWithType:UIButtonTypeCustom];
userStatusButton.frame=CGRectMake(0, 0, 20, 20);
[userStatusButton addTarget:self action:#selector(selectUserStatus) forControlEvents:UIControlEventTouchUpInside];
displayNameField.rightViewMode = UITextFieldViewModeAlways;
displayNameField.rightView = _userStatusButton;
-(void)selectUserStatus
{
displayNameField.inputView = _dataPickerView;
self.editUserProfileScrollView.scrollEnabled = YES;
UIActionSheet *actionSheetForDate = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheetForDate showInView:self.parentViewController.tabBarController.view];
[actionSheetForDate setBounds:CGRectMake(0,0,320, 500)];
UIDatePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 50, 320, 216)];
_dataPickerView.tag = 1;
_dataPickerView.showsSelectionIndicator = YES;
_dataPickerView.dataSource = self;
_dataPickerView.delegate = self;
[_actionSheetForDate addSubview:_dataPickerView];
[_actionSheetForDate sendSubviewToBack:_dataPickerView];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheetUserField)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *btnBarCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(dismissActionSheetUserField)];
[pickerToolbar setItems:[NSArray arrayWithObjects:btnBarCancel,flexSpace,doneButton, nil] animated:YES];
[_actionSheetForDate addSubview:pickerToolbar];
displayNameField.inputAccessoryView = pickerToolbar;
}
I have a UITextField that I set the inputView to a UIDatePicker so that when the UITextField is pressed, instead of a keyboard, a UIDatePicker pops up. I would like to also add a toolbar with a 'Next' button on it so that the user could easily jump to the next field, but I am not sure how to do this as I am already altering a view on the UITextField to get the UIDatePicker. Here is the code used in loading the view:
- (void)viewDidLoad
{
self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self.datePicker addTarget:self action:#selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged];
issue.inputView = self.datePicker;
}
Suggestions for getting the 'Next' button added to a toolbar on top of it?
Create a view to use as the text field's inputAccessoryView. This view should contain your buttons (e.g. Next, Cancel, Previous).
Here's the code :
if (keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleDone target:self action:#selector(switchToNextField:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, next, nil]];
}
issue.inputAccessoryView = keyboardToolbar;
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
issue.inputView = datePicker;
And in your switchToNextField method, just make the nextField as the firstResponder using [nextTextField becomeFirstResponder];
You can create a view then put any UI elements inside of it.
#property (nonatomic,strong) UIPopoverController *popoverControllerBirthday;
add above to your .h file and sync it at .m #synthesize popoverControllerBirthday;
also change your #synthesize datePicker; to #synthesize datePicker=_datePicker;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField==self.yourtextfield) {
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
_datePicker=[[UIDatePicker alloc]init];//Date picker
_datePicker.frame=CGRectMake(0,0,320, 216);
_datePicker.datePickerMode = UIDatePickerModeDate;
[_datePicker setMinuteInterval:5];
[_datePicker setTag:10];
[popoverView addSubview:_datePicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(result)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Next" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 220, 320, 45);
UIColor *titleColor = [UIColor whiteColor];
[button setTitleColor:titleColor forState:UIControlStateNormal];
// UIImage *doneButtonImage = [UIImage imageNamed:#"create event button.png"]; choose a button background if you ahve one
[button setBackgroundImage:doneButtonImage forState:UIControlStateNormal];
[popoverView addSubview:button];
popoverContent.view = popoverView;
popoverControllerBirthday = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverControllerBirthday.delegate=self;
CGRect myFrame =textField.frame;
myFrame.origin.x = 200;// your coordinates change it as you wish
myFrame.origin.y = 195;
[popoverControllerBirthday setPopoverContentSize:CGSizeMake(320, 265) animated:NO];
[popoverControllerBirthday presentPopoverFromRect:myFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
return NO; // tells the textfield not to start its own editing process (ie show the keyboard)
}
else{
return YES;
}
}
//handle your result call next uitextfield in your case
-(void) result
{
//in result method you can get date using datepicker.date
if (self.popoverControllerBirthday)
{
[self.popoverControllerBirthday dismissPopoverAnimated:NO];
self.popoverControllerBirthday = nil;
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"M d yyyy"];
NSString *parseDatePickerDAte =[dateFormatter stringFromDate:_datePicker.date]; // your string that is choosen at datepicker
// self.textfield.text = parseDatePickerDAte ; set this to your textfield if you want
//google this dont remember exact code for that, just changing your textfield to desired one
[self.nexttextfield becomesFirstResponder];
}
You can use SMDatePicker instead of UIDatePicker. It will create a custom view with UIToolbar and UIDatePicker. You can easy customise toolbar with necessary buttons. Take a look at example project.
I have a UIbutton on my UIViewController. Clicking that opens up an UIActionSheet with a UIDatePicker on it and a close button. I set the UIDatePicker to date mode .So it shows up with todays date. Suppose i want to select 2015 instead of 2013, i have to scroll down. But it won't allow me to scroll down. I have to scroll up once to 2012 and then scroll down. This seems like a minor issue but the client wants to scroll properly down. I checked on my personal phone to set alarm, i can freely scroll down. I don't have to scroll up and then scroll down. So this is what happens on button click.
- (IBAction)btnShowDateNeededClick:(UIButton *)sender
{
UIDatePicker *datePickerView = [[UIDatePicker alloc]init];
datePickerView.datePickerMode = UIDatePickerModeTime;
self.actSheet = [[ UIActionSheet alloc]init];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blueColor];
closeButton.tag = 101;
[closeButton addTarget:self action:#selector(dismissActionSheetWithTime:) forControlEvents:UIControlEventValueChanged];
[self.actSheet addSubview:closeButton];
[self.actSheet showInView:self.view];
[self.actSheet addSubview:datePickerView];
[self.actSheet sendSubviewToBack:datePickerView];
[self.actSheet setBounds:CGRectMake(0, 0, 320, 500)];
}
As you see, i have datePicker, segment control as a button added to UIActionsheet. Now do i have to change settings or date picker? If you need more information, please ask. Thanks.
Try This. may be this help.
In .h File.
UIDatePicker *datePickerView;
UIActionSheet *Actionsheet1;
In .m File
In viewDidLoad Method
// Picker Initialization
datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
datePickerView.datePickerMode = UIDatePickerModeDateAndTime;
Make a function which will be called on your Button click
-(void) Create_Start_Date_Picker_Function
{
Actionsheet1 = [[UIActionSheet alloc] initWithTitle:nil delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
Actionsheet1.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[Actionsheet1 addSubview: datePickerView];
[Actionsheet1 showInView:self.view];
[Actionsheet1 setBounds:CGRectMake(0, 0, 320, 485)];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blueColor];
[closeButton addTarget:self action:#selector(Dismiss_Actionshit1) forControlEvents:UIControlEventValueChanged];
[Actionsheet1 addSubview:closeButton];
}
And in method for dismiss actionshit.
-(void) Dismiss_Actionshit1
{
// Your action sheet dismiss code
}
I am implementing picker by Add UIPickerView & a Button in Action sheet - How?. I tried to replace the UISegmentedControl with a UIButton but it does not working. Could someone remind me why UIButton cannot be shown here?
Thank you.
- (IBAction) makeButtonPressed: (id) sender;
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
[actionSheet setTitle:#"Select Make"];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.dataSource = self;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[actionSheet addSubview:pickerView];
// UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
// doneButton.momentary = YES;
// doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
// doneButton.segmentedControlStyle = UISegmentedControlStyleBar;
// doneButton.tintColor = [UIColor blackColor];
// [doneButton addTarget:self action:#selector(makeDone:) forControlEvents:UIControlEventValueChanged];
// [actionSheet addSubview:doneButton];
UIButton *doneButton = [[UIButton alloc] init];
doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
[doneButton addTarget:self action:#selector(makeDone:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:doneButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 460)];
}
The designated initializer for UIButton is +buttonWithType:. You should create your button with one of the predefined styles, or use UIButtonTypeCustom.
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundRect];
doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
[doneButton addTarget:self action:#selector(makeDone:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:doneButton];