How to create an Empty Application in Xcode without Storyboard

Xcode6 has removed the Empty Application template when creating a new project. How can we create an empty application (without Storyboard) in Xcode6 and above, like in earlier versions?

76462 次浏览

是的,或者只是使用以前的测试版来创建它,然后继续使用最新版本。

XCode6及以上版本中,没有像在 XCode5及以前版本中那样直接创建空应用程序的选项。但是我们仍然可以通过以下步骤创建一个没有 Storyboard的应用程序:

  1. 创建一个 Single View Application
  2. 删除 Main.storyboardLaunchScreen.xib(选择它们,右键单击,然后选择其中一个 从项目中删除它们,或者完全删除它们)。
  3. 删除“主故事板文件基本名称”和“启动屏幕界面” Info.plist文件中的“文件基名”条目。
  4. 打开 Appgenerate.m,编辑 applicationDidFinishLaunchingWithOptions,使其看起来像下面这样:

迅捷3及以上:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.white
self.window?.makeKeyAndVisible()
return true
}

斯威夫特2. x:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
self.window?.makeKeyAndVisible()
return true
}

目标 C:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.rootViewController = [[ViewController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

我正在使用 XCode6 Beta,我通过将 XCode5.x.x 中的空模板添加到 XCode6 Beta 中来寻找这个问题的另一个解决方案。

右键单击应用程序中的 XCode5.x.x,单击“显示包内容”,从给定路径复制“空的 Application.xctemplate”

内容/开发人员/平台/iPhoneOS.Platform/开发人员/库/Xcode/模板/项目模板/应用程序

现在退出 Window & open 为 XCode6指定的路径

内容/开发人员/平台/iPhoneOS.Platform/开发人员/库/Xcode/模板/项目模板/iOS/应用程序/

在应用程序文件夹中粘贴’空的 Application.xctemplate’。现在通过退出并创建新项目来重新启动 XCode6。您将得到’空申请’选项。

现在,当我创建新的 Empty 项目时,一个。Pch 文件自动添加到 project 中(我们必须在 XCode6中手动添加)

希望能成功

Akhils 的回答是完全正确的。对于我们这些使用 Swift 的人来说,应该是这样的:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
self.window?.makeKeyAndVisible()
return true
}

你还需要做以下几个步骤:

  1. 添加前缀文件(如果需要)
  2. 添加一个默认的启动图像,否则应用程序的大小将是320x480在 iPhone5。

下面是一个完整的教程:

  1. 删除 Main.Storyboard 文件
  2. 删除 LaunchScreen.xib 文件
  3. 删除 Info.plist 中的“ Main Storyboard file base name”属性
  4. 在 Info.plist 中删除“ Launch screen interface file base name”属性
  5. 添加“[ app name ]-Prefix.pch”文件到包含内容的支持文件:

    #import <Availability.h>
    
    
    #ifndef __IPHONE_3_0
    #warning "This project uses features only available in iOS SDK 3.0 and later."
    #endif
    
    
    #ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #endif
    
  6. add "$SRCROOT/$PROJECT_NAME/[pch file name]" to project settings -> Build Settings -> Apple LLVM 6.0 - Language -> "Prefix Header"

  7. add "YES" to to project settings -> Build Settings -> Apple LLVM 6.0 - Language -> "Precompile Prefix Header"
  8. open "Image.xcassets" and add LaunchImage
  9. build the project, then there will be a warning about missing default launch image, just press on the warning and select to add the default, this will add "Default-568h@2x" OR - If you want to use splash images from "Images.xcassets", go to the project settings -> TARGETS -> General -> in "Launch Images Source" choose to use asset catalog, it will create a new one, you can choose then, which to use as asset catalog from the existing.
  10. implement application:didFinishLaunchingWithOptions: method:

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    return YES;
    

你还需要做一件事:

1)在 plist 文件中删除主故事板文件的基本名称

//AppDelegate.h




@property (strong, nonatomic) UIViewController *viewController;
@property (strong, nonatomic) UINavigationController *nav;


//AppDelegate.m


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {`enter code here`
// Override point for customization after application launch.


CGRect screenBounds = [[UIScreen mainScreen] bounds];


UIWindow *window = [[UIWindow alloc] initWithFrame:screenBounds];




self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];






self.viewController = [[UIViewController alloc] initWithNibName:@"ViewController" bundle:nil];


self.nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];


[window setRootViewController:  self.nav];


[window makeKeyAndVisible];


[self setWindow:window];


return YES;
}

我有原始的空应用程序模板,这是在 Xcode 版本使用之前的 Xcode 6。我已经上传了 给你

您可以下载它并手动将其粘贴到 Xcode 模板目录中,也可以使用 Xcode 的包管理器 恶魔岛安装它。搜索 Xcode 空应用程序

删除 Main.Storyboard 文件

这可以简单地删除。

更新 ProjectName-Info. plist 文件

取出 Main storyboard base file name键。

创建一个 nib 文件并链接到项目的视图控制器

1. 创建一个 nib 文件(File-> New-> File-> View)

2. 将 File's Owner's类更新为任何项目视图 控制器调用

3. 将 File's Owner's view出口连接到笔记本文件中的 view对象

更新应用程序委托

1. 导入项目的视图控制器的头文件

2. 更新 application:didFinishLaunchingWithOptions:方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MyViewController *viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}

一个简单的方法是将 XCode 5Empty Application模板复制到 XCode的 template 目录。

您可以从 这里下载 XCode 5Empty Application模板,然后解压并复制到 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application目录。

附注: 这种方法也适用于迅速!

剪辑
正如@harrisg 在下面的评论中建议的那样,您可以将上述模板放在 ~/Library/Developer/Xcode/Templates/Project Templates/iOS/Application/文件夹中,这样即使 Xcode 得到更新,它也可以使用。
如果没有这样的目录,那么您可能必须在 ~/Library/Developer/Xcode/中创建这个目录结构: Templates/Project Templates/iOS/Application/

使用这个简单的方法,我能够在 XCode 6中创建一个 Empty Application

Xcode Empty Application Template 希望这个能帮上忙!

您可以为 Xcode 创建自己的项目模板:

Https://github.com/elprup/xcode7-project-template

对于 Xcode 8Swift 3 只要删除 .storyboard文件,它就会自动从您的 .plist中删除相应的引用,并在您的 AppDelegate.swift中添加以下代码。

    let initialViewController = UIViewController()
initialViewController.view.backgroundColor = .white
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = initialViewController
window?.makeKeyAndVisible()

您可以编写自己的定制 ViewController,并在 AppDelegate.swift中将其用作 self.window?.rootViewController,只需在上面的代码中将 UIViewController 替换为您自己的 ViewController 即可。

其他人已经解释了如何去掉故事板,所以我将在这里跳过这一点。这就是我在我的代码中使用较少可选链接的方式(用 Swift 3.1编写) :

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


let window = UIWindow(frame: UIScreen.main.bounds)
window.backgroundColor = .white
window.rootViewController = MyRootViewController()
window.makeKeyAndVisible()
self.window = window


return true
}

Xcode 9.3.1 和 < strong > Swift 4

  1. 首先你需要在 工程项目导航员菜单中删除 主要内容
  2. 然后删除 < em > Info.plist 中的 < em > 主故事板文件基本名称 行。
  3. 别忘了删除 工程项目目标-< em > 一般 -< em > 部署资料 中的单元格 主界面(只需删除 主要部分)。
  4. 在这些步骤之后,转到 < em > 应用代理迅捷 ,在函数 < em > did FinishLaunchingWithOptions 中写下:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    window?.rootViewController = UINavigationController(rootViewController: ViewController())
    
    
    return true
    }
    

Xcode 11.2.1 and Swift 5

  1. First of all you need delete Main.storyboard in Project navigator menu.
  2. Then delete row Main storyboard file base name in Info.plist.
  3. And don't forgot delete cell Main Interface (just remove Main) in your Project Target - General - Deployment Info.
  4. This step is important and it was not in previous versions of Xcode and Swift. In Info.plist go to: Application Scene ManifestScene ConfigurationApplication Session RoleItem 0 and delete here Storyboard.name row.
  5. After that steps go to SceneDelegate and in function scene write the next:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    
    window = UIWindow(frame: windowScene.coordinateSpace.bounds)
    window?.windowScene = windowScene
    window?.rootViewController = ViewController()
    window?.makeKeyAndVisible()
    }
    

更新: Swift 5和 iOS 13:

  1. 创建单视图应用程序。
  2. 删除 Main.Storyboard (右键单击并删除)。
  3. 从 Info.plist 文件中的默认场景配置中删除“ Storyboard Name”:enter image description here
  4. 打开 SceneDelegate.swift并将 func scene从:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
}

 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).x


if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = ViewController()
self.window = window
window.makeKeyAndVisible()
}
}

Xcode 11和 ios 13的更新。当你设置好一切但仍然看到一个黑屏时,这是因为生命周期是由 UISceneGenerate 处理的,当你创建一个新项目时,它会自动生成 UISceneRegiate.m 和 UISceneRegiate.h。让我们回到过去,在我们习惯 UISceneGenerate 之前。以下步骤可能有所帮助:

  1. 在 plist 中删除应用场景清单。

  2. 删除应用场景清单 h 和应用场景清单 m

  3. 在 APPregiate.m 中删除 # 杂注标记-UISceneSession 生命周期下的代码

  4. Add@property (strong,non-atom) UIWindow * 窗口; 在 APPregiate.h 中