【发布时间】:2011-10-19 23:43:58
【问题描述】:
我只需要支持键盘视图的纵向模式。
因为,我有一个支持所有方向的 UIViewController。它提供了一个仅支持纵向方向的视图控制器,并且该视图具有一个文本字段。当 TextField 成为第一响应者时,弹出键盘视图。现在,如果我更改设备方向,键盘将其方向更改为横向。
如何让键盘保持纵向?
【问题讨论】:
标签: iphone keyboard orientation
我只需要支持键盘视图的纵向模式。
因为,我有一个支持所有方向的 UIViewController。它提供了一个仅支持纵向方向的视图控制器,并且该视图具有一个文本字段。当 TextField 成为第一响应者时,弹出键盘视图。现在,如果我更改设备方向,键盘将其方向更改为横向。
如何让键盘保持纵向?
【问题讨论】:
标签: iphone keyboard orientation
试试这个:
创建一个名为 UIInterfaceOrientation previousOrientation 的 iVar。
当你说[textField becomeFirstResponder]; 时添加:
[viewController shouldAutoRotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
// Set the previousOrientation to whatever it was
previousOrientation = viewController.interfaceOrientation
// This prevents it from leaving portrait orientation
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
然后在[textField resignFirstResponder]中,当用户关闭键盘或回车时;
您可以恢复到原来的方向。
[ViewController shouldAutoRotateToInterfaceOrientation:previousOrientation];
// Turn the notifications back on so that it will be able to rotate again
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
【讨论】:
如果您只需要纵向方向,您可以通过以下方式禁用自动旋转:
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
【讨论】:
UIKeyboardDidShowNotification 将其禁用,然后通过观察 @ 将其重新打开987654323@。希望这会有所帮助。
UITextViewDelegate的相应回调方法中进行