Xcode6
Empty Application
在创建新项目时已删除模板。Xcode6
与早期版本一样,我们如何在上方和上方创建一个空的应用程序(没有Storyboard)?
Xcode6
Empty Application
在创建新项目时已删除模板。Xcode6
与早期版本一样,我们如何在上方和上方创建一个空的应用程序(没有Storyboard)?
Answers:
在XCode6
更高版本中,没有任何选项可以像在XCode5
早期版本中那样直接创建空应用程序。但是我们仍然可以Storyboard
通过以下步骤创建应用程序:
Single View Application
。Main.storyboard
并LaunchScreen.xib
(选择它们,单击鼠标右键,然后选择将其从项目中删除,或完全删除)。Info.plist
。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
}
目标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上占用整个屏幕尺寸,这很丑陋。
一种简单的方法是将XCode 5
的Empty Application
模板复制到XCode
的模板目录。
您可以从此处下载XCode 5
的Empty 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更新也可以使用。
如果没有这样的目录存在,那么你可能需要创建此目录结构:Templates/Project Templates/iOS/Application/
在~/Library/Developer/Xcode/
使用这种简单的方法,我可以创建一个Empty Application
in XCode 6
。(下面的截图)
希望这可以帮助!
您还需要执行几个步骤:
所以这是一个完整的教程:
将“ [应用名称] -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文件名]”添加到项目设置->构建设置-> Apple LLVM 6.0-语言->“前缀标题”
实现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;
阿基尔斯的答案是完全正确的。对于使用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并在函数场景中编写下一个:
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()
}
您还需要执行一个步骤:
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;
}
更新: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()
}
}
我有原始的Empty Application模板,该模板在Xcode 6之前的Xcode版本中使用过。我已经在这里上传了它。
您可以下载它并手动将其粘贴到Xcode模板目录中,也可以使用Xcode的软件包管理器Alcatraz进行安装。只需搜索Xcode空应用程序。
删除Main.storyboard文件
可以简单地将其删除。
更新ProjectName-Info.plist文件
拔下Main storyboard base file name
钥匙。
创建一个nib文件并链接到项目的视图控制器
1.创建一个笔尖文件(文件–>新建–>文件–>视图)
2,将File's Owner's
类更新为项目的视图控制器
3.将File's Owner's
view
插座链接到view
nib文件中的对象
更新应用程序委托
1.导入项目的视图控制器的头文件
2.更新申请: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。
我正在使用XCode6 Beta,我通过从XCode5.xx向XCode6 Beta添加Empty模板来搜索此问题的另一种解决方案。
为此,右键单击“应用程序”中的XCode5.xx,单击“显示包内容”,然后从给定路径复制“ Empty Application.xctemplate”
内容/开发人员/平台/iPhoneOS.platform/开发人员/库/ Xcode /模板/项目模板/应用程序
现在退出窗口并打开XCode6的给定路径
内容/开发人员/平台/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates / iOS / Application /
将“ Empty Application.xctemplate”粘贴到Application文件夹中。现在,通过退出并创建新项目来重新启动XCode6。您将获得“空应用程序”选项。
现在,当我创建新的Empty项目时,将在项目中自动添加一个.pch文件(我们必须在XCode6中手动添加)
希望它能工作
您可以为Xcode创建自己的项目模板。根据您的要求,您可以在此网站上使用模板:
其他人已经解释了如何摆脱情节提要,所以我将在这里跳过。这就是我更喜欢在代码中使用较少的可选链接(用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 11和ios 13上进行了更新。设置完所有内容但仍然看到黑屏后,这是因为生命周期由UISceneDelegate处理,并且在创建新项目时,它将自动生成UISceneDelegate.m和UISceneDelegate.h。回到以前,我们习惯了UISceneDelegate。以下步骤可能会有所帮助:
在plist中删除Application Scene Manifest。
删除应用程序场景Manifest.h和应用程序场景Manifest.m
删除#pragma标记下的代码-APPdelegate.m中的UISceneSession生命周期
添加@property(强的,非原子的)UIWindow *窗口;在APPdelegate.h中