iOS 竖屏切换到横屏的正确方式

方法一.强制手机旋转(自带有旋转动画)

1.appdelegate 代码如下

#import  <UIKit/UIKit.h>

@interfaceAppDelegate :UIResponder <UIApplicationDelegate>

@property(nonatomic,assign)BOOLshouldRote;

@property (strong, nonatomic) UIWindow *window;

@end

#import"AppDelegate.h"

@interfaceAppDelegate()

@end

@implementationAppDelegate

//屏幕旋转全屏

-(UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window{

   UIDevice*device = [UIDevicecurrentDevice];

   if(!_shouldRote|| device.orientation==UIDeviceOrientationPortraitUpsideDown)returnUIInterfaceOrientationMaskPortrait;

   returnUIInterfaceOrientationMaskLandscapeRight;

}

@end

2.跳转横屏控制器代码

AppDelegate*delegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;

 delegate.shouldRote=YES;

[[UIDevicecurrentDevice]setValue:[NSNumbernumberWithInt:UIDeviceOrientationLandscapeRight] forKey:@"orientation”];

[self.navigationController pushViewController:[NextVC new] animated:YES];


方法二.正常push/present(无旋转动画)

1.在横屏控制器添加横屏contentview,在contentview里面添加ui控件

- (void)viewDidLoad {

    [superviewDidLoad];

    self.contenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.height, self.view.width)]; 

    [self.view addSubview:self.contenView];

    [self.view addSubview:subviews];

}

2.旋转contentview

- (void)viewDidLayoutSubviews{

    [superviewDidLayoutSubviews];

   //由于旋转后的origin偏差,修正contenView的origin

//    [UIView animateWithDuration:0.3 animations:^{

       self.contenView.transform=CGAffineTransformRotate(CGAffineTransformIdentity,M_PI_2);

       self.contenView.frame=self.view.bounds;

//    }];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。