问题描述
我只需要支持键盘视图的纵向模式.
i need to support only portrait mode for keyboard view.
因为,我有一个支持所有方向的 uiviewcontroller.它提供了一个仅支持纵向方向的视图控制器,并且该视图具有一个文本字段.当 textfield 成为第一响应者时,弹出键盘视图.现在,如果我更改设备方向,键盘会将其方向更改为横向.
because, i have an uiviewcontroller that supports all orientations. 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. now, if i change the device orientation, keyboard changes its orientation to landscape.
如何让键盘保持纵向?
推荐答案
试试这个:
创建一个名为uiinterfaceorientation previousorientation的ivar.
create an ivar called uiinterfaceorientation previousorientation.
当你说 [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]中,当用户关闭键盘或回车时;您可以恢复到原来的方向.
then in [textfield resignfirstresponder], when the user closes the keyboard or presses enter; 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];