iOS how to implement a drop down list and how to take care of closing it?

后端 未结 5 360
臣服心动
臣服心动 2020-12-24 04:05

I need some inputs on how to implement dropdown list kind of functionality in iOS.

I have some solutions in mind like using UITableView for displaying t

相关标签:
5条回答
  • 2020-12-24 04:19

    Drop down lists are usually implemented in iOS using a UIPickerView. The picker view can be set as the input view of the text field which would hold the drop down, it is then animated on and off screen in the same manner as the keyboard.

    You usually also need a UIToolbar holding a "Done" button as the input accessory view, this appears above the picker and allows you to dismiss once a choice is made if you're not doing that automatically.

    You remove the picker by sending resignFirstResponder to the text field, either from a picker view delegate method or the action method of your done button.

    You create the toolbar as an accessory view as follows:

    accessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    accessoryView.barStyle = UIBarStyleBlackTranslucent;
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTapped:)];
    
    accessoryView.items = [NSArray arrayWithObjects:space,done, nil];
    
    textField.inputAccessoryView = accessoryView;
    

    This will give you a single "Done" button on the right which is connected to an action method called doneTapped:

    0 讨论(0)
  • 2020-12-24 04:21

    I found this project available at github useful. https://github.com/kmdarshan/dropdown

    0 讨论(0)
  • 2020-12-24 04:23

    Try this out. It may help.

    1) Add the UITableView on a transparent UIView. 
    2) The UIView should have the same size as the display screen.
    3) The UITableView shall take the same small size you have.
    4) Implement the touches method as you mentioned for the holding UIView.
    
    0 讨论(0)
  • 2020-12-24 04:28

    I have created a dropdown control for iOS. You can check out it from below URL

    https://github.com/iVishal/VSDropdown

    0 讨论(0)
  • 2020-12-24 04:38

    First of all, if you are on iPad, a UIPopoverViewController is designed exactly for this. If you are needing something more custom I always have an invisible fullscreen sized button unhide just under the dropdown. It covers the entire screen and when touched or when the dropdown dismisses it hides its self. Super simple.

    0 讨论(0)
提交回复
热议问题