Ben*_*y B 14 uikeyboard nsnotification ios
我试图在键盘出现/更改时在键盘上方移动UITextView.假设我显示英文键盘,然后直接切换到中文键盘(比标准英文键盘高).在这种情况下,我的文本视图总是显得太高(肉眼看来,文本视图被中文键盘"输入附件视图"的大小错误地偏移.我发布的图片却缺乏声誉所以:)).当我的应用程序收到UIKeyboardDidShowNotification(使用UIKeyboardFrameEndUserInfoKey获取高度)时,我正在调整文本视图位置,经过一些调查后,UIKeyboardDidShowNotification被多次调用,通常是键盘尺寸不正确(我已经NSLogged了userInfo字典).我在ViewWillAppear中注册我的键盘通知,并在ViewWillDisappear中取消注册.我无法确定导致此通知多次触发的原因; 我的理解是,只有在键盘完成显示后才会触发此通知一次.另外需要注意:我在响应UIKeyboardDidShowNotification的方法中使用了NSLogged"self",它实际上始终是相同的View Controller对象.
但是,即使多次触发此通知,我仍然不明白为什么某些通知的键盘高度会有所不同.其中一个通知始终具有正确的高度,但是当它不是最后一个通知时,文本视图最终会出现在错误的位置.任何有关如何进一步排除故障的见解将非常感激!
编辑:我测试的越多,特别是中文键盘的问题就越多.每当我将键盘从英文切换到中文时,我得到三个UIKeyboardWillShowNotifications:
2014-12-24 22:49:29.385 Example[1055:421943] info dictionary: {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 252}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 460}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 442}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 316}, {320, 252}}";
}
2014-12-24 22:49:29.408 Example[1055:421943] info dictionary: {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 442}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 316}, {320, 252}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";
}
2014-12-24 22:49:29.420 Example[1055:421943] info dictionary: {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 288}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 442}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 424}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 316}, {320, 252}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 280}, {320, 288}}";
}
Run Code Online (Sandbox Code Playgroud)
第一个具有正确的结束高度:252.然而,接下来的两个在216和288处是不正确的.这可靠地发生.
以下是一些片段,用于演示我如何管理订阅通知:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self registerForKeyboardNotifications];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];
}
- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification object:nil];
}
Run Code Online (Sandbox Code Playgroud)
如果您在模拟器上使用Cmd + K快捷键来显示/隐藏键盘,则可能会多次调用它,因为它不会将文本字段作为第一个响应者重新调用.
如果您改为使用键盘Return按钮,那么它将做正确的事情并将其重新签名.
主要原因是您的通知被多次调用。例如,在 swift 中,如果你在 viewWillAppear 方法中有 NSNotificationCenter.defaultCenter().addObserver(xx"keyboardWillShow"xx) ,并且如果你在视图之间来回移动,那么它将导致有多个相同的观察者“键盘将显示”。相反,您应该考虑将 addObserver 调用移至“viewDidLoad()”方法。或者,您可以在视图出现/消失时注册/取消注册观察者。
小智 0
- (void)keyboardDidAppear:(NSNotification *)note {\n CGSize keyboardSize = [note.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;\n UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);\n textView.contentInset = contentInsets;\n textView.scrollIndicatorInsets = contentInsets;\n}\n\n- (void)keyboardWillBeHidden:(NSNotification *)note {\n UIEdgeInsets contentInsets = UIEdgeInsetsZero;\n textView.contentInset = contentInsets;\n textView.scrollIndicatorInsets = contentInsets;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n解决方案来自Apple的官方解决方案,如\xe6\x9b\xb9所说。
\n