UIKeyBoard 在方向更改为横向时调整大小
这是一个非常菜鸟的问题。我在底部有一个 UIToolBar,当显示 UIKeyBoard 时,它应该随着键盘上下动画移动。我在 UIKeyBoard 通知的帮助下实现了这一点。我们正在讨论的视图启用了分割视图。当设备方向为横向时,两个视图都会显示为列[希望有意义]。
当显示键盘时,我这样做
CGSize keyBoardSize = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
CGRect toolbarFrame= [BottomToolBar frame];
toolbarFrame.origin.y -= keyBoardSize.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
BottomToolBar .frame = viewFrame;
[UIView commitAnimations];
当键盘隐藏时我这样做
toolbarFrame.origin.y += keyBoardSize.height;
我的问题是当设备方向更改为横向时,当键盘可见时,底部工具栏消失了。我看到它上升得很快。我不知道如何解决这个问题。有人可以帮忙吗?另外,有没有办法让键盘跨越分割视图中的两个视图?
This is very rookie question. I have a UIToolBar at the bottom which is supposed to animatedly move up and down with key board when the UIKeyBoard is displayed. I got that working with the help of UIKeyBoard Notifications. The view we are talking about has split view enabled. When device orientation is landscape, both the views as columns are shown [hope that makes sense].
When key board is shown, i do this
CGSize keyBoardSize = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
CGRect toolbarFrame= [BottomToolBar frame];
toolbarFrame.origin.y -= keyBoardSize.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
BottomToolBar .frame = viewFrame;
[UIView commitAnimations];
when key board is hiiden i do this
toolbarFrame.origin.y += keyBoardSize.height;
My Problem is when device orientation changes to landscape, when the key board is visible the bottom tool bar is gone. I see it move up quickly. I am not sure how to fix this. Can anyone help please? Also, is there a way to NOT make the key board span across both the views in the split view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我也有这个问题,我能想到的就是关闭键盘并重新显示它(辞职然后再次成为第一响应者)。但这似乎很不令人满意。
另请注意,您应该将矩形从屏幕坐标转换为视图坐标。 (屏幕坐标不旋转。)
更新:您必须注册 UIKeyboardWillShowNotification,然后当界面旋转时将调用您的操作:)
另请参阅:
https://devforums.apple.com/message/181482#181482
I have this problem too, all I can think of is dismissing the keyboard and reshowing it (resign and then become first responder again). But that seems very unsatisfactory.
Note also you should be converting the rect from screen coordinates to the view's coordinates. (The screen coordinates don't rotate.)
UPDATE: You must register for UIKeyboardWillShowNotification, then your action will be called when the interface rotates :)
Also see:
https://devforums.apple.com/message/181482#181482
UITextFields 有一个 inputAccessoryView 属性,允许在 UIKeyboard 上方添加 UIToolBar。
UITextFields have an inputAccessoryView property that permits adding a UIToolBar above the UIKeyboard.
除了外部键盘之外,这些答案都很有效。当存在硬件键盘时,消息中传递的键盘框架的“高度”属性看起来仍然与虚拟键盘相同,这意味着此代码从非键盘的视图框架中减去高度。现有的键盘(创造一个尴尬的空间)。
我发现的最佳解决方案是注意键盘框架的“y”属性被指定为位于屏幕底部(这意味着虚拟键盘实际上存在,就在屏幕之外)。
These answers work well, except for external keyboards. When a hardware keyboard is present, it looks like the "height" property of the keyboard frame passed in the message is still the same as it would be with a virtual keyboard, meaning this code subtracts the height from the view frame for a non-existent keyboard (creating an awkward space).
The best solution I've found is to notice that the "y" property of the keyboard frame is specified to be at the bottom of the screen (implying that the virtual keyboard is actually present, just off the screen).
设置选项
UIViewAnimationOptionBeginFromCurrentState
解决方案是为基于块的动画或您的情况 。这样,当方向从纵向更改为横向时,视图不会突然移动。
The solution is to set option
UIViewAnimationOptionBeginFromCurrentState
for block based animation, orin your case. Then the view will not get a sudden movement when changing orientation from portrait to landscape.