首頁>Program>source

因此,小鍵盤預設情况下不帶有"完成"或"下一步"按钮,因此我想添加一个.在iOS 6及以下版本中,有一些技巧可以在鍵盤上添加按钮,但它们似乎在iOS 7中不起作用。

首先,我订阅顯示通知的鍵盤

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

然後我尝試在鍵盤顯示時添加一个按钮:

- (void)keyboardWillShow:(NSNotification *)note 
{
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
    doneButton.frame = CGRectMake(0, 50, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setTitle:@"Done" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(dismissKeyboard) 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 view found; add the custom button to it
        if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)
        [keyboard addSubview:doneButton];
    }
}

但是for迴圈不会執行,因為它找不到任何子视圖.有什麼建議? 我找不到適用於iOS7的任何解決方案,所以應该采用其他方法吗?

編輯:感谢所有對工具欄人員的建議,但我不愿意走那條路,因為我的空間非常有限(而且很丑陋)。

最新回復
  • 5月前
    1 #

    在iOS7 num-keypad中投射完成按钮的簡單方法.在下面的UITextField委託方法中,為鍵盤顯示添加一个通知。

    -(void)textFieldDidBeginEditing:(UITextField *)textField {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    }
    

    現在實現方法 keyboardWillShow 如下.在這裏,我们需要特別註意iOS7。

    - (void)keyboardWillShow:(NSNotification *)note {
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"doneButtonNormal.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"doneButtonPressed.png"] forState:UIControlStateHighlighted];
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIView *keyboardView = [[[[[UIApplication sharedApplication] windows] lastObject] subviews] firstObject];
            [doneButton setFrame:CGRectMake(0, keyboardView.frame.size.height - 53, 106, 53)];
            [keyboardView addSubview:doneButton];
            [keyboardView bringSubviewToFront:doneButton];
            [UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]-.02
                                  delay:.0
                                options:[[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
                             animations:^{
                                 self.view.frame = CGRectOffset(self.view.frame, 0, 0);
                             } completion:nil];
        });
    }else {
        // locate keyboard view
        dispatch_async(dispatch_get_main_queue(), ^{
            UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
            UIView* keyboard;
            for(int i=0; i<[tempWindow.subviews count]; i++) {
                keyboard = [tempWindow.subviews objectAtIndex:i];
                // keyboard view found; add the custom button to it
                if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)
                    [keyboard addSubview:doneButton];
            }
        });
      }
    }
    

    現在將此巨集添加到適当的標頭中以檢測SySTEM_VERSION

    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

  • 5月前
    2 #

    更安全的方法是使用 UIToolBarDone Button as inputAccessoryView


    Sample Code :

    UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init];
    [keyboardDoneButtonView sizeToFit];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                   style:UIBarButtonItemStyleBordered target:self
                                                                  action:@selector(doneClicked:)];
    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
    txtField.inputAccessoryView = keyboardDoneButtonView;
    

    您的 -doneClicked 方法應如下所示:

    - (IBAction)doneClicked:(id)sender
    {
        NSLog(@"Done Clicked.");
        [self.view endEditing:YES];
    }
    

    Sample Code Swift:

    let keyboardDoneButtonView = UIToolbar.init()
    keyboardDoneButtonView.sizeToFit()
    let doneButton = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Done, 
                                                       target: self, 
                                                       action: Selector("doneClicked:")))    
    keyboardDoneButtonView.items = [doneButton]
    textFieldInput.inputAccessoryView = keyboardDoneButtonView
    

    您的 -doneClicked 方法應如下所示:

    func doneClicked(sender: AnyObject) {
      self.view.endEditing(true)
    }
    

  • 5月前
    3 #

    更簡單的方法:

    Swift 3.0 and above

    func addDoneButton() {
        let keyboardToolbar = UIToolbar()
        keyboardToolbar.sizeToFit()
        let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace,
            target: nil, action: nil)
        let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done,
            target: view, action: #selector(UIView.endEditing(_:)))
        keyboardToolbar.items = [flexBarButton, doneBarButton]
        textField.inputAccessoryView = keyboardToolbar
    }
    

    Swift 2.3 and below

    func addDoneButton() {
        let keyboardToolbar = UIToolbar()
        keyboardToolbar.sizeToFit()
        let flexBarButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace,
            target: nil, action: nil)
        let doneBarButton = UIBarButtonItem(barButtonSystemItem: .Done,
            target: view, action: #selector(UIView.endEditing(_:)))
        keyboardToolbar.items = [flexBarButton, doneBarButton]
        textField.inputAccessoryView = keyboardToolbar
    }
    

    Objective C

    - (void)addDoneButton {
        UIToolbar* keyboardToolbar = [[UIToolbar alloc] init];
        [keyboardToolbar sizeToFit];
        UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc]
        initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
        target:nil action:nil];
        UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc]
        initWithBarButtonSystemItem:UIBarButtonSystemItemDone
        target:self.view action:@selector(endEditing:)];
        keyboardToolbar.items = @[flexBarButton, doneBarButton];
        self.textField.inputAccessoryView = keyboardToolbar;
    }
    

    編輯:

    我已经建立了一个名為DCKit的有用的庫,该庫已经具有現成的工具欄:

    它還具有许多其他很酷的功能。

  • 5月前
    4 #

    只是在上述Swift版本的答案基础上进行,因為我不得不翻译它:

      @IBOutlet weak var numberTextField: UITextField!
        override func viewDidLoad() {
            addDoneButtonTo(numberTextField)
        }
        // MARK: Done for numberTextField
        private func addDoneButtonTo(textField: UITextField) {
            let flexBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
            let doneBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "didTapDone:")
            let keyboardToolbar = UIToolbar()
            keyboardToolbar.sizeToFit()
            keyboardToolbar.items = [flexBarButton, doneBarButton]
            textField.inputAccessoryView = keyboardToolbar
        }
        func didTapDone(sender: AnyObject?) {
            numberTextField.endEditing(true)
        }
    

  • 5月前
    5 #

    您可以使用

    myTextField.inputAccessoryView =_inputView;

    input附件视圖是始终位於鍵盤上方並隨 [textfield resignFirstResponder]一起關闭的视圖

    在輸入视圖上放入完成並執行文字欄位的signsignfirst响應器。

  • spring:將applicationContext拆分為多个檔案
  • html5:跨域postMessage在IE10中是否损壞?