number pad keyboard not show return key in iphone
Asked Answered
S

5

11

I am new to iPhone technology. I've saved a problem in a iPhone application, but when i use textfield keyboard type number pad the keyboard doesn't show a return/done button.

How can I hide the number pad keyboard after entering the number in the textfield? Do you have any solutions for that. Please provide help.

Senility answered 29/4, 2011 at 14:13 Comment(0)
H
11

You can do this in this way:

@property (nonatomic, strong) UITextField *textField;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonDidPressed:)];
    UIBarButtonItem *flexableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[self class] toobarHeight])];
    [toolbar setItems:[NSArray arrayWithObjects:flexableItem,doneItem, nil]];
    self.textField.inputAccessoryView = toolbar;
}

- (void)doneButtonDidPressed:(id)sender {
    [self.textField resignFirstResponder];
}

+ (CGFloat)toolbarHeight {
    // This method will handle the case that the height of toolbar may change in future iOS.
    return 44.f;
}
Hyperthyroidism answered 16/7, 2012 at 11:27 Comment(2)
For iOS 6.1, this did not work unless I initialized the toolbar with a frame like so: UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; Otherwise, perfect answerExcurvate
@Excurvate Confirming that. You need to specify a frame in order for this to work correctly in 6.1.Slide
I
7

you must be using UIKeyboardTypeNumberPad, try this instead UIKeyboardTypeNumbersAndPunctuation, It'll not only show the return key but also provide you with some extra punctuations

Inanna answered 30/11, 2011 at 7:31 Comment(0)
P
1

There is no return or done key in number pad. You can do one thing when user touch outside of the textfield you can hide the keyboard. You should do something like this -

if (there is a touch outside of your textField)
{

   [textField resignFirstResponder];

}
Plumley answered 29/4, 2011 at 14:27 Comment(2)
but i want to create a done button on number pad ,so how to create this done button on number pad . if it is posible then provide help me.Senility
sorry but that's not possible with iPhone sdk with using private APIs' you have to go with some alternative solutions, You can create your own keyboard with done button on it..Plumley
S
0

This question has already been answered, I know because thats how i got the solution. You have 2 options, first is to hide the keyboard by implementing a touch on the mainview that will send the "finished editing" message. that Hides the keyboard [self.view endEditing:YES];

If you do add the touch listener to the mainview you have to implement a condition so that any other buttons keep working.

What you do want to do to simulate a return key is to actually add it like this:

Register for a keyboard did show notification and add this to the code:

if ([self.passCodeLabel isFirstResponder])
        {
            UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
            doneButton.frame = CGRectMake(0, 163, 106, 53);
            //doneButton.frame = CGRectMake(0, 163, 257, 257);
            doneButton.adjustsImageWhenHighlighted = NO;
            [doneButton setImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal];
            [doneButton setImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted];
            [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

            // locate keyboard view
            UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
            NSLog(@"%@",[[UIApplication sharedApplication] windows]);

            UIView* keyboard;
            NSLog(@"Shared applicaiton windows count:%i",tempWindow.subviews.count);
            for(int i=0; i<[tempWindow.subviews count]; i++) {
                keyboard = [tempWindow.subviews objectAtIndex:i];
                NSLog(@"%@",[keyboard description]);
                // keyboard view found; add the custom button to it
                if([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES)
                {
                    NSLog(@"Adding return button");
                    [keyboard addSubview:doneButton];
                }
            }
        } 

This will add your own "done" button image to the keyboard (which you can just copy by taking a screenshot of the screen of the blank slot and adding the done text).

Btw the code i pasted works on my layout. For yours you might have to modify it a bit, but the principle is the same.

  • Create the button

  • Look for the keyboard subview

  • Add the button to that subview

Squire answered 15/4, 2012 at 2:17 Comment(0)
O
0
- (void)viewDidLoad
{

    [super viewDidLoad];
    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                           nil];
    [numberToolbar sizeToFit];

    _txt_mobileno.inputAccessoryView = numberToolbar;

}

-(void)doneWithNumberPad
{
    [_txt_mobileno resignFirstResponder];

}
Omniscient answered 15/12, 2014 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.