IOS 用代码配置启动页

IOS 用代码配置启动页,第1张

1,xcode版本10.1,首先在LaunchScreen.storyboard上设置一下StoryBoard ID 如图

2,在APPDelegate.m中增加属性:@property (strong, nonatomic) UIView *launchView

3,在- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions;中调用下面方法

-(void)setLaunchImg{

    UIViewController *viewController = [[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil] instantiateViewControllerWithIdentifier:@"LaunchScreen"]

    self.launchView= viewController.view

    self.launchView.frame = CGRectMake(0, 0, self.window.screen.bounds.size.width, self.window.screen.bounds.size.height)

    [self.window addSubview:self.launchView]

    UIImageView*imageV = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,self.launchView.frame.size.width,self.launchView.frame.size.height)]

    [imageVsetImage:[UIImageimageNamed:@"logo.jpeg"]]

    imageV.contentMode = UIViewContentModeScaleAspectFill

    [self.launchViewaddSubview:imageV]

    [self.window bringSubviewToFront:self.launchView]

//可以设置启动页的存在时间

    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(removeLun) userInfo:nil repeats:NO]

}

-(void)removeLun

{

    [self.launchView removeFromSuperview]

}

自从Xcode 12+开始,使用LaunchImage配置固定大小图片的方案已被苹果爸爸废弃,想要设置启动页需要通过LaunchScreen,个人理解苹果之所以作这样的改动是基于苹果新推出的全面屏和越来越多的屏幕尺寸以及Safe Area机制(这里要注意,Safe Area需要iOS 9+)

创建工程,随之会自动创建一个LaunchScreen.storyboard的文件

点击打开可以发现和传统的xib文件无差别

可以在这个storyboard上可以放置各种ImageView、Label等,但需要注意UI控件必须添加在Safe Area区域内,这样无论是iPhone X之后的刘海全面屏还是iPhone 8这样的非全面屏都可以正常显示,通过下方的View as:iPhone 11 可以选择各种不同的机型/横竖屏来查看当前启动页的展示情况

如果本文对你有所帮助记得点个赞哈 :)

两种方法设置:

LaunchScreen.storyboard

在LaunchScreen.storyboard中拖拽imageView并设置约束,勾选右侧Use as launch Screen

工程—General—App Icons and Launch Images选项中设置Launch Screen File为所制作的LaunchScreen.storyboard或者LaunchScreen.xib

Assets.xcassets

设置LaunchImage

准备:

各种尺寸启动图:640 × 960,640 × 1136,750 × 1334,1242 × 2208,(横平需要2208 ×1242)

iPhone Portrait iOS5,6(1x:320 × 480 pixels, 2x:640 × 960 pixels, Retina 4:640 × 1136 pixels)

iPhone Portrait iOS8,9(Retina HD 5.5”:1242 × 2208 pixels, Retina HD 4.7”:750 × 1334 pixels)

iPhone Landscape iOS 8,9(Retina HD 5.5”:2208 × 1242 pixels)

iPhone Portrait iOS7,9(2x:640 × 960 pixels, Retina 4:640 × 1136 pixels)

iPhone X Portrait iOS 11+ (3x:1125 x 2436 pixels)

配置

工程—General—App Icons and Launch Images选项中设置Launch Image Source 为LaunchImage

LaunchScreen.storyboard 取消勾选Use as Launch Screen

置空路径Launch Screen File


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7816465.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存