iOS,键盘预测栏可见性
iOS, Keyboard Predictive bar visibility
我有一些 UITextView
的实例,我希望这个 textView
在显示键盘时占据所有空的垂直 space。问题是我不知道键盘的高度是多少,因为它在 iOS 8 中有预测栏,当键盘已经显示时,用户可以更改它的实际高度。我不想更改 textView
的 autocorrectionType
。我对那个酒吧很好,只是想以正确的方式处理它。
所以问题是:有没有可能知道这个栏是否可见?有没有触发用户滑动到 show/hide 这个栏?
提前致谢。
您可以更改将 UITextView 上的 AutocorrectionType 设置为 UITextAutocorrectionTypeNo(或在 IB 中更正为 NO)禁用自动更正以及 iOS 中的预测文本栏 8. 似乎没有办法然而,只禁用预测栏。
yourTextView.autocorrectionType = UITextAutocorrectionTypeNo;
编辑:
check this I think it is gonna be helpful
可以通过添加观察者获取键盘高度:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillChange:", name: UIKeyboardDidChangeFrameNotification, object: nil)
//Method
func keyboardWillChange(notification: NSNotification){
println(notification.userInfo?.description)
}
当用户滑动到 show/hide UIKeyboard 的预测栏时,你可以处理,
第一步
在viewdidLoad()
中声明一个键盘通知并声明一个全局变量kbSize
float kbSize;
- (void)viewDidLoad
{
kbSize=0.0;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
--- rest of your code here---
}
第二步
现在在 keyboardWillShowNotification() 方法中执行以下操作
#pragma mark - Notifications
- (void)keyboardWillShowNotification:(NSNotification *)aNotification{
NSDictionary *infos = aNotification.userInfo;
NSValue *value = infos[UIKeyboardFrameEndUserInfoKey];
CGRect rawFrame = [value CGRectValue];
CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];
if(kbSize==0.0)
{
kbSize=keyboardFrame.size.height;
NSLog(@"prediction bar is visible");
}
else if(keyboardFrame.size.height<kbSize)
{
NSLog(@"prediction bar is not visible");
--- rest of your code here, how you want to change your view when bar is not visible ---
}
else
{
NSLog(@"prediction bar is visible");
--- rest of your code here, how you want to change your view when bar is visible ---
}
}
结论:-
由于 keyboardWillShowNotification 将始终在用户对您的文本字段进行一些编辑或用户滑动以隐藏或显示预测栏时触发。
所以我们只需要在 keyboardWillShowNotification
触发时检查键盘的高度,所以如果用户滑动栏以隐藏然后键盘高度会自动降低并且我们已经将键盘的高度存储在变量 kbSize
,我们只需要检查键盘的当前高度是否小于存储的 kbSize
。因此,通过这种方式,我们可以在运行时检查栏是否可见或用户滑动以隐藏栏。
就我而言,问题是我没有考虑底部安全边距,我不得不减去底部安全边距以在出现键盘时正确设置内容大小。可能会帮助那些在没有预测的情况下努力计算键盘高度的人。
let window = UIApplication.shared.keyWindow
bottomInsets = window?.safeAreaInsets.bottom ?? 0
我有一些 UITextView
的实例,我希望这个 textView
在显示键盘时占据所有空的垂直 space。问题是我不知道键盘的高度是多少,因为它在 iOS 8 中有预测栏,当键盘已经显示时,用户可以更改它的实际高度。我不想更改 textView
的 autocorrectionType
。我对那个酒吧很好,只是想以正确的方式处理它。
所以问题是:有没有可能知道这个栏是否可见?有没有触发用户滑动到 show/hide 这个栏?
提前致谢。
您可以更改将 UITextView 上的 AutocorrectionType 设置为 UITextAutocorrectionTypeNo(或在 IB 中更正为 NO)禁用自动更正以及 iOS 中的预测文本栏 8. 似乎没有办法然而,只禁用预测栏。
yourTextView.autocorrectionType = UITextAutocorrectionTypeNo;
编辑:
check this I think it is gonna be helpful
可以通过添加观察者获取键盘高度:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillChange:", name: UIKeyboardDidChangeFrameNotification, object: nil)
//Method
func keyboardWillChange(notification: NSNotification){
println(notification.userInfo?.description)
}
当用户滑动到 show/hide UIKeyboard 的预测栏时,你可以处理,
第一步
在viewdidLoad()
中声明一个键盘通知并声明一个全局变量kbSize
float kbSize;
- (void)viewDidLoad
{
kbSize=0.0;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
--- rest of your code here---
}
第二步
现在在 keyboardWillShowNotification() 方法中执行以下操作
#pragma mark - Notifications
- (void)keyboardWillShowNotification:(NSNotification *)aNotification{
NSDictionary *infos = aNotification.userInfo;
NSValue *value = infos[UIKeyboardFrameEndUserInfoKey];
CGRect rawFrame = [value CGRectValue];
CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];
if(kbSize==0.0)
{
kbSize=keyboardFrame.size.height;
NSLog(@"prediction bar is visible");
}
else if(keyboardFrame.size.height<kbSize)
{
NSLog(@"prediction bar is not visible");
--- rest of your code here, how you want to change your view when bar is not visible ---
}
else
{
NSLog(@"prediction bar is visible");
--- rest of your code here, how you want to change your view when bar is visible ---
}
}
结论:- 由于 keyboardWillShowNotification 将始终在用户对您的文本字段进行一些编辑或用户滑动以隐藏或显示预测栏时触发。
所以我们只需要在 keyboardWillShowNotification
触发时检查键盘的高度,所以如果用户滑动栏以隐藏然后键盘高度会自动降低并且我们已经将键盘的高度存储在变量 kbSize
,我们只需要检查键盘的当前高度是否小于存储的 kbSize
。因此,通过这种方式,我们可以在运行时检查栏是否可见或用户滑动以隐藏栏。
就我而言,问题是我没有考虑底部安全边距,我不得不减去底部安全边距以在出现键盘时正确设置内容大小。可能会帮助那些在没有预测的情况下努力计算键盘高度的人。
let window = UIApplication.shared.keyWindow
bottomInsets = window?.safeAreaInsets.bottom ?? 0