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

后端 未结 5 394
臣服心动
臣服心动 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:

提交回复
热议问题