iOS:手机横屏时,按下横屏视图上的导航条“返回”按钮后,切换为另一个竖屏视图

验证了很多网上介绍的方法,基本上分为两类:

方法一:旋转视图,然后重新绘画view;

方法二:使用一个已经被apple放弃的私有API函数”setOrientation”(详见参考资料),实现旋转视图


(1)方法一的实例:

[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait animated: YES];
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.transform = CGAffineTransformMakeRotation(M_PI*2);
self.navigationController.view.bounds = CGRectMake(self.navigationController.view.bounds.origin.x,
                                                   self.navigationController.view.bounds.origin.y,
                                                   self.view.frame.size.height,
                                                   self.view.frame.size.width);

调试结果:虽然实现了切换为竖屏,但是视图已经偏移到手机界面外了。实验了很多次,都没有能够调试出想要的效果。


(2)方法二的实例:

// Force main view to Portrait
if(UIDeviceOrientationIsLandscape(self.interfaceOrientation)) {
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait);
    }
}

备注:由于需要调用方法“objc_msgSend”,故需要包含下面的头文件:

#import <objc/message.h>

调试结果:调试一次,就能实现需求。方法二的风险:因为调用了apple的一个私有API,可能在APP上架apple store被审核不通过(备注:有待实际验证)。


后记:为什么apple不提供一个官方的方法呢?害得我为了实现这么一个简单的需求,到处搜罗实现方法!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值