Imitate UIPickerView behavior on safari

When the user encounter a drop-down box while browsing the web the safari loads a UIPicker view in order to populate the selections and present it to the user.
On one of my recent projects I needed this same functionality but implemented as a native solution and not as web-view/html solution.
The solution I found was using the UIActionSheet control that contains a UIPickerView as a subview.

In the .h file include:

UIActionSheet * pickerContainer;
UIPickerView * pickerItems;

In the .m file include this code on the viewDidLoad method:

pickerContainer = [[UIActionSheet alloc] init];
pickerItems = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0,0,0)];
pickerItems.delegate = self;
[pickerContainer addSubview:pickerItems];

And when you want the picker to appear on screen execute this code:

[pickerContainer showInView:self.view];
[pickerContainer setBounds:CGRectMake(0,0,320,400)];

This will make the ActionSheet appear on screen in the same way the picker appears when you select a drop-down element on an HTML page.
You can also add buttons above the UIPickerView in order to mimic the same functionality that happens on web views.

Happy coding!

Netanel Lev

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s