ios6,ios7强制转屏

本文详细介绍了如何在iOS应用中通过在父视图控制器中设置视图旋转方向,并在子视图控制器中重写相关方法来实现特定方向的旋转功能。通过调用`setViewOrientation`方法并重写`supportedInterfaceOrientations`、`shouldAutorotate`和`shouldAutorotateToInterfaceOrientation`方法,开发者可以确保应用在不同的设备方向下呈现正确的用户界面布局。此教程适用于希望优化其iOS应用在不同屏幕方向上用户体验的开发者。

在父视图控制器里面写如下代码

-(void)setViewOrientation:(UIInterfaceOrientation )orientation
{
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        [[UIDevice currentDevice] performSelector:@selector(setOrientation:)
                                       withObject:(id)orientation];
    }
    [UIViewController attemptRotationToDeviceOrientation];//这句是关键
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return toInterfaceOrientation==UIInterfaceOrientationPortrait;
}

在子视图控制器的加入,这里子视图控制器要横屏

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear :YES];
    [self setViewOrientation:UIInterfaceOrientationLandscapeRight];
}
//重写下面子类的方法

- (NSUInteger) supportedInterfaceOrientations{

    returnUIInterfaceOrientationMaskLandscapeRight;

}

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0){

    return toInterfaceOrientation == UIInterfaceOrientationLandscapeRight;

}

 

 

转载于:https://www.cnblogs.com/xiaobaizhu/p/3435306.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值