inputAccessoryView with custom view hide After I Dismiss keyboard

烈酒焚心 提交于 2021-02-08 11:58:23

问题


First of all , my project is so structured:

-home page with a tableView to add element

-floating Button: when clicked make appear keyboard with above a custom view (and inside a textView) to digit input

Now, I have a problem with inputAccessoryView and dismiss keyboard:

I have used inputAccessoryView to move a custom view above the keyboard like this:

MytextView.inputAccessoryView= MyContainerView; 

And this work correctly.

My problem occur when I dismiss keyboard using:

[MytextView resignFirstResponder];

The keyboard and relative inputView disappear properly but After when I try again to make MytextView the firstResponder does not work (the keyboard not appear).

I hypothesize that occurs because textView is hide with inputAccessoryView under the screen and inputAccessoryView change The inizial position of textview (Initial in the Middle of the screen); so textView is not focusable and keyboard not appear when I use:

[MyTextView becomeFirstResponder]

Is there a way to reposition programmatically the textView to initial position (middle of the screen) so can become focusable when I call becomeFirstResponder?

Or is there a way to fix inputAccessoryView in the safe area when i dismiss keyboard?

(Sorry , I’m New to objective-c and IOS)

Thanks!


回答1:


Let me jump out of the comments to have more freedom. Based on your description you could do something like this

- (void) viewDidLoad
{
    super.viewDidLoad;
    [NSNotificationCenter.defaultCenter addObserver:self
                           selector:@selector( keyboardDidHide: )
                           name:UIKeyboardDidHideNotification
                         object:nil];
}

- ( void ) keyboardDidHide:( NSNotification * ) notification
{
... do stuff here...
}

Then you can reposition or do whatever you need in there. Alternatively, you can also listen for UIKeyboardDidShowNotification and do preparatory stuff there. In fact, there is a whole family of UIKeyboard... notifications that you could use this way and I hope it helps.

EDIT

Here is something else, if you ever need to reposition based on the keyboard size.

- ( void ) keyboardDidShow:(NSNotification *)notification
{
    NSDictionary * info = notification.userInfo;
    CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];

... do stuff with kbRect ...
}



回答2:


have you tried to use

[self.view endEditing:YES]

to dismiss the input view?

I never get any issue to set any input to firstResponder after I trigger that code.



来源:https://stackoverflow.com/questions/63094204/inputaccessoryview-with-custom-view-hide-after-i-dismiss-keyboard

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