以前都是将知识点写在 《iOS - 小常识知识点这篇文章》 ,但是后来累积的太多,所以还是决定抽时间按功能模块抽分出来!
0.点项目 - Targets - General - Deployment Info ,如图
第一、默认所有竖屏之强制横屏版 -- UIDevice 和 Animation 版 (无需解锁即可强制横竖屏)
1.项目名称 - TARGETS - General - Deployment Info - Device Orientation , 勾选Portrait !
2.创建按钮,并实现点击按钮,强制横屏
UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(10, 64+10, WIDTH-20, 200)];
navView.backgroundColor = [UIColor clearColor];
[self.view addSubview:navView];
UIButton *testBtn = [UIButton buttonWithType:UIButtonTypeCustom];
testBtn.backgroundColor = SLIVERYCOLOR;
testBtn.frame = CGRectMake(0, 170, 50, 30);
[navView addSubview:testBtn];
[testBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
- (void)btnClick:(UIButton *)sender{
sender.selected = !sender.selected;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
if (sender.selected) {
[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
}else{
[self interfaceOrientation:UIInterfaceOrientationPortrait];
}
}
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
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 = orientation;
// 从2开始是因为0 1 两个参数已经被selector和target占用
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
- (void)deviceOrientationDidChange
{
NSLog(@"NAV deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);
if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
[self orientationChange:NO];
//注意: UIDeviceOrientationLandscapeLeft 与 UIInterfaceOrientationLandscapeRight
} else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
[self orientationChange:YES];
}
}
- (void)orientationChange:(BOOL)landscapeRight
{
if (landscapeRight) {
[UIView animateWithDuration:0.2f animations:^{
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
self.view.bounds = CGRectMake(0, 0, WIDTH, HEIGHT);
}];
} else {
[UIView animateWithDuration:0.2f animations:^{
self.view.transform = CGAffineTransformMakeRotation(0);
self.view.bounds = CGRectMake(0, 0, WIDTH, HEIGHT);
}];
}
}
- (BOOL)shouldAutorotate
{
return NO;
}
3.如果该横屏控制器为根控制器,此步忽视!如果该横屏控制器为导航控制器下的子控制器,或者是tabbar控制器下的导航控制器下的子控制器,则需要重写 父控制器的 shouldAutorotate 方法直到跟控制器未知!
#pragma mark - 注意点:必须重写父类控制器直至根控制器!在重写方法的 shouldAutorotate 方法里面传入需要横屏的控制器!
#pragma mark - 旋转相关 (tabbar控制器下的子控制器需要旋转需要重写此方法,且需要旋转哪个控制器就传入哪个)
- (BOOL)shouldAutorotate
{
//在viewControllers中返回需要改变的NavigationController
//由于此处项目中只有一个基类导航控制器,所以五个控制器名称相同,就取 要横屏的第四个导航控制器 - self.viewControllers[3]
return [self.viewControllers[3] shouldAutorotate];
}
#pragma mark - 旋转相关 (导航控制器下的子控制器需要旋转需要重写此方法,且需要旋转哪个控制器就传入哪个)
- (BOOL)shouldAutorotate
{
//在viewControllers中返回需要改变的viewController
for (id obj in self.viewControllers) {
if ([obj isKindOfClass:[MSUPlayerController class]]) {
return [obj shouldAutorotate];
}
}
return 0;
}
第二、默认所有竖屏之强制横屏版 -- AppDelegate BOOL版 (需手动解锁锁屏按钮方可横竖屏)
0.项目名称 - TARGETS - General - Deployment Info - Device Orientation , 勾选Portrait !
1.在 appdelegate 头文件中添加属性
/// 判断是否横竖屏
@property (nonatomic,assign)BOOL allowRotation;
2.在 appdelegate.m 文件中实现方法
/* 横竖屏 */
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;
}
3.在需要横屏的文件中,导入头文件 #import "AppDelegate.h" ,添加如此代码
/// 横竖屏相关
AppDelegate *app =(AppDelegate *)[[UIApplication sharedApplication] delegate];
app.allowRotation = YES;
第三、UI适配版
1.控制器中横竖屏如何分写
1).在控制器中的 viewWillLayoutSubviews 中分横竖屏情况写控件 frame
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// 通过状态栏电池图标来判断屏幕方向
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) {
// 竖屏 balabala
} else {
// 横屏 balabala
}
}
2).在控制器中的 viewWillTransitionToSize 中分横竖屏情况写控件 frame
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator {
if (size.width > size.height) { // 横屏
// 横屏布局 balabala
} else {
// 竖屏布局 balabala
}
}
2.UIView 中横竖屏如何分写
- (void)layoutSubviews{
[super layoutSubviews];
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) {
// 竖屏 balabala
} else {
// 横屏 balabala
}
}