为了账号安全,请及时绑定邮箱和手机立即绑定

如何只允许单个UIViewController沿横向和纵向旋转?

如何只允许单个UIViewController沿横向和纵向旋转?

神不在的星期二 2019-10-29 10:51:10
我的应用仅适用于iphone设备(iphone 4和5),并且仅支持ios 6。我的整个应用仅支持portrait模式。但是有一个名为“ ChatView”的视图,我想同时支持landscape和portrait模式。我已将所需的设备旋转设置如下:我还尝试了以下代码来支持“ ChatView”中的旋转--(BOOL)shouldAutorotate{    return YES;}-(NSUInteger)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskLandscape;}但是它无法旋转该视图。我为此进行了很多搜索,但找不到我的问题的解决方案。而且在“ ChatView”中,还有一些对象,例如按钮,文本字段,其框架是通过编程方式设置的。所以我想知道我是否还必须为横向模式设置所有这些对象的框架?请帮我。谢谢.....
查看完整描述

3 回答

?
慕姐8265434

TA贡献1813条经验 获得超2个赞

简单,但效果很好。iOS 7.1和8


AppDelegate.h


@property () BOOL restrictRotation;

AppDelegate.m


-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

{

if(self.restrictRotation)

    return UIInterfaceOrientationMaskPortrait;

else

    return UIInterfaceOrientationMaskAll;

}

ViewController


-(void) restrictRotation:(BOOL) restriction

{

    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;

    appDelegate.restrictRotation = restriction;

}

viewDidLoad


[self restrictRotation:YES]; or NO


查看完整回答
反对 回复 2019-10-29
?
三国纷争

TA贡献1804条经验 获得超7个赞

您的视图控制器将永远不会旋转到应用程序本身不支持的任何位置。您应该启用所有可能的旋转,然后在不应旋转的视图控制器中放入以下行


- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    return UIInterfaceOrientationMaskPortrait;

}

在ChatView中,应为:


- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    return UIInterfaceOrientationMaskAll;

}

如果您需要在轮换之后更改布局,则应对子视图进行适当的更改


- (void)viewWillLayoutSubviews

使用self.view.bounds检查的电流的大小view,因为self.view.frame旋转后也不会改变。


查看完整回答
反对 回复 2019-10-29
  • 3 回答
  • 0 关注
  • 440 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信