简体   繁体   English

禁用iPhone中键盘的方向

[英]Disable orientation for Keyboard in iPhone

I need to support only Portrait mode for Keyboard view. 我只需要支持键盘视图的纵向模式。

Because, I have an UIViewController that supports all orientations. 因为,我有一个支持所有方向的UIViewController。 It presents an View controller which supports only Portrait orientation and this view has an Text Field. 它提供了一个仅支持纵向方向的视图控制器,该视图具有文本字段。 When TextField becomes first responder Keyboard view is poped up. 当TextField成为第一响应者时,键盘视图会加速。 Now, if I change the device orientation, keyboard changes its orientation to Landscape. 现在,如果我更改设备方向,键盘会将其方向更改为横向。

How do I keep the keyboard in Portrait orientation? 如何将键盘保持在纵向方向?

如果您只需要纵向方向,您可以通过以下方式禁用自动旋转:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

Try this: 尝试这个:

Create an iVar called UIInterfaceOrientation previousOrientation . 创建名为UIInterfaceOrientation UIInterfaceOrientation previousOrientation的iVar。

When you say [textField becomeFirstResponder]; 当你说[textField becomeFirstResponder]; add: 加:

[viewController shouldAutoRotateToInterfaceOrientation:UIInterfaceOrientationPortrait];

// Set the previousOrientation to whatever it was
previousOrientation = viewController.interfaceOrientation

// This prevents it from leaving portrait orientation
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

Then in [textField resignFirstResponder] , when the user closes the keyboard or presses enter; 然后在[textField resignFirstResponder] ,当用户关闭键盘或按回车键时; You can revert to the original orientation. 您可以恢复原始方向。

[ViewController shouldAutoRotateToInterfaceOrientation:previousOrientation];

// Turn the notifications back on so that it will be able to rotate again
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM