如何在 Xcode 9 上建立一个 iOS 的 Empty Application
in 技术日志 with 0 comment and 3388 read

如何在 Xcode 9 上建立一个 iOS 的 Empty Application

in 技术日志 and 3389 read

自从 Xcode 6 以来,系统就不再支持建立 Empty App 了,我们没有办法,拿着钱包抵工资,只能自己进行一些简单的操作了。

第一步,建立一个 Single View App

SVA

第二步,删掉 Storyboard

LaunchScreen.storyboard 和 Main.storyboard 都删除到垃圾桶。

删除

第三步,在 Info.plist 页面删掉两条数据

Main storyboard 和 Launch screen 两条。点击数据后面的“-”号,即可。

删除

第四步,在 App Icons and Launch Images 页面

点击 Use Asset Catalog... 按钮。

UAC

然后在对话框中点击 Migrate 按钮。

migrate

第五步,修改 AppDelegate.m 文件

在应用委托实现文件的开头,添加如下引用。

#import "ViewController.h"

修改 didFinishLaunchingWithOptions: 方法,在 return YES; 上方添加如下代码。

// 创建 Window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 设置根视图控制器
self.window.rootViewController = [[ViewController alloc] init];
// 设置窗口背景颜色
self.window.backgroundColor = [UIColor whiteColor];
// 使窗口可见
[self.window makeKeyAndVisible];

第六步,修改 ViewController.m 文件

在视图控制器实现文件的开头,添加如下引用。

#import "AppDelegate.h"

然后手撸一个 loadView 方法。

在这个方法中,可以进行视图的初始化了。下面放一个简单的例子。

- (void)loadView {
    
    [super loadView];
    
    CGRect winFrame = [[UIScreen mainScreen] bounds];
    
    CGRect tableFrame = CGRectMake(0, 85, winFrame.size.width, winFrame.size.height - 100);
    CGRect fieldFrame = CGRectMake(30, 40, 250, 41);
    CGRect buttonFrame = CGRectMake(288, 40, 72, 41);
    
    self.taskTable = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    self.taskTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.taskTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    
    self.taskField = [[UITextField alloc] initWithFrame:fieldFrame];
    self.taskField.borderStyle = UITextBorderStyleRoundedRect;
    self.taskField.placeholder = @"Type a task, tap Insert";
    
    self.insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.insertButton.frame = buttonFrame;
    [self.insertButton setTitle:@"Insert" forState:UIControlStateNormal];
    
    // 给button添加 目标动作对
    [self.insertButton addTarget:self action:@selector(addTask:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.taskTable];
    [self.view addSubview:self.taskField];
    [self.view addSubview:self.insertButton];
    
}

实现效果预览如下图。

效果

Responses