How to toggle a word selection view (change height) in iOS custom keyboard?

后端 未结 3 1782
故里飘歌
故里飘歌 2021-01-06 18:16

\"enter

I\'d to like to add a word selection view similar to the above in an iOS 8 cus

相关标签:
3条回答
  • 2021-01-06 18:48

    You want to change the size and contents of input accessory view based on input. Not the frame of the keyboard (input view). The areas you marked with red rectangles are input accessory views in two different states.

    enter image description here

    UIKit posts keyboard related notifications.

    • UIKeyboardWillShowNotification,
    • UIKeyboardDidShowNotification,
    • UIKeyboardWillHideNotification
    • UIKeyboardDidHideNotification

    The object listening for these notifications can get geometry information related to the input view - like height of the keyboard - to adjust the edited views.

    Getting notified about the text change.

    To change the input accessory view based on input you have to first observe changes in its contents. You can do it either by implementing the UITextFieldDelegate's textField:shouldChangeCharactersInRange:replacementString: method or listening for UITextFieldTextDidChangeNotification.

    [[NSNotificationCenter defaultCenter] addObserver:self    
                                             selector:@selector(handleTextFieldDidChangeNotification:) 
                                                 name:UITextFieldTextDidChangeNotification       
                                               object:_textFieldInInputAccessoryView];
    

    Handling the text change.

    The key is to change the input accessory view contents and size when the change in the text field occurs. UIKit attaches the input accessory view to the top of the input view (keyboard). What you have to do is to update the frame of the input accessory view if you want to add an extra line with controls below the text field.

    - (void)handleTextFieldDidChangeNotification:(NSNotification *)notification
    {
         // Update the contents/frame of the input accessory view.
    
         // Reload the input views.
         [_yourTextField reloadInputViews];
    }
    

    Based on Text Programming Guide for iOS - Custom Views for Data Input

    0 讨论(0)
  • 2021-01-06 19:03

    It IS possible to change the size of the keyboard in the current iOS 8

    Taken verbatim from the documentation: "In iOS 8.0, you can adjust a custom keyboard’s height any time after its primary view initially draws on screen."

    To resize your custom keyboard, add a simple layout constraint.

    CGFloat _expandedHeight = 500;
    NSLayoutConstraint *_heightConstraint = 
        [NSLayoutConstraint constraintWithItem: self.view 
                                     attribute: NSLayoutAttributeHeight 
                                     relatedBy: NSLayoutRelationEqual 
                                        toItem: nil 
                                     attribute: NSLayoutAttributeNotAnAttribute 
                                    multiplier: 0.0 
                                      constant: _expandedHeight];
    [self.view addConstraint: _heightConstraint];
    

    For more information look at Apple's prerelease documentation here!

    0 讨论(0)
  • 2021-01-06 19:13

    You can not use the custom keyboard APIs in iOS 8 to extend the keyboard past its default frame. I know this from asking an Apple engineer this question at WWDC this year.

    0 讨论(0)
提交回复
热议问题