开发者

Add Custom image button in iphone keyboard when select UITextField

开发者 https://www.devze.com 2023-01-22 11:31 出处:网络
I want to add some custom image button in iPhone keyboard, when anyone selects my UITextField. In iPhone keyboard there is default button to return (or dismiss the keyboar开发者_StackOverflow中文版d)?

I want to add some custom image button in iPhone keyboard, when anyone selects my UITextField. In iPhone keyboard there is default button to return (or dismiss the keyboar开发者_StackOverflow中文版d)? I want to change the return button into my own button and do the same function, to dismiss the keyboard.

Is it possible? Can anyone help me with some source code?


Yes you can do this.Here is the source code

- (void)loadView {
     self.view = [[UIView alloc] initWithFrame:[UIScreen
 mainScreen].applicationFrame];
     self.view.backgroundColor = [UIColor
 groupTableViewBackgroundColor];

     textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300,
 26)];
     textField.borderStyle = UITextBorderStyleRoundedRect;
     textField.keyboardType = UIKeyboardTypeNumberPad;
     textField.returnKeyType = UIReturnKeyDone;
     textField.textAlignment = UITextAlignmentLeft;
     textField.text = @"12345";
     [self.view addSubview:textField];
        // add observer for the respective notifications (depending on
 the os version)    if ([[[UIDevice
 currentDevice] systemVersion]
 floatValue] >= 3.2) {
        [[NSNotificationCenter
 defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardDidShow:) 
                                                     name:UIKeyboardDidShowNotification 
                                                   object:nil];         } else {        [[NSNotificationCenter
 defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardWillShow:) 
                                                     name:UIKeyboardWillShowNotification 
                                                   object:nil];     }    }

 - (void)addButtonToKeyboard {  // create custom button     UIButton
 *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163,
 106, 53);
    doneButton.adjustsImageWhenHighlighted
 = NO;  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
        [doneButton setImage:[UIImage
 imageNamed:@"DoneUp3.png"]
 forState:UIControlStateNormal];
        [doneButton setImage:[UIImage
 imageNamed:@"DoneDown3.png"]
 forState:UIControlStateHighlighted];
    } 
else {
    [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];  UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews
 count]; i++) {         keyboard =
 [tempWindow.subviews objectAtIndex:i];
        // keyboard found, add the button
        if ([[[UIDevice currentDevice]
 systemVersion] floatValue] >= 3.2) {
            if([[keyboard description]
 hasPrefix:@"<UIPeripheralHost"] ==
 YES)
                [keyboard addSubview:doneButton];       } else {            if([[keyboard
 description] hasPrefix:@"<UIKeyboard"]
 == YES)
                [keyboard addSubview:doneButton];       }   } }

 - (void)keyboardWillShow:(NSNotification
 *)note {   // if clause is just an additional precaution, you could also
 dismiss it     if ([[[UIDevice
 currentDevice] systemVersion]
 floatValue] < 3.2) {       [self
 addButtonToKeyboard];  } }

 - (void)keyboardDidShow:(NSNotification
 *)note {   // if clause is just an additional precaution, you could also
 dismiss it     if ([[[UIDevice
 currentDevice] systemVersion]
 floatValue] >= 3.2) {      [self
 addButtonToKeyboard];
     } }


 - (void)doneButton:(id)sender {    NSLog(@"doneButton");
     NSLog(@"Input: %@", textField.text);
     [textField resignFirstResponder]; }

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
     return (interfaceOrientation == UIInterfaceOrientationPortrait); }

 - (void)didReceiveMemoryWarning {
     [super didReceiveMemoryWarning]; }

 - (void)dealloc {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
     [textField release];
     [super dealloc]; }

Hope this will help you.

Further information: http://www.appsfor2012.com/2012/09/keyboard-with-custom-button.html

0

精彩评论

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