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

后端 未结 3 1781
故里飘歌
故里飘歌 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

提交回复
热议问题