UIDatePicker will not show after UITableViewCell select
i am using the Code example from Apple, to display a UIDatePicker when a specific UITableViewCell is selected.
how can i change this behavior?
My Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath 开发者_JAVA百科*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.section == 5) {
//Date Picker
UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];
self.pickerView.date = [self.dateFormatter dateFromString:targetCell.textLabel.text];
if (self.pickerView.superview == nil)
{
[self.view.window addSubview: self.pickerView];
// size up the picker view to our screen and compute the start/end frame origin for our slide up animation
//
// compute the start frame
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
CGRect startRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height,
pickerSize.width, pickerSize.height);
self.pickerView.frame = startRect;
// compute the end frame
CGRect pickerRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height - pickerSize.height,
pickerSize.width,
pickerSize.height);
// start the slide up animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
// we need to perform some post operations after the animation is complete
[UIView setAnimationDelegate:self];
self.pickerView.frame = pickerRect;
// shrink the table vertical size to make room for the date picker
CGRect newFrame = self.tableView.frame;
newFrame.size.height -= self.pickerView.frame.size.height;
self.tableView.frame = newFrame;
[UIView commitAnimations];
}
}
}
Assuming the code you've provided is of your detail
ViewController, you don't need to set the frame for pickerRect
. Just need to tell the app that when the orientation changes, the pickerView should rotate with it, as follows
self.picker.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleTopMargin;
You can force the size of picker view by setting its frame, as you've done. Since you are working with a UISplitViewController
, the pickerView needs to be a subView of your detail
ViewController, NOT self.view.window
. Also, go through the sample provided at this link to see what other mistakes you were making.
What I see from the given picture, is that you are trying to slide the UIPickerView from the bottom of the screen upwards.
If so, then I think you are not setting frames as you should be.
As shown in View and Window Architecture, the origin of coordinate system is upper left corner.
I whould do something like this:
CGRect pickerFrame = pickerView.frame;
pickerFrame.origin.x = leftTableViewWidth;
pickerFrame.origin.y = screenFrame.size.height + pickerFrame.size.height;
pickerView.frame = pickerFrame;
// begin animation here
pickerFrame.origin.y += pickerFrame.size.height;
// commit animation
精彩评论