Adding UIToolbar to input accessory view on some text fields

前端 未结 5 1347
囚心锁ツ
囚心锁ツ 2020-12-31 16:05

In my quest for my first iPhone app I have posted about the correct way to handle the return key on the iOS keyboard. Now I need to figure out the toolbar above the keyboar

相关标签:
5条回答
  • 2020-12-31 16:40

    Not sure if it's too late. I put my solution about adding a UIToolbar into UIKeyboard, and The keyboard types changes with the UITextField content, and handling the UIBarButtonItem with "Previous", "Next" and "Done" at Github. And if the TextFields at different sections of the tableview, it turns to be more tricky.

    0 讨论(0)
  • I think your buttons are just not visible because of the following code:

    self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent;
    self.keyboardToolbar.tintColor = [UIColor darkGrayColor];
    

    Try using

    self.keyboardToolbar.barStyle = UIBarStyleBlack;
    self.keyboardToolbar.tintColor = [UIColor whiteColor];
    

    instead

    0 讨论(0)
  • 2020-12-31 16:43

    UIBarButtonItem isn't a UIView subclass, so you can't add it as a subview of the UIToolbar. Instead, use the – setItems:animated: method on your toolbar to add those buttons.

    0 讨论(0)
  • 2020-12-31 16:47

    The problem must be in the place from where you add this sub-accessory view.

    0 讨论(0)
  • 2020-12-31 16:58

    I have managed to use your code in my current project with the addition of adding the toolbar on top of a UIDatePicker that gets initiated by a textfield inside a UITableView. I'm using static cells.

    It works for me.

    I have the UITextFields as IBOutlets and only use this code to set the InputAccessoryView

    [textFieldName setInputAccessoryView:self.keyboardToolbar];
    [textFieldEmail setInputAccessoryView:self.keyboardToolbar];
    [textFieldPhone setInputAccessoryView:self.keyboardToolbar]; 
    [textFieldDate setInputAccessoryView:self.keyboardToolbar];
    [textFieldSize setInputAccessoryView:self.keyboardToolbar]; 
    

    Well, the next and prev buttons work like a charm (but only on the way down the table). I still had to manually scroll the table to the top when going back up. For some reason the UITableViewController only handles the scrolling down scenario, which I found quite odd, but here is my code snippet for previous.

    - (void) gotoPrev {
    if (self.activeField == textFieldName) { // name --> nowhere
        return;
    }
    else if (self.activeField == textFieldEmail) { // email --> name
        [textFieldName becomeFirstResponder];
    }
    else if (self.activeField == textFieldPhone) { // phone --> email
        [textFieldEmail becomeFirstResponder];
        // Scroll jeegar
        NSIndexPath * myIndexPath= [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView scrollToRowAtIndexPath:myIndexPath atScrollPosition:UITableViewScrollPositionTop  animated:YES];
    }
    else if (self.activeField == textFieldDate) { // date --> phone
        [textFieldPhone becomeFirstResponder];
    }
    else if (self.activeField == textFieldSize) { // people --> date
        [textFieldDate becomeFirstResponder];
    }    
    }
    
    0 讨论(0)
提交回复
热议问题