我在textView的inputAccessoryView属性中使用工具栏。当键盘显示时,它会按预期显示工具栏。旋转设备时,我想删除工具栏。我试过了:
myTextView.inputAccessoryView.hidden = !layoutIsPortrait;
这会隐藏工具栏,但会留下较高键盘的轮廓。键盘显然仍然适合工具栏。它看起来很糟糕并且会干扰潜在响应者的触摸事件。
myTextView.inputAccessoryView = nil;
只有在我resignFirstResponder,然后再次成为FirstRirstResponder时才有效。这是不可接受的。我丢失了光标位置和textView的内容,键盘闪烁,然后返回。
[myTextView.inputAccessoryView removefromSuperview];
什么都不做。 我在iVar中保存了对工具栏的引用,并代之以
[myIvarReference removeFromSuperview];
这样可行,但键盘的较高轮廓再次隐约可见。这次它不会干扰其他视图的触摸。所以现在这是一个有效的解决方案,但在视觉上是不可接受的。 我还可以尝试显示和隐藏inputAccessoryView吗?
屏幕截图 - 键盘上方的微弱线条是已删除工具栏的残余
答案 0 :(得分:27)
myTextView.inputAccessoryView = nil;
[myTextView reloadInputViews];
这将从视图中删除工具栏并重新加载视图。这样您就不需要调用resignFirstResponder和becomeFirstResponder。此外,这仍将保留您的光标位置和内容。
答案 1 :(得分:5)
对我而言,Eric的解决方案从未实际重置帧或触摸区域。据推测,这是Apple如何处理事情的错误。但是,我找到了解决这个问题的解决方法。当我设置一个没有框架的新inputAccessoryView时,reloadInputViews工作正常:
myTextView.inputAccessoryView = [[UIView alloc] initWithFrame: CGRectZero];
[myTextView reloadInputViews];
答案 2 :(得分:5)
上面的答案都没有对我有用,并且reloadInputViews导致了奇怪的问题。最终,我得到它来展示和隐藏,并通过以下方式接触直通:
隐藏它:
[textview.inputAccessoryView setHidden:YES];
[textview.inputAccessoryView setUserInteractionEnabled:NO];
显示:
[textview.inputAccessoryView setHidden:NO];
[textview.inputAccessoryView setUserInteractionEnabled:YES];
答案 3 :(得分:2)
从未找到改变键盘框架的方法。最终决定放弃inputAccessoryView,将我的工具栏作为子视图直接添加到视图中,并直接将其与键盘一起动画。这使两者保持独立,因此不再有线。
答案 4 :(得分:1)
Xamarin代码
Control.InputAccessoryView = new UIView(CGRect.Empty);
Control.ReloadInputViews();
答案 5 :(得分:1)
奇怪的是,这些方法都不适用于我的情况。
我有一个searchcontroller,如果选择了特定的搜索范围,它会弹出一个标准的Apple iOS键盘,以及一个自定义键盘视图,如果选择了其他范围,则会将一个集合视图作为输入字段。 在这两种情况下,当显示输入视图时,屏幕上会绘制一个不需要的附件视图。
所以,
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]
及其各种组合等。
从配件视图中删除个别配件项的工作是什么:
// insert after assignments of mySearch delegates
UITextInputAssistantItem *junk = [self.mySearch inputAssistantItem];
junk.leadingBarButtonGroups = @[];
junk.trailingBarButtonGroups = @[];
答案 6 :(得分:0)
根据Eric Appel的回答:
myTextView.inputAccessoryView = nil;
[myTextView reloadInputViews];
hideInputAccessoryView = YES;
进一步修改:
- (BOOL)canBecomeFirstResponder
{
BOOL showInputAccessoryView = YES;
if (hideInputAccessoryView)
showInputAccessoryView = NO;
return showInputAccessoryView;
}
即使键盘被重新签名,这也应隐藏InputAccessoryView。
答案 7 :(得分:0)
LD_LIBRARY_PATH
对我有用,将mTextView.inputAccessoryView = [[UIView alloc] initWithFrame:CGRectZero];
[mTextView reloadInputViews];
设置为inputAccessoryView
将无效,我只是不知道原因。
答案 8 :(得分:0)
Xcode 11.6和iOS 13.6
我试图在键盘出现时添加两个工具栏。
首先,我在viewDidLoad
self.textView.inputAccessoryView = self.addDefaultToolbar()
出现键盘后,我在UItextView中选择了text
并尝试添加第二个工具栏选项
以下代码未在选择的Toolbar
上显示text
。
func textViewDidChangeSelection(_ textView: UITextView) {
if let selectedRange = textView.selectedTextRange{
let selectedText = textView.text(in: selectedRange)
if selectedText != nil && selectedText!.count > 0{
print("selectedText - \(selectedText!)")
if self.defaultToolBar != nil{
self.defaultToolBar?.removeFromSuperview()
self.defaultToolBar = nil
}
self.textView.inputAccessoryView = self.addSelectionToolbar()
}
else{
print("not selectedText - \(selectedText!)")
if self.selectionToolBar != nil{
self.selectionToolBar?.removeFromSuperview()
self.selectionToolBar = nil
}
self.textView.inputAccessoryView = self.addDefaultToolbar()
}
}
}
添加了self.textView.reloadInputViews()
之后,我可以看到更改了第二个工具栏,反之亦然。
工作代码。
func textViewDidChangeSelection(_ textView: UITextView) {
if let selectedRange = textView.selectedTextRange{
let selectedText = textView.text(in: selectedRange)
if selectedText != nil && selectedText!.count > 0{
print("selectedText - \(selectedText!)")
if self.defaultToolBar != nil{
self.defaultToolBar?.removeFromSuperview()
self.defaultToolBar = nil
}
self.textView.inputAccessoryView = self.addSelectionToolbar()
self.textView.reloadInputViews()
}
else{
print("not selectedText - \(selectedText!)")
if self.selectionToolBar != nil{
self.selectionToolBar?.removeFromSuperview()
self.selectionToolBar = nil
}
self.textView.inputAccessoryView = self.addDefaultToolbar()
self.textView.reloadInputViews()
}
}
}
答案 9 :(得分:0)
快捷键5
我使用这个:
view.autocorrectionType = .no
答案 10 :(得分:0)
快捷键5
我使用这个:
func textFieldShouldBeginEditing(_ textField:UITextField)->布尔{ self.view.endEditing(true) 返回真 }