【问题标题】:Add Custom image button in iphone keyboard when select UITextField选择 UITextField 时在 iphone 键盘中添加自定义图像按钮
【发布时间】:2010-10-30 06:41:02
【问题描述】:

当有人选择我的 UITextField 时,我想在 iPhone 键盘中添加一些自定义图像按钮。在 iPhone 键盘中有默认按钮返回(或关闭键盘)?我想将返回按钮更改为我自己的按钮并执行相同的功能,以关闭键盘。

有可能吗?谁能帮我提供一些源代码?

【问题讨论】:

  • 请参阅下面的数字键盘答案...

标签: iphone uitextfield


【解决方案1】:

是的,你可以这样做。这是源代码

- (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]; }

希望这会对你有所帮助。

更多信息:http://www.appsfor2012.com/2012/09/keyboard-with-custom-button.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多