prostock
prostock

Reputation: 9545

animate picker view over tab bar like keyboard

I animate a custom view with a picker subview like how a keyboard appears when activating a UITextField.

Currently I do it by adding my custom view as a subview to the app's UIWindow. If I add the the custom view as a subview to the current tab bar view controller's view, the tab bar will cover the picker. Is there a way to animate the custom view as a subview of the current view controller without having the tab bar covering the custom view? What view does the keyboard come up on?

Upvotes: 2

Views: 2276

Answers (2)

Tom
Tom

Reputation: 2714

Just a little disclaimer: This is my first post on stackoverflow so if formatting isn't all fine I'm sorry for that. Also, I'm fairly new to iOS programming as well as to the Objective C language, so I'm definitely not saying this is the way things 'should' be done. However, recently I ran into a comparable situation which I solved in a way I think is quite elegant:

To me, it seems like using a UIPickerView could just be the preferred way to enter text into a textfield as opposed to using the regular keyboard. From a user experience point of view, to me that means the UIPickerView should (dis)appear in a similar way as the keyboard as well.

Since iOS 3.2, UITextField supports the inputView property to assign a custom view to be used as a keyboard, which provides a way to display a UIPickerView:

UIPickerView *myPickerView = [[UIPickerView alloc] init];
//myPickerView configuration here...
myTextField.inputView = myPickerView;

Like that. This will not give you a direct way to dismiss the view since your UIPickerView has no return button, which is why I recommend to use the inputAccessoryView property to display a toolbar with a done button (the bar is just for aesthetics, you might as well just use a UIButton object):

UIToolbar *myToolbar = [[UIToolBar alloc] initWithFrame:
 CGRectMake(0,0, 320, 44); //better code with variables to support view rotation
UIBarButtonItem *doneButton =
 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
 target:self action:@selector(inputAccessoryViewDidFinish)];
 //using default text field delegate method here, here you could call
 //myTextField.resignFirstResponder to dismiss the views
[toolbar setItems:[NSArray arrayWithObject: doneButton] animated:NO];
myTextField.inputAccessoryView = myToolbar;
//you can -release your doneButton and myToolbar if you like

When I reread your question, I noticed that the UIPickerView is just part of the subview. Anyway, this method works for any view so in your case you would set your subview as the UITextField's inputView.

Upvotes: 8

alex_c
alex_c

Reputation: 2046

You can try adding your subview to the UITabBarController's view itself, rather than the view of the active controller (tabBarController.view rather than tabBarController.selectedViewController.view), and doing a bringSubviewToFront on the subview you added. I haven't tried this, so I'm not sure if it works.

It does feel a bit hack-ish, since it depends on the UITabBarController cooperating - that makes me uncomfortable because this behavior isn't well defined. I don't see anything inherently wrong with using the UIWindow instead.

Upvotes: 0

Related Questions