iOS 横竖屏强制转换

强制转成横屏:
// 如果要上传AppStore请慎用,不知道是否能够通过,未尝试
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationLandscapeRight;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}

通过判断状态栏来设置视图的transform属性。

  • (void)deviceOrientationDidChange: (NSNotification *)notification
    {
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGFloat startRotation = [[self valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
    CGAffineTransform rotation;
    switch (interfaceOrientation) {
    case UIInterfaceOrientationLandscapeLeft:
    rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 270.0 / 180.0);
    break;
    case UIInterfaceOrientationLandscapeRight:
    rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 90.0 / 180.0);
    break;
    case UIInterfaceOrientationPortraitUpsideDown:
    rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 180.0 / 180.0);
    break;
    default:
    rotation = CGAffineTransformMakeRotation(-startRotation + 0.0);
    break;
    }
    view.transform = rotation;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 强制转成横屏:// 如果要上传AppStore请慎用,不知道是否能够通过,未尝试if ([[UIDevice cu...
    seventhboy阅读 300评论 0 0
  • iOS 中横竖屏切换的功能,在开发iOS app中总能遇到。以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰...
    零度_不结冰阅读 2,319评论 0 0
  • 1、改变 UITextField 占位文字 颜色和去掉底部白框 [_userName setValue:[UICo...
    i_MT阅读 1,215评论 0 2
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 2,105评论 0 3
  • 昨夜夜,做了一场古怪的梦,梦的内容有些难以启齿,可能由于我单身多年,又结束了一段极为短暂恋爱未满关系,胸中些许抑...
    anybodysomeone阅读 166评论 0 0

友情链接更多精彩内容