iPad横屏、iPhone竖屏
1、启动页LaunchImage
新建一个Image Set,并且命名为LaunchImage(或者其他的名字),准备iPad横屏、iPhone竖屏的启动页,然后在Finder中打开LaunchImage.imageset找到Contents.json,然后修改内容
{
"images" : [
{
"filename" : "iPhone.png",
"idiom" : "iphone",
"scale" : "1x",
"subtype" : "retina4"
},
{
"filename" : "iPhone@2x.png",
"idiom" : "iphone",
"scale" : "2x",
"subtype" : "retina4"
},
{
"filename" : "iPhone@3x.png",
"idiom" : "iphone",
"scale" : "3x",
"subtype" : "retina4"
},
{
"filename" : "HD5.5.png",
"idiom" : "iphone",
"scale" : "3x",
"subtype" : "736h"
},
{
"filename" : "iPhone 8.png",
"idiom" : "iphone",
"scale" : "2x",
"subtype" : "667h"
},
{
"filename" : "iphone xs.png",
"idiom" : "iphone",
"scale" : "3x",
"subtype" : "2436h"
},
{
"filename" : "iPhone Xs max.png",
"idiom" : "iphone",
"scale" : "3x",
"subtype" : "2688h"
},
{
"filename" : "iPhone 11 Pro Max.png",
"idiom" : "iphone",
"scale" : "2x",
"subtype" : "1792h"
},
{
"filename" : "iPad.png",
"idiom" : "ipad",
"scale" : "1x"
},
{
"filename" : "iPad@2x.png",
"idiom" : "ipad",
"scale" : "2x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
filename:图片的名称,不可以重复
idiom:设备,iphone或ipad,全小写
scale:图片倍数
然后在xcode中查看是否显示正常,不正常再调整
在LaunchScreen中添加ImageView,并且添加全屏的约束
注意:Bottom和Top都是连Superview的
项目的General里面启动选择LaunchScreen
在Info.plist中添加iPad和iPhone支持的屏幕方向
上面iPhone只支持竖屏,iPad只支持横屏,这样APP启动的时候,iPhone的启动页是竖屏,iPad的启动页是横屏
发现网上还有另外一种方法设置启动页方向的,我没试过,有兴趣的可以试试,链接在下面
iOS LaunchImage_auccy的博客-CSDN博客
但是,iPhone项目中有个别界面要求是横屏,需要作特殊处理
在AppDelegate中添加以下代码
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if (kIsIpad) {
return UIInterfaceOrientationMaskLandscape;
} else {
return UIInterfaceOrientationMaskAll;
}
}
因为iPhone个别界面需要支持横屏,所以返回UIInterfaceOrientationMaskAll
在根ViewController中添加以下代码:
- (BOOL)shouldAutorotate
{
// 是否支持旋转
return NO;
}
// 设备支持方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if (kIsIpad) {
return UIInterfaceOrientationMaskLandscape;
} else {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
}
// 默认方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if (kIsIpad) {
return UIInterfaceOrientationLandscapeRight;
} else {
return UIInterfaceOrientationPortrait;
}
}
这样每一个继承根ViewController的ViewController都支持同样的屏幕方向
iPhone中个别需要横屏的ViewController不继承根ViewController
然后在需要横屏的OneViewController中添加以下设置屏幕方向方法
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight;
}
-(BOOL)shouldAutorotate {
return NO;
}
如果允许旋转,系统会报错
然后在ViewWillAppear中添加以下代码
if (!kIsIpad) {
//首先设置UIInterfaceOrientationUnknown欺骗系统,避免可能出现直接设置无效的情况
[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationUnknown] forKey:@"orientation"];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
}
上面的方法可以通过App Store的审核,可以放心使用,这样设置是为了让界面的其他组件知道当前的屏幕方向,以正确显示
然后在ViewDidDisappear中将屏幕方向设置回来,防止退出该界面后,其他界面变成横屏
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
if(!kIsIpad) {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationUnknown] forKey:@"orientation"];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationPortrait] forKey:@"orientation"];
}
}
有可能OneViewController加载完成后是竖屏,然后又变横屏,导致横屏的界面异常,这时候可以在ViewDidAppear中重新加载一次界面,这样就完美了
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//重新加载一次界面,避免iPhone竖屏转横屏初始化时出现界面问题
[self.view layoutSubviews];
.....
}
如果在OneViewController中存在UIAlertController,d框的时候会报错,可以新建一个UIAlertController的category,然后添加屏幕方向设置的代码即可
- (BOOL)shouldAutorotate
{
// 是否支持旋转
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if(kIsIpad || [[UIViewController visibleViewController] isKindOfClass:[OneViewController class]]) {
return UIInterfaceOrientationMaskLandscapeRight;
}else {
return UIInterfaceOrientationMaskPortrait;
}
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if(kIsIpad || [[UIViewController visibleViewController] isKindOfClass:[OneViewController class]]) {
return UIInterfaceOrientationLandscapeRight;
}else {
return UIInterfaceOrientationPortrait;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)