The Kraken
The Kraken

Reputation: 3158

Keep UIDatePicker from Scrolling with TableView

Seems like a really simple issue, but I haven't found a solution yet:

I have an UIDatePicker that shows when a certain table view cell is selected. I display it with the following code:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0: {
                _alarmPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 315, 320, 0)];
                [_alarmPicker setDatePickerMode:UIDatePickerModeTime];
                [_alarmPicker addTarget:self action:@selector(alarmPickerChanged) forControlEvents:UIControlEventValueChanged];
                [self.view addSubview:_alarmPicker];
            }
                break;

            default:
                break;
        }
    }
}

The problem is, whenever the picker is displayed, it scrolls upward and downward with the rest of the table. Beyond simply disabling all scrolling on the table view when the picker is selected, how can I keep the picker fixed but the table scrollable?

Upvotes: 0

Views: 549

Answers (3)

Lex L.
Lex L.

Reputation: 613

After some trials I found this worked very easily for me: [self.window addSubview:picker];

For removing the picker I then used: [picker removeFromSuperview];

Upvotes: 1

matt
matt

Reputation: 535511

If you're on iOS 6, you can set up constraints between the UIDatePicker and self.view.superview. This will prevent the date picker from moving: it will be "pinned" to the superview.

Upvotes: 0

Gobot
Gobot

Reputation: 2480

You could add it to the key window:

[[[UIApplication sharedApplication] keyWindow] addSubview:_alarmPicker];

Therefore it will be on top. You were adding it to your table view (assuming you are using UITableViewController).

Upvotes: 1

Related Questions