Pregunta

I am implemented a UITextField which is supposed to only let user include decimal numbers. I have changed the keyboard type to UIKeyboardTypeDecimalPad but there is no return button on this keyboard layout to get out of the text field. I don't want to add extra controls on the keyboard as it looks messy. How can I change the dot button to return and change its functionality or is there another alternative?

¿Fue útil?

Solución

By default return key doesn't comes with Decimal keyboard type. Use the following code snippet to acheive that:

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 35.0f)];
    toolbar.barStyle=UIBarStyleBlackOpaque;

    // Create a flexible space to align buttons to the right
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    // Create a cancel button to dismiss the keyboard
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resetView)];

    // Add buttons to the toolbar
    [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, barButtonItem, nil]];

    // Set the toolbar as accessory view of an UITextField object
    _yourTextField.inputAccessoryView = toolbar;

Hope this helps. Happy coding !!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top