iOS13移除StoryBoard

iOS13移除StoryBoard

当我们想要使用纯代码构建时,不需要storyboard时如何删除呢?

Objective-C

  1. 删除Main.storyboard文件(SceneDelegate.h和SceneDelegate.m文件可删可不删)
  2. 删除Info.plist文件Main storyboard file base name项和Application Scene Manifest项
  3. 在AppDelegate.h添加以下代码
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
  1. 在AppDelegate.m添加如下代码
#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


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


@end
  1. 此时运行,发现屏幕是黑色的。需要设置ViewController的view的背景颜色为白色或者其他颜色,试图控制器的view背景色默认是黑色。

Swift

  1. 删除Main.storyboard文件
  2. 删除Info.plist文件Main storyboard file base name项
  3. 在AppDelegate.h添加以下代码
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

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

}
  1. 同样此时运行,发现屏幕是黑色的。需要设置ViewController的view的背景颜色为白色或者其他颜色,试图控制器的view背景色默认是黑色。

这样我们就完成了删除storyboard的任务,可以在AppDelegate文件自由的配置UITabBarController/UINavigationcontroller等。

发布了75 篇原创文章 · 获赞 28 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/WxqHUT/article/details/104502699