Swift -《从0到1 - 1》:创建swift默认项目,设置启动图

新建一个swift默认项目,基于Xcode13.x,swift5.x,以后会在此项目基础上进行完善,这篇文章介绍怎么从零开始创建一个swift项目,设置启动图

GitHub Demo 地址

参考文章:
iOS官方设备尺寸介绍
怎么新建一个swift项目
使用LaunchScreen.storyboard设置启动图

一、创建默认项目

1、打开Xcode,新建项目

在这里插入图片描述

2、选择APP

在这里插入图片描述

3、输入项目名,选择语言为Swift,然后选个保存路径点创建

在这里插入图片描述

4、运行项目

因为controller没写代码,默认是个空白页面
至此默认项目创建成功

在这里插入图片描述

二、默认项目改造

1、删除SceneDelegate

删除SceneDelegate.swiftMain.storyboard文件
在项目配置那里,把最低版本为13.0,Main interface改为空

在这里插入图片描述

点击info,删除Application Scene Manifest

在这里插入图片描述

AppDelegate.swift,删除无用代码,然后新增UIWindow代码

在这里插入图片描述

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        //初始化 window
        initWindow()
        
        return true
    }
    
    func initWindow(){
        // 创建窗口
        self.window = UIWindow.init()
        self.window?.backgroundColor = .white
        self.window?.frame = UIScreen.main.bounds
        // 显示窗口
        self.window?.makeKeyAndVisible()
        self.window?.rootViewController = ViewController()
    }
    
}

然后再次运行,出现白板页面

如果出现黑屏,可能是以下问题:
1、info.plist 文件是否删除Application Scene Manifest
2、是否删除SceneDelegate.swift 文件
3、是否删除APPdelegate.swift 中 与Scene相关的代码

2、添加启动图

LaunchScreen.storyboard 添加imageView,添加约束,上下左右都设置为0

在这里插入图片描述

添加约束后效果如下图,此时上下还有安全距离

在这里插入图片描述

在左窗口选中顶部约束—>点击右侧的Second item—>选择Superview—>将Constant设置为0

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

同理,在左窗口选中底部约束—>点击右侧的First item—>选择Superview—>将Constant设置为0

在这里插入图片描述
在这里插入图片描述

然后添加图片,改图片填充模式设为等比例填充
在这里插入图片描述

确保启动文件设置为Launch Screen.storyboard

在这里插入图片描述

再次运行的话应该就有启动图片了(没有的话先确保设置了Launch Screen,然后清一下缓存或者删了APP重新运行)

在这里插入图片描述

这里设置启动图是用了一张图片,还有一种方式,设置多张启动图
内容是直接粘的这篇文章使用LaunchScreen.storyboard设置启动图

1、在Assets.xcassets中右键,选择new Image Set添加图片,如下图所示:
在这里插入图片描述

右键新建的图片 --> Show in Finder --> 进入修改Contents.json --> 修改相应图片组信息

修改前:
在这里插入图片描述

{
  "images" : [
    {
      "idiom" : "universal",
      "scale" : "1x"
    },
    {
      "idiom" : "universal",
      "scale" : "2x"
    },
    {
      "idiom" : "universal",
      "scale" : "3x"
    }
  ],
  "info" : {
    "author" : "xcode",
    "version" : 1
  }
}

修改后:

{
      "images" : [
        {
          "idiom" : "iphone",
          "scale" : "1x"
        },
        {
          "idiom" : "iphone",
          "scale" : "2x"
        },
        {
          "idiom" : "iphone",
          "scale" : "3x"
        },
        {
          "idiom" : "iphone",
          "subtype" : "retina4",
          "scale" : "1x"
        },
        {
          "idiom" : "iphone",
          "subtype" : "retina4",
          "scale" : "2x"
        },
        {
          "idiom" : "iphone",
          "subtype" : "retina4",
          "scale" : "3x"
        },
        {
          "idiom" : "iphone",
          "subtype" : "736h",
          "scale" : "3x"
        },
        {
          "idiom" : "iphone",
          "subtype" : "667h",
          "scale" : "2x"
        },
        {
          "idiom" : "iphone",
          "subtype" : "2436h",
          "scale" : "3x"
        },
        {
          "idiom" : "iphone",
          "subtype" : "2688h",
          "scale" : "3x"
        },
        {
          "idiom" : "iphone",
          "subtype" : "1792h",
          "scale" : "2x"
        }
      ],
      "info" : {
        "version" : 1,
        "author" : "xcode"
      }
    }

在这里插入图片描述
具体尺寸也可参考官方原文:https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/adaptivity-and-layout/#device-screen-sizes-and-orientations

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值