Xcode6
在创建新项目时删除了Empty Application
模板。如何在Xcode6
及以上的版本中创建一个无Storyboard
的空应用程序,就像早期版本一样?
Xcode6
在创建新项目时删除了Empty Application
模板。如何在Xcode6
及以上的版本中创建一个无Storyboard
的空应用程序,就像早期版本一样?
XCode6
及以上版本没有直接创建空应用程序选项,如同 XCode5
和以前的版本。但是我们仍然可以通过以下步骤创建一个不带 Storyboard
的应用程序:
Single View Application
。Main.storyboard
和 LaunchScreen.xib
(选择它们,右键单击,并选择从项目中删除或完全删除)。Info.plist
文件中删除 "Main storyboard file base name" 和 "Launch screen interface file base name" 条目。Swift 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
}
Swift 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
}
Objective-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;
}
LaunchScreen.xib
文件和相应的 Info.plist
条目。没有它,应用在较大的iPhone上将不会占据整个屏幕大小,看起来很丑。 - tboyce12XCode 5
中的 Empty Application
模板复制到 XCode
的模板目录中。XCode 5
的 Empty Application
模板,然后解压并复制到 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application
目录。~/Library/Developer/Xcode/Templates/Project Templates/iOS/Application/
文件夹中,以便在 Xcode 更新后仍可用。
如果没有这个文件夹,则可能需要在 ~/Library/Developer/Xcode/
中创建以下目录结构:Templates/Project Templates/iOS/Application/
使用这种简单方法,我能够在 XCode 6
中创建一个 Empty Application
。(如下图所示)
还有几个步骤需要完成:
下面是完整的教程:
将“[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
将"$SRCROOT/$PROJECT_NAME/[pch文件名]"添加到项目设置-> Build Settings -> Apple LLVM 6.0 - Language -> "Prefix Header"
实现application:didFinishLaunchingWithOptions:
方法:
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;
Akhil的答案完全正确。对于我们使用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
}
Xcode 9.3.1和Swift 4
完成上述步骤后,打开AppDelegate.swift文件,在函数didFinishLaunchingWithOptions中编写以下内容:
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和Swift 5
完成上述步骤后,进入SceneDelegate文件,并在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 windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
}
更新:Swift 5 和 iOS 13:
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()
}
}
还有一步需要完成:
1)在plist文件中删除Main storyboard文件的基础名称
//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;
}
删除Main.storyboard文件
可以直接删除此文件。
更新ProjectName-Info.plist文件
删去Main storyboard base file name
键。
创建nib文件并将其链接到项目的视图控制器
1.创建一个nib文件(文件->新建->文件->视图)
2.将File's Owner
的类更新为项目的视图控制器名称
3.将File's Owner
的view
插座与nib文件中的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 8
和 Swift 3
。
只需删除.storyboard
文件,它会自动从您的.plist
中删除相应的引用,并在您的AppDelegate.swift
中添加以下代码。
let initialViewController = UIViewController()
initialViewController.view.backgroundColor = .white
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = initialViewController
window?.makeKeyAndVisible()
你可以编写自己的自定义ViewCountroller,并在AppDelegate.swift
中使用它作为self.window?.rootViewController
,只需在上面的代码中将UIViewController替换为你自己的ViewController即可。