1. 创建App:Single View APP->Product Name:Lottery;后缀:Class Prefix:HM

2. Lottery目录下新建3个文件夹:Classes、Comman、Other

3. Classes目录下新建5个文件夹,并在每个文件夹下再新建4个文件夹:

  Arena(竞技场) :          OtherModelViewController            

  Discovery(发现):        Other、Model、View、Controller

  Hall(购彩大厅):           Other、Model、View、Controller

  History(开奖信息):      Other、Model、View、Controller   

  MyLottery(我的彩票):  Other、Model、View、Controller

  Main(主框架):            Other、Model、View、Controller

4. 将“AppDelegate.h”和“AppDelegate.m”移到“Other”目录下

5. 删除项目目录文件:“viewController.h”、“viewController.m”、“Main.storyboard”->Move to Trash

6. 设置禁用系统自带的“Main”:General->Deployment Info->Main Interface:清空

7. 禁止横屏:General->Deployment Info->Device Orientation->Landscape Left:勾勾去掉Landscape Right:勾勾去掉

8. 应用图标设置:将素材图片拖进Assets.xcassets->appIcon

9. 应用启动图片设置:

  9.1 General->App Icons and Launch Images->Launch Images Source:Assets->MigrateLaunch Screen File:清空

  9.2 将素材图片拖进Assets.xcassets->LaunchImage

10. 其它素材图片拖进Assets.xcassets

11. 新建Pch文件:Lottery->Other->Pch file->Save As:PrefixHeader.pch

  11.1 设置路径:Build Settings搜索header->Apple LLVM 9.0 - Language->Prefix Header:Lottery/Other/PrefixHeader.pchPreficHeader.pch路径

  11.2 在“PrefixHeader.pch”文件对屏幕尺寸进行宏定义,代码如下:

#define kScreenSize [UIScreen mainScreen].bounds    //屏尺寸
#define kScreenWidth kScreenSize.size.width         //屏宽
#define kScreenHeight kScreenSize.size.height       //屏高
View Code

 12. 创建子控制器“storyboard”文件

  12.1 子控制器目录->View->Storyboard->子控制器目录名字.storyboard->拖进Navigation Controller->勾选is Initial View Controller

  12.2 删除子控制器,再添加需要的控制器View Controller或Table View Controller

  12.3 按住键盘control键,从Navigation Controller拖线至子控制器,选择root view controller

13. 加载子视图控制器

  13.1 新建UITabBarController:Main(主框架)->Controller->Cocoa Touch Class->Class:HMTabBarController;Subclass of:UITabBarController;

  13.2 HMTabBarController.m”文件中创建方法,根据SB名称加载子控制器,代码如下:

//根据 sb 名称加载子视图控制器
- (UIViewController *)loadSubViewControllerWithSBName:(NSString *)name {
    //获取 sb 对象
    UIStoryboard *sb = [UIStoryboard storyboardWithName:name bundle:nil];
    return [sb instantiateInitialViewController];
}
View Code

  13.3 在“HMTabBarController.m”文件的“viewDidLoad”方法中获取5个子控制器,并设置为tabbarController子控制器,代码如下:

    //获取5个子控制器
    UIViewController *v1 = [self loadSubViewControllerWithSBName:@"Hall"];
    UIViewController *v2 = [self loadSubViewControllerWithSBName:@"Arena"];
    UIViewController *v3 = [self loadSubViewControllerWithSBName:@"Discovery"];
    UIViewController *v4 = [self loadSubViewControllerWithSBName:@"History"];
    UIViewController *v5 = [self loadSubViewControllerWithSBName:@"MyLottery"];
    
    //设置 tabbarController 子控制器
    self.viewControllers = @[v1,v2,v3,v4,v5];
View Code

  13.4 显示window和5个控制器:“Other”目录下“AppDelegate.m”文件的“application方法中创建window和tabbarController代码如下:

    //创建 window
    self.window = [[UIWindow alloc] initWithFrame:kScreenSize];
    
    //创建 tabbarController
    HMTabBarController *tabbarController = [[HMTabBarController alloc] init];
    
    //设置 window 根控制器
    self.window.rootViewController = tabbarController;
    
    //显示 window
    [self.window makeKeyAndVisible];
View Code

14. 自定义标签栏按钮

  14.1 新建UIViewMain(主框架)->View->Cocoa Touch Class->Class:HMTabBar;Subclass of:UIView

  14.2 在HMTabBar.h”文件中申明按钮常量按钮添加图片的方法,代码如下:

@property (nonatomic, weak) UIButton *currentButton;

//按钮添加图片的方法
- (void)addButtonWithImage:(UIImage *)image andImageSel:(UIImage *)imageSel;
View Code

  14.3 HMTabBar.m”文件中自定义按钮,去掉高亮状态,代码如下:

@interface HMTabBarButton : UIButton

@end

@implementation HMTabBarButton

- (void)setHighlighted:(BOOL)highlighted {}

@end
View Code

  14.4 在“HMTabBar.m”文件中实现按钮布局,代码如下:

//btn 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    
    for (int i = 0; i < self.subviews.count; i++) {
        //获取 btn
        UIButton *tabbarButton = self.subviews[i];
        
        //设置 tag 切换控制器
        tabbarButton.tag = i;
        
        //计算 btn 的 frame
        CGFloat w = kScreenWidth / 5;
        CGFloat h = 49;
        CGFloat x = i * w;
        CGFloat y = 0;
        
        tabbarButton.frame = CGRectMake(x, y, w, h);
        
        //应用启动默认为第1个按钮
        if (i == 0) {
            [self tabBarButtonClick:tabbarButton];
        }
    }
}
View Code

  14.5 在“HMTabBar.m”文件中实现按钮添加图片方法,代码如下:

//添加 btn 图片
- (void)addButtonWithImage:(UIImage *)image andImageSel:(UIImage *)imageSel {
    //创建 btn
    HMTabBarButton *tabbarButton = [[HMTabBarButton alloc] init];
    
    //设置 btn 图片
    [tabbarButton setBackgroundImage:image forState:UIControlStateNormal];
    [tabbarButton setBackgroundImage:imageSel forState:UIControlStateSelected];
    
    //监听 btn
    [tabbarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchDown];
    
    //添加到自己上
    [self addSubview:tabbarButton];
}
View Code

  14.6 在“HMTabBar.m”文件中实现按钮点击事件,代码如下:

//点击 btn 时调用
- (void)tabBarButtonClick:(UIButton *)sender {

    //取消 btn 选中状态
    self.currentButton.selected = NO;
    
    //设置点击 btn 为选中状态
    sender.selected = YES;
    
    //记录选中的 btn
    self.currentButton = sender;
}
View Code

 15. 添加标签栏按钮:在“HMTabBarController.m”文件的“viewDidLoad”方法中创建5个标签栏按钮,代码如下:

    //创建1个自定义的 tabbar
    HMTabBar *tabbar = [[HMTabBar alloc] init];
    
    //设置 tabbar 的 frame 为系统的 frame
    tabbar.frame = self.tabBar.frame;
    
    for (int i = 0; i < self.viewControllers.count; i++) {
        
        //获取 btn 图片
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"TabBar%d",i + 1]];
        UIImage *imagePress = [UIImage imageNamed:[NSString stringWithFormat:@"TabBar%dSel",i + 1]];
        
        [tabbar addButtonWithImage:image andImageSel:imagePress];
    }
    
    //添加到 tabbarController 中
    [self.view addSubview:tabbar];
View Code

 16. 使用block切换控制器

  16.1 在“HMTabBar.h”文件中新建block,代码如下:

//1. 写 block 属性     (相当于代理的协议,协议方法,id属性)
@property (nonatomic, copy) void(^tabbarButtonBlock)(NSInteger);
View Code

  16.2 在“HMTabBar.m”文件中tabBarButtonClick”方法中按钮点击时执行block,代码如下:

    //判断 block 是否有值     (相当于代理方法是否有响应)
    if (self.tabbarButtonBlock) {
        
        //2. 执行 block       (相当于执行代理方法)
        self.tabbarButtonBlock(sender.tag);
    }
View Code

  16.3 在“HMTabBarController.m”文件的“viewDidLoad”方法中使用block,代码如下:

    //3. 使用 block       (相当于遵守协议,使用代理方法)
    __weak HMTabBarController *weakSelf = self;
    tabbar.tabbarButtonBlock = ^(NSInteger index) {
        weakSelf.selectedIndex = index;
    };
View Code

17. 导航栏设置

  17.1 新建UINavigationControllerMain(主框架)->Controller->Cocoa Touch Class->Class:HMNavigationController;Subclass of:UINavigationController

  17.2 设置5个控制器的“storyboard”文件的类为“HMNavigationController”:Custom Class->Class:HMNavigationController

  17.3 在“HMNavigationController.m”文件的“viewDidLoad”方法中设置导航栏图片文字为白色,代码如下:

    //设置导航栏图片
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
    
    //设置文字为白色
    [self.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }];
View Code