How to hide inputAccessoryView without dismissing keyboard

青春壹個敷衍的年華 提交于 2019-11-30 18:29:11
myTextView.inputAccessoryView = nil;
[myTextView reloadInputViews];

This removes the toolbar from the view and reloads the view. This way you don't need to call resignFirstResponder and becomeFirstResponder. Additionally, this will still keep your cursor placement and content.

For me Eric's solution never actually reset the frame or the touch areas. Presumably it's a bug with how Apple handles things. However, I found a workaround that solved the problem for me. When I set a new inputAccessoryView without a frame, reloadInputViews worked fine:

myTextView.inputAccessoryView = [[UIView alloc] initWithFrame: CGRectZero];
[myTextView reloadInputViews];

None of the answers above were working for me and reloadInputViews was causing weird issues. Eventually I got it to show and hide and have touches passthrough by doing:

Hide it:

[textview.inputAccessoryView setHidden:YES];
[textview.inputAccessoryView setUserInteractionEnabled:NO];

Show it:

[textview.inputAccessoryView setHidden:NO];
[textview.inputAccessoryView setUserInteractionEnabled:YES];

Never found a way to alter the frame of the keyboard. Ultimately decided to forego the inputAccessoryView, add my toolbar directly to the view as a subview and animate it myself along with the keyboard directly. This keeps the two independent and so, no more line.

Xamarin code is

Control.InputAccessoryView = new UIView(CGRect.Empty);
Control.ReloadInputViews();

Oddly enough, none of these approaches worked in my case.

I have a searchcontroller which pops up a standard Apple iOS keyboard if a particular search scope is selected, and a custom keyboard view with a collection view as the input field in cases of other scopes being selected. In both cases, an undesired accessory view was drawn on the screen when the input view was displayed.

So,

self.mySearch.searchbar.inputAccessoryView = nil // did not work

[self.mySearch.searhbar.inputAccessoryView setHidden:YES] // did not work

self.mySearch.inputAccessoryView = nil  // did not work

self.mySearch.searchbar.inputAccessoryView.frame = CGRectZero //did not work

[self.mySearch reloadInputViews]

and various combinations thereof etc, etc.

What did work was to delete to individual accessory items from the accessory view:

 // insert after assignments of mySearch delegates
UITextInputAssistantItem *junk = [self.mySearch inputAssistantItem];
junk.leadingBarButtonGroups = @[];
junk.trailingBarButtonGroups = @[];

Based on Eric Appel's answer:

myTextView.inputAccessoryView = nil;
[myTextView reloadInputViews];
hideInputAccessoryView = YES;

Further modify:

- (BOOL)canBecomeFirstResponder
{
    BOOL showInputAccessoryView = YES;

    if (hideInputAccessoryView)
        showInputAccessoryView = NO;

    return showInputAccessoryView;
}

This should hide InputAccessoryView even when the keyboard is resigned.

mTextView.inputAccessoryView = [[UIView alloc] initWithFrame:CGRectZero];
[mTextView reloadInputViews];

works for me, setting inputAccessoryView to nil will not work, I just don't know why.

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