views:

264

answers:

3

Hi.

So I have a UITableView where one of its rows is a UITextView.

When the user is writing something to UITextView (using the keyboard) the user can scroll the tableview and select another cell. Everything works.

When the user selects another cell, a datePicker appears, and the user can select a given date. I want the user to be able to scroll the tableView the same way like when the keyboard is on the screen.

The problem here is that when I scroll with the picker, the table bounces back to the previous position (with some cells hidden by the picker).

I assume that this happens because I add the picker to the main window, as a subview... but I'm honestly not sure...

Where (and how) should I add my picker so that it "appears" where the keyboard appears?

Not sure I was clear...

Thanks a lot.

A: 

I actually did this with the UIDatePicker, created my own SlideUpDatePicker that animated up from the bottom just like the keyboard. It took me a week or two to get it all working perfectly, it was definitely not easy to get it to work in portrait and landscape and with a nav controller and its animations.

The keyboard creates a new top level window to hold its view and it posts notifications to the notification center before and after it appears and disappears so that the view controller can adjust the size of it's view to account for the keyboard. The view controller needs to handle those notifications and resize the tableview by the size of the keyboard.

My SlideUpDatePicker posted the same notifications as the keyboard so the view controller wouldn't have to be modified and could respond the same for both the keyboard and the slide up date picker.

If you use the dumpWindows() function you can see the UIWindow and UIView hierarchy with the keyboard and a table view in a navigation based app:

dumpView: <UIWindow: 0x411fd50; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x4120af0>>                                                                                                                                                                                                                                                                                                                                                                                 
dumpView: : <UILayoutContainerView: 0x4123310; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x411f800>>                                                                                                                                                                                                                                                                                                                                                                                 
dumpView: : | <UINavigationTransitionView: 0x4123500; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x41232e0>>                                                                                                                                                                                                                                                                                                                                                     
dumpView: : | : <UIViewControllerWrapperView: 0x4519d30; frame = (0 64; 320 416); autoresize = W+H; layer = <CALayer: 0x4519a40>>                                                                                                                                                                                                                                                                                                                                                                      
dumpView: : | : | <UITableView: 0x7808000; frame = (0 0; 320 416); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x45182a0>>                                                                                                                                                                                                                                                                                                                                                                
dumpView: : | : | : <DeleteCell: 0x45196e0; baseClass = UITableViewCell; frame = (0 507; 320 45); opaque = NO; autoresize = W; layer = <CALayer: 0x4519a80>>                                                                                                                                                                                                                                                                                                                                           
dumpView: : | : | : <TextCell: 0x4523280; baseClass = UITableViewCell; frame = (0 442; 320 45); autoresize = W; layer = <CALayer: 0x4521ed0>>                                                                                                                                                                                                                                                                                                                                                          
dumpView: : | : | : <DatePickerCell: 0x45224b0; baseClass = UITableViewCell; frame = (0 398; 320 44); autoresize = W; layer = <CALayer: 0x451e170>>                                                                                                                                                                                                                                                                                                                                                    
dumpView: : | : | : <ButtonCell: 0x4521680; baseClass = UITableViewCell; frame = (0 354; 320 44); autoresize = W; layer = <CALayer: 0x4521750>>                                                                                                                                                                                                                                                                                                                                                        
dumpView: : | : | : <ButtonCell: 0x45209f0; baseClass = UITableViewCell; frame = (0 310; 320 44); autoresize = W; layer = <CALayer: 0x4520ac0>>                                                                                                                                                                                                                                                                                                                                                        
dumpView: : | : | : <ButtonCell: 0x4520100; baseClass = UITableViewCell; frame = (0 266; 320 44); autoresize = W; layer = <CALayer: 0x45201d0>>                                                                                                                                                                                                                                                                                                                                                        
dumpView: : | <UINavigationBar: 0x45018b0; frame = (0 20; 320 44); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x4500fe0>>                                                                                                                                                                                                                                                                                                                                                                  
dumpView: : | : <UINavigationItemView: 0x4522a20; frame = (100 8; 160 27); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4526310>>                                                                                                                                                                                                                                                                                                                                                     
dumpView: : | : <UINavigationItemButtonView: 0x45230a0; frame = (5 7; 87 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4520260>>                                                                                                                                                                                                                                                                                                                                                  
dumpView: <UITextEffectsWindow: 0x4537080; frame = (0 0; 320 480); opaque = NO; layer = <CALayer: 0x45371c0>>                                                                                                                                                                                                                                                                                                                                                                                          
dumpView: : <UIKeyboard: 0x4536ce0; frame = (0 264; 320 216); opaque = NO; layer = <CALayer: 0x4536f60>>                                                                                                                                                                                                                                                                                                                                                                                               
dumpView: : | <UIKeyboardImpl: 0x4175b10; frame = (0 0; 320 216); opaque = NO; layer = <CALayer: 0x4173f90>>                                                                                                                                                                                                                                                                                                                                                                                           
dumpView: : | : <UIKeyboardLayoutStar: 0x4177d90; frame = (0 0; 320 216); layer = <CALayer: 0x4177610>>                                                                                                                                                                                                                                                                                                                                                                                                
dumpView: : | : | <UIKBKeyplaneView: 0x418a6a0; frame = (0 0; 320 216); userInteractionEnabled = NO; layer = <CALayer: 0x418a750>>                                                                                                                                                                                                                                                                                                                                                                     
dumpView: : | : | : <UIKBKeyView: 0x41748e0; frame = (1 119; 40 42); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x41545c0>>                                                                                                                                                                                                                                                                                                                                                           
dumpView: : | : | : <UIKBKeyView: 0x417e2d0; frame = (279 119; 40 42); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x417df10>>                                                                                                                                                                                                                                                                                                                                                         
dumpView: : | : | : <UIKBKeyView: 0x418a780; frame = (1 173; 78 42); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x417f990>>                                                                                                                                                                                                                                                                                                                                                           
dumpView: : | : | : <UIKBKeyView: 0x418adf0; frame = (81 173; 158 42); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4180120>>                                                                                                                                                                                                                                                                                                                                                         
dumpView: : | : | : <UIKBKeyView: 0x418cdb0; frame = (241 173; 78 42); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x418cf20>>                    
progrmr
By the logic of it, it works. Now I'll give it a fast try using the notifications. The main problem is that I don't have two weeks, so... let's see what happens.Anyway, thanks a lot.
camilo
A: 

Not so elegant...

Not so pro...

i did this...

CGRect frame3 = self.tableView.frame;
frame3.size.height = frame3.size.height-216;
self.tableView.frame = frame3;

and then i made my picker appear. Et voilá! ;)

camilo
+1  A: 

You should use inputView property of UIResponder class (also of UITextField class). Just do:

UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
//picker.delegate = self;
//picker.dataSource = self;
//[picker setShowsSelectionIndicator:YES];
textField.inputView = picker;
[picker release];

and UIPickerView will behave like the keyboard view.

For more details and a sample code see my article about working with pickers.

lechec