代码改变世界

iOS 删除默认主页Main.storyboard及跳转到指定自定义主页

2021-03-08 21:10  zhaosn  阅读(436)  评论(0编辑  收藏  举报

环境:Xcode V12.4 及 MacOS V11.2.1

一、删除默认主页Main.storyboard

 

1、删除Main.storyboard文件

2、选中项目----General ---- Deployment Info —— 清空Main Interface的Main,此时会自动删除Info.plist中的

<key>UIMainStoryboardFile</key>
    <string>Main</string>

如果没删除,检查后删除。

 

3、也可以删除或不删除自动生成的ViewController文件,这里选择删除,选中,右键 Delete , Move to Trash

此时在真机运行会先显示白色(因为有启动页),后显示黑色(无主页)

 

4、指定General ---- Deployment Info 的iOS 13.0

IPHONEOS_DEPLOYMENT_TARGET = 13.0;

 

5、APP只适配iPhone,不适配iPad

原来的 TARGETED_DEVICE_FAMILY = "1,2";

会变为 TARGETED_DEVICE_FAMILY = 1;

 

*************************************************

二、自定义首页及跳转

 

1、New file —— Cocoa Touch Class,创建两个UIViewController,一个选中Also create XIB file,另一个不选中

 

FirstViewController不选Also create XIB file

SecondViewController选中Also create XIB file

 

2、在AppDelegate.h中添加属性window

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;

@end

 

3、在AppDelegate.m的didFinishLaunchingWithOptions中设置window的基本信息,并设置根UIViewController,设置完成后运行即跳转到对应页面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //给出默认背景颜色,如果此处不给背景颜色,其他页面巧合也没给背景颜色,则进入到页面中时显示黑色
    self.window.backgroundColor = [UIColor whiteColor];
    
    //不带xib的UIViewController
    //FirstViewController *vc = [[FirstViewController alloc] init];
    
    //带xib的UIViewController
    SecondViewController *vcs = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vcs];
    
    self.window.rootViewController = navi;
    [self.window makeKeyAndVisible];
    
    return YES;
}