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

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 18:52:05

问题


I'd to like to add a word selection view similar to the above in an iOS 8 custom keyboard.

  1. How can I change the keyboard view rectangle size dynamically? and
  2. notify the active app (such as the built-in Messages app) to reposition the input bar correspondingly?

UPDATE: based on this, the height of keyboard is customisable. How can that be possible?


回答1:


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!




回答2:


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.

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




回答3:


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.



来源:https://stackoverflow.com/questions/25010193/how-to-toggle-a-word-selection-view-change-height-in-ios-custom-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!