开发者

Custom button on UIKeyboard

开发者 https://www.devze.com 2023-04-03 19:50 出处:网络
I am trying to add a custom done button on the UIKeyboard (the number pad specifically). I have followed all the examples to do this online but I can\'t get the button to appear above the keyboard. Be

I am trying to add a custom done button on the UIKeyboard (the number pad specifically). I have followed all the examples to do this online but I can't get the button to appear above the keyboard. Below is my code (mainly patched together from online examples):

    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(214, 427, 106, 53);
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];

UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView *keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) 
{
    keyboard = [tempWindow.subviews objectAtIndex:i];
    if([[keyboard description] hasPrefix:@"<UIKeyboardTypeNumberPad"] == YES)
        [keyboard addSubview:doneButton];
}

Am I doing something wrong?

(This code is called after the keyboard is displayed).

EDIT:

I am showing the keypad in a modal view controller which is the child of a UINavigationController. In all the sample code I've tried the problem seems to be with this line:

        if([[keyboard description] hasPrefix:@"<UIKeyboardTypeNumberPad"] == YES)

which is returning NO. I have tested this using the UIKeyboardDidShow notification so the keyboard is definitely visible 开发者_开发百科when the code is called.


The way you are trying is hard-code hacking and violating HIG I think.

If you want to customize keyboard, you should create your own keyboard and set it to your UIResponder's inputView.


I figured out the solution. In iOS4.0 Apple changed the description of the keyboard so you now have to use UIPeripheralHostView (this is clearly not the best way to implement this solution if Apple can change that value at any time). My positioning of the button was also off as I was positioning the y coordinate against the whole view instead of just the keyboard. Here is the final code:

- (void)keyboardDidShow:(NSNotification *)note {


// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(215, 163, 106, 53);
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];

UIWindow* tempWindow;

UIView* keyboard;

for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
    tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];

    for(int i = 0; i < [tempWindow.subviews count]; i++)
    {
        keyboard = [tempWindow.subviews objectAtIndex:i];

        if([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES)
        {
            [keyboard addSubview:doneButton];
        }
    }
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消