ios 如何在没有 Storyboard 的情况下在 Xcode 中创建一个空应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25783282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an Empty Application in Xcode without Storyboard
提问by Akhil K C
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?
Xcode6
Empty Application
创建新项目时删除了模板。我们如何创建一个空应用程序(没有 Storyboard)Xcode6
,就像在早期版本中一样?
回答by Akhil K C
There is no option in XCode6
and above versions for directly creating an Empty Application as in XCode5
and earlier. But still we can create an application without Storyboard
by following these steps:
在XCode6
和之前的版本中没有直接创建空应用程序的选项XCode5
。但是我们仍然可以在不执行Storyboard
以下步骤的情况下创建应用程序:
- Create a
Single View Application
. - Remove
Main.storyboard
andLaunchScreen.xib
(select them, right-click, and choose to either remove them from the project, or delete them completely). - Remove "Main storyboard file base name" and "Launch screen interface
file base name" entries in
Info.plist
file. - Open AppDelegate.m, and edit applicationDidFinishLaunchingWithOptions so that it looks like this:
- 创建一个
Single View Application
. - 删除
Main.storyboard
和LaunchScreen.xib
(选择它们,右键单击,然后选择从项目中删除它们,或完全删除它们)。 - 删除文件中的“主故事板文件基本名称”和“启动屏幕界面文件基本名称”条目
Info.plist
。 - 打开 AppDelegate.m,然后编辑 applicationDidFinishLaunchingWithOptions 使其看起来像这样:
Swift 3 and above:
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:
斯威夫特 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:
目标-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;
}
回答by S1LENT WARRIOR
A simple approach would be to copy the XCode 5
's Empty Application
template to XCode
's templates directory.
一种简单的方法是将XCode 5
的Empty Application
模板复制到XCode
的模板目录。
You can download XCode 5
's Empty Application
template from here, then unzip it and copy to /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application
directory.
您可以从这里下载XCode 5
的Empty Application
模板,然后解压缩并复制到目录。 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application
P.S this approach works with swift as well!
PS 这种方法也适用于 swift !
Edit
As suggested by @harrisg in a comment below, You can place the above mentioned template in ~/Library/Developer/Xcode/Templates/Project Templates/iOS/Application/
folder so that it may be available even if Xcode gets updated.
And if there is no such directory present, then you might have to create this directory structure: Templates/Project Templates/iOS/Application/
in ~/Library/Developer/Xcode/
编辑
正如@harrisg 在下面的评论中所建议的,您可以将上述模板放在~/Library/Developer/Xcode/Templates/Project Templates/iOS/Application/
文件夹中,以便即使 Xcode 更新它也可能可用。
如果没有这样的目录存在,那么你可能需要创建此目录结构:Templates/Project Templates/iOS/Application/
在~/Library/Developer/Xcode/
Using this simple approach i'm able to create an Empty Application
in XCode 6
. (Screenshot attached below)
使用这种简单的方法,我可以创建一个Empty Application
in XCode 6
. (下面附截图)
Hope this helps!
希望这可以帮助!
回答by Nikita
There are a few more steps that you need to do:
您还需要执行以下几个步骤:
- To add a prefix file. (If you need it)
- To add a default launch image, otherwise the app size will be 320x480 on iPhone 5.
- 添加前缀文件。(如果你需要的话)
- 要添加默认启动图像,否则 iPhone 5 上的应用程序大小将为 320x480。
So here is a full tutorial:
所以这是一个完整的教程:
- remove Main.storyboard file
- remove LaunchScreen.xib file
- remove "Main storyboard file base name" property in Info.plist
- remove "Launch screen interface file base name" property in Info.plist
add "[app name]-Prefix.pch" file to supporting files with contents:
#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
add "$SRCROOT/$PROJECT_NAME/[pch file name]" to project settings -> Build Settings -> Apple LLVM 6.0 - Language -> "Prefix Header"
- add "YES" to to project settings -> Build Settings -> Apple LLVM 6.0 - Language -> "Precompile Prefix Header"
- open "Image.xcassets" and add LaunchImage
- 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.
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;
- 删除 Main.storyboard 文件
- 删除 LaunchScreen.xib 文件
- 删除 Info.plist 中的“主故事板文件基本名称”属性
- 删除 Info.plist 中的“启动屏幕界面文件基本名称”属性
将“[应用程序名称]-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 - 语言 -> “前缀标题”
- 将“YES”添加到项目设置 -> 构建设置 -> Apple LLVM 6.0 - 语言 ->“预编译前缀头”
- 打开“Image.xcassets”并添加 LaunchImage
- 构建项目,然后会有一个关于缺少默认启动图像的警告,只需按下警告并选择添加默认值,这将添加“Default-568h@2x”或 - 如果您想使用“图像”中的启动图像.xcassets”,进入项目设置 -> TARGETS -> General -> 在“Launch Images Source”中选择使用资产目录,它会创建一个新的,你可以选择,作为资产目录从现有的.
实现
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;
回答by Nils Hott
Akhils answer is totally correct. For those of us using Swift, it would be like this:
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
}
回答by iAleksandr
Xcode 9.3.1and Swift 4
Xcode 9.3.1和Swift 4
- First of all you need delete Main.storyboardin Project navigatormenu.
- Then delete row Main storyboard file base namein Info.plist.
- And don't forgot delete cell Main Interface(just remove Main) in your Project Target- General- Deployment Info.
After that steps go to AppDelegate.swiftand in function didFinishLaunchingWithOptionswrite the next:
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 }
- 首先,您需要在项目导航器菜单中删除Main.storyboard。
- 然后在Info.plist 中删除行Main storyboard file base name。
- 并且不要忘记在Project Target- General- Deployment Info 中删除单元格Main Interface(只需删除Main) 。
在这些步骤之后转到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.1and Swift 5
Xcode 11.2.1和Swift 5
- First of all you need delete Main.storyboardin Project navigatormenu.
- Then delete row Main storyboard file base namein Info.plist.
- And don't forgot delete cell Main Interface(just remove Main) in your Project Target - General - Deployment Info.
- This step is important and it was not in previous versions of Xcode and Swift. In Info.plistgo to: Application Scene Manifest→ Scene Configuration→ Application Session Role→ Item 0and delete here Storyboard.namerow.
After that steps go to SceneDelegateand in function scenewrite 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() }
- 首先,您需要在项目导航器菜单中删除Main.storyboard。
- 然后在Info.plist 中删除行Main storyboard file base name。
- 并且不要忘记在Project Target - General - Deployment Info 中删除单元格Main Interface(只需删除Main)。
- 这一步很重要,在以前的 Xcode 和 Swift 版本中没有。在Info.plist 中转到:Application Scene Manifest→ Scene Configuration→ Application Session Role→ Item 0并在此处删除Storyboard.name行。
在这些步骤之后转到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() }
回答by Bhushan_pawar
There is one more step that you need to do:
您还需要执行一个步骤:
1)remove Main storyboard file base name in plist file
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;
}
回答by Eric Wiener
Update: Swift 5 and iOS 13:
更新:Swift 5 和 iOS 13:
- Create a Single View Application.
- Delete Main.storyboard (right-click and delete).
- Delete "Storyboard Name" from the default scene configuration in the Info.plist file:
- Open
SceneDelegate.swift
and changefunc scene
from:
- 创建单一视图应用程序。
- 删除 Main.storyboard(右键单击并删除)。
- 从 Info.plist 文件中的默认场景配置中删除“Storyboard Name”:
- 打开
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 }
}
to
到
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()
}
}
回答by Isuru
I have the original Empty Application template which was used in Xcode versions prior to Xcode 6. I have uploaded it here.
我有在 Xcode 6 之前的 Xcode 版本中使用的原始 Empty Application 模板。我已将其上传到此处。
You can either download it and paste it in the Xcode template directory manually or install it using the package manager for Xcode, Alcatraz. Just search for Xcode Empty Application.
您可以下载它并手动将其粘贴到 Xcode 模板目录中,也可以使用 Xcode 的包管理器Alcatraz安装它。只需搜索Xcode Empty Application。
回答by Bhargav
Remove the Main.storyboard file
删除 Main.storyboard 文件
This can simply be deleted.
这可以简单地删除。
Update the ProjectName-Info.plist file
更新 ProjectName-Info.plist 文件
Remove the Main storyboard base file name
key.
取下Main storyboard base file name
钥匙。
Create a nib file and link to the project's view controller
创建一个 nib 文件并链接到项目的视图控制器
1.Create a nib file (File –> New –> File –> View)
1.创建一个nib文件(文件->新建->文件->查看)
2.Update the File's Owner's
class to whatever the project's view
controller is called
2.将File's Owner's
类更新为任何项目的视图控制器被调用
3.Link the File's Owner's
view
outlet to the view
object in the nib file
3.将File's Owner's
view
outlet链接到view
nib文件中的对象
Update the app delegate
更新应用委托
1.Import the project's view controller's header file
1.导入项目的视图控制器的头文件
2.Update the application:didFinishLaunchingWithOptions:
method:
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;
}
回答by Vakas
For Xcode 8
and Swift 3
.Just delete the .storyboard
file, it will automatically delete the corresponding reference from your .plist
and in your AppDelegate.swift
add the following code.
对于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()
let initialViewController = UIViewController() initialViewController.view.backgroundColor = .white window = UIWindow(frame: UIScreen.main.bounds) window?.rootViewController = initialViewController window?.makeKeyAndVisible()
You can write your own custom ViewCountroller and use in that in AppDelegate.swift
as your self.window?.rootViewController
, just replace the UIViewController with your own ViewController in the above code.
您可以编写自己的自定义 ViewCountroller 并将其AppDelegate.swift
用作您的self.window?.rootViewController
,只需在上面的代码中将 UIViewController 替换为您自己的 ViewController 即可。