iOS8 drawing rectangle not showing - ios

I have the below code to draw a datepicker (showing around the middle of the screen). It works in iOS 7 but in iOS 8 it is not showing. Only the "Select Expiry Date" is shown in a small box in the bottom of the screen.
menu = [[UIActionSheet alloc] initWithTitle:#"Select Expiry Date"
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
pickerView = [[CDatePickerViewEx alloc] init];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 50;
pickerRect.size.height = 100;
pickerView.frame = pickerRect;
UIButton *startbtn = [UIButton buttonWithType:UIButtonTypeCustom];
startbtn.frame = CGRectMake(240,12, 60, 25);
[startbtn setTitle:#"OK" forState:UIControlStateNormal];
[startbtn setBackgroundImage:[UIImage imageNamed:#"btn"] forState:UIControlStateNormal];
[startbtn addTarget:self action:#selector(pressedbuttonCancel:) forControlEvents:UIControlEventTouchUpInside];
[menu addSubview:pickerView];
[menu addSubview:startbtn];
[menu showInView:self];
[menu setFrame:CGRectMake(0,200,320, 260)];

Check if AutoLayout is enabled for your view, as well as Size classes. I ran into a similar issue before and remember of fixing it with programmatic constraints.

Related

Set value to UIPickerView row error iOS

When i click the button, pickerview coming up and i am choosing a city from there. And if i click again i want to get what i selected before.
Here is my code,
-(void)cityButtonPressed{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 30, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
NSLog(#"selectedPickerView = %i",selectedPickerView);
[pickerView selectRow:selectedPickerView inComponent:0 animated:NO];// this line is not working
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
NSString *done = #"Done";
closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:done]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 10, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor greenColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 500)];
[pickerView reloadAllComponents];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
[cityButton setTitle:[cities objectAtIndex:row] forState:UIControlStateNormal];
if (row == 0) {
[cityButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
}else{
[cityButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
selectedPickerView = row;
}
What am i doing wrong here.
And one more question. When i click the button,this error is coming.
<Error>: CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
I am wondering, is it not working because of this error?
You're creating an action sheet with no title or buttons? Ugh. Then you're adding a segmented control as a tappable button? Double ugh Apple will likely reject an app that uses a segmented control as a button.
As to your specific problem, I'm not sure if you can change the selected component in a picker view before it's on-screen. What happens if you move your selectRow:inComponent:animated: call down to after displaying the action sheet? You should probably also get rid of the reload call on your picker view, since it will load it's data when it's displayed.

iPad Actionsheet not showing

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

Showing a selection list on a popup using UIActionSheet

I'm developing an iOS 5.0+ app with latest SDK.
I want to show a popup with some options to choose. But I don't know how to do it.
I have this code to show an UIActionSheet with a seven buttons:
- (void)setUserActivity
{
activityActionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:#""
destructiveButtonTitle:nil
otherButtonTitles:nil];
[activityActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
for (int index = 0; index < 7; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(activitySelected:)
forControlEvents:UIControlEventTouchDown];
CGRect frame = CGRectMake(0.0, index*40 + 10, 160.0, 40.0);
button.frame = frame;
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %d", index] forState:UIControlStateNormal];
[activityActionSheet addSubview:button];
}
CGRect toolbarFrame = CGRectMake(0, 0, activityActionSheet.bounds.size.width, 44);
UIToolbar* controlToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
[controlToolbar setBarStyle:UIBarStyleBlack];
[controlToolbar sizeToFit];
UIBarButtonItem* spacer =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem* setButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Set", nil)
style:UIBarButtonItemStyleDone
target:self
action:#selector(dismissActivityActionSheet)];
UIBarButtonItem* cancelButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Cancel", nil)
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cancelActivityActionSheet)];
[controlToolbar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil]
animated:NO];
[activityActionSheet addSubview:controlToolbar];
[activityActionSheet showInView:self.view];
[activityActionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
And this is what I get:
How can I add a scroll to allow user click on button 6? Or, is there a better approach to do it?
Is there any reason why you're adding buttons as a subview of the UIActionSheet?
The preferred method is to provide additional button titles in via the otherButtonTitle method parameter:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Title"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Okay"
otherButtonTitles:#"One", #"Two", #"Three", #"Four", #"Five", #"Six", nil];
Then if you conform to the delegate, you can use the method actionSheet:didDismissWithButtonIndex: and determine which of the buttons was selected.
You should add an UIScrollView between your buttons and the ActivityActionSheet...
...
UIScrollView *myButtonScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,160,441)];
for (int index = 0; index < 7; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(activitySelected:)
forControlEvents:UIControlEventTouchDown];
CGRect frame = CGRectMake(0.0, index*40 + 10, 160.0, 40.0);
button.frame = frame;
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %d", index] forState:UIControlStateNormal];
//[activityActionSheet addSubview:button];
[myButtonScroll addSubView:button];
}
[myButtonScroll setContentSize:CGSizeMake(160, 7*40 + 20)];
[activityActionSheet addSubView:myButtonScroll];
...
Assuming there are only 7 buttons on your view...
I would use a UIActionSheet only if you plan to insert the buttons through this method:
activityActionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:#""
destructiveButtonTitle:nil
otherButtonTitles:nil];
You can easily insert the buttons as a list of strings i.e. #"Button1",#"Button2",nil (with a nil delimiter at the end) and pass it in the otherButtonTitles parameter.
If you want to use customised views (adding buttons to the view), I would rather use another control (create my own UIView subclass that acts similarly to UIActionSheet).

how to manage uipickerview in uiactionsheet

i am trying to show the UIPickerView in UIActionSheet . from the below mentioned code it works great in portrait mode but looks wired in Landscape Mode. (my app support iPhone 4/5 n landscape orientation also).
what i am doing wrong ?
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame=CGRectMake(0, 40, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"OK"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
Finally i got the solution
First of all i have to set the width of the picker view as i have mentioned
CGRect pickerFrame=CGRectMake(0, 40, 0, 0);
so it can be like
CGRect pickerFrame=CGRectMake(0, 40, 320, 0);
I found one more good solution :
Just call the picker view in action sheet by clicking a text field:
self.textField.inputView = yourCustomView;

UIdate picker does not move freely

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
}

Resources