TextInput Field with Pickerview instead of keyboard

ε祈祈猫儿з 提交于 2021-02-07 20:20:34

问题


I want to add a pickerview instead of a keyboard for a specific textfield and fill the picker view with content directly in the .m file

What is the easiest way to accomplish that?

Can I do this directly in the interface builder or do i have to add much code on my own?


回答1:


For any Xamarin.iOS developers, here's how to do it in C#

public partial class MyViewController : UIViewController
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        ConfigurePicker();
    }

    void ConfigurePicker()
    {
        var pickerTextField = new UITextField();

        var picker = new UIPickerView
        {
            Model = new PickerViewModel(),
            ShowSelectionIndicator = true
        };

        var screenWidth = UIScreen.MainScreen.Bounds.Width;
        var pickerToolBar = new UIToolbar(new RectangleF(0, 0, (float)screenWidth, 44)) { BarStyle = UIBarStyle.Default, Translucent = true };
        var flexibleSpaceButton = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
        var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, (sender, e) => pickerTextField.ResignFirstResponder());
        pickerToolBar.SetItems(new[] { flexibleSpaceButton, doneButton }, false);

        pickerTextField.InputView = picker;
        pickerTextField.InputAccessoryView = pickerToolBar;
    }

    class PickerViewModel : UIPickerViewModel
    {
        int[] _pickerSource = new []{ 1, 2, 3, 4, 5 };

        public override nint GetRowsInComponent(UIPickerView pickerView, nint component) => _pickerSource.Count;

        public override string GetTitle(UIPickerView pickerView, nint row, nint component) => _pickerSource[(int)row];

        public override nint GetComponentCount(UIPickerView pickerView) => 1;
    }
}



回答2:


You can use custom button on uitextfield. and can fire action for that button is pressed. when pickerview dismiss then just add the selected text in textbox.




回答3:


Set tag to that textField and check condition in textFieldDidBeginEditing method write picker code.




回答4:


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self createPickerView]; 
}

-(void)createPickerView
{
    //create Picker Here
}

or Put Button over in place textfeild give inaction according to it.




回答5:


UIPickerView *picker = [[UIPickerView alloc] init];
picker.delegate = self;
picker.dataSource = self;
textField.inputView = picker;



回答6:


here is the solution for your question

you an find it in one of the stack overflow question,click on link shown below.

Elegantly replace iPhone keyboard with UIPickerView

or

First, here is a screencapture showing how this looks.

Implement UITextFieldDelegate and display a "popup" containing a UIPickerView.

- (void)textFieldDidEndEditing:(UITextField *)textField {

    UIPickerView *picker = [[UIPickerView alloc] 
                                 initWithFrame:CGRectMake(0, 244, 320, 270)];
    picker.delegate = self;
    picker.dataSource = self;
    [self.view addSubview:picker];
    [picker release];
}

When the keyboard disappears, a picker view is then visible.

If you want to take this a bit further, you can animate the UIPickerView "slide in" like the keyboard.

- (void)viewDidLoad {

    //picker exists in the view, but is outside visible range
    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 480, 320, 270)];
    picker.delegate = self;
    picker.dataSource = self;
    [self.view addSubview:picker];
    [picker release];
}

//animate the picker into view
- (void)textFieldDidEndEditing:(UITextField *)textField {

    [UIView beginAnimations:@"picker" context:nil];
    [UIView setAnimationDuration:0.5];

    picker.transform = CGAffineTransformMakeTranslation(0,-236);
    [UIView commitAnimations];

}

//animate the picker out of view
- (void)textFieldDidBeginEditing:(UITextField *)textField {

    [UIView beginAnimations:@"picker" context:nil];
    [UIView setAnimationDuration:0.5];

    picker.transform = CGAffineTransformMakeTranslation(0,236);
    [UIView commitAnimations];
}

//just hide the keyboard in this example
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}


来源:https://stackoverflow.com/questions/7306442/textinput-field-with-pickerview-instead-of-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!