FIRST一:多視圖控制

創建多個視圖控制器

創建多個視圖控制器,並將主視圖控制器設爲windows的根視圖。

#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


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

在這裏插入圖片描述

懶加載初始化子控制器

1.將視圖控制器設置爲主視圖控制器的子控制器。
2.將子控制器的視圖添加到主控制器中
在這裏插入圖片描述

自定義tabBar

自定義TabBar,繼承自UIView;自定義tabBarItem,繼承自UIButton。
CustomTabBarItem.h

//
//  CustomTabBarItem.h
//  FIRST
//
//  Created by 謝鑫 on 2019/9/22.
//  Copyright © 2019 Shae. All rights reserved.
// 自定義CustomTabBarItem,繼承自UIButton

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface CustomTabBarItem : UIButton
//帶尺寸和題目的初始化h方法
+(instancetype)itemWithFrame:(CGRect)frame title:(NSString*)title;
@end

NS_ASSUME_NONNULL_END

CustomTabBarItem.m

//
//  CustomTabBarItem.m
//  FIRST
//
//  Created by 謝鑫 on 2019/9/22.
//  Copyright © 2019 Shae. All rights reserved.
//

#import "CustomTabBarItem.h"

@implementation CustomTabBarItem
//實現初始化方法
+(instancetype)itemWithFrame:(CGRect)frame title:(NSString *)title{
    CustomTabBarItem *item=[self buttonWithType:UIButtonTypeCustom]; //按鈕的初始化方法 buttonWithType
    item.frame=frame;
    
    [item setTitle:title forState:UIControlStateNormal];//按鈕的屬性設置 forState
    [item setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [item.titleLabel setFont:[UIFont systemFontOfSize:17]];
    
    [item setBackgroundImage:[UIImage imageNamed:@"normal"] forState:UIControlStateNormal];
    [item setBackgroundImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
    return item;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

CustomTabBar.h

//
//  CustomTabBar.h
//  FIRST
//
//  Created by 謝鑫 on 2019/9/22.
//  Copyright © 2019 Shae. All rights reserved.
//自定義TabBar,繼承自UIView

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface CustomTabBar : UIView
+(instancetype)tabBarWithTitles:(NSArray*)titles;//帶titls的初始化方法
@end

NS_ASSUME_NONNULL_END

CustomTabBar.m

//
//  CustomTabBar.m
//  FIRST
//
//  Created by 謝鑫 on 2019/9/22.
//  Copyright © 2019 Shae. All rights reserved.
//

#import "CustomTabBar.h"
#import "CustomTabBarItem.h"
static const CGFloat TabBarHeight=44;
//匿名類別
@interface CustomTabBar ()
@property (nonatomic,strong)NSArray *mTitles;//用於接受傳入的數組titles
@property (nonatomic,weak)CustomTabBarItem *preSelectedItem;//上一個選中的CustomTabBarItem
-(void)addItems;//添加CustomTabBarItem的方法
@end

@implementation CustomTabBar
//實現初始化方法
+ (instancetype)tabBarWithTitles:(NSArray *)titles{
    CustomTabBar *tabBar=[[CustomTabBar alloc]init];
    [tabBar setBackgroundColor:[UIColor lightGrayColor]];
    tabBar.mTitles=titles;//給mTitles賦值titles
    return tabBar;
}
//添加CustomTabBarItem的具體實現方法
- (void)addItems{
    CGFloat itemCount=self.mTitles.count;
    CGFloat itemWidth=self.frame.size.width/itemCount;
    CGFloat itemHight=self.frame.size.height;
    for (int i=0; i<itemCount; i++) {
        CustomTabBarItem *item=[CustomTabBarItem itemWithFrame:CGRectMake(itemWidth*i, 0, itemWidth, itemHight) title:self.mTitles[i]];
        [self addSubview:item];
        [item addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
        [item setTag:i+1];//tag從1開始
    }
}
-(void)itemSelected:(CustomTabBarItem *)sender{
    NSLog(@"%ld",(long)sender.tag);
    
}
//將要加入父視圖
- (void)willMoveToSuperview:(UIView *)newSuperview{
    self.frame=CGRectMake(0, newSuperview.frame.size.height-TabBarHeight,newSuperview.frame.size.width , TabBarHeight);
    [self addItems];
    //默認的item
  
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

調試

CustomTabBar *test=[CustomTabBar tabBarWithTitles:@[@"1",@"2",@"3"]];
    [self.view addSubview:test];

在這裏插入圖片描述

自定義的tabBar控制切換viewController

按鈕響應方法:

-(void)itemSelected:(CustomTabBarItem *)sender{
    //如果選擇的和上一次相同就直接返回
    if(sender==self.preSelectedItem){
        return;
    }
    //設置之前的item不選擇
    [sender setSelected:NO];
    //設置當前sender已經選擇
    [sender setSelected:YES];
    //調用協議方法,通過代理方法和MainViewController通信,控制viewController的切換
    [self.delegate tabBar:self itemSelectedAtIndex:sender.tag];
    
    NSLog(@"%ld",(long)sender.tag);
    self.preSelectedItem=sender;
}
//代理方法的具體實現,如果不實現這個協議方法,會崩潰
-(void)tabBar:(CustomTabBar *)tabBar itemSelectedAtIndex:(NSInteger)index{
    NSLog(@"%ld test",index);
    //bringSubviewToFront將對應子視圖控制器的view顯示
    switch (index) {
        case 1:
            [self.view bringSubviewToFront:self.aViewController.view];
            break;
        case 2:
            [self.view bringSubviewToFront:self.bViewController.view];
            break;
        case 3:
            [self.view bringSubviewToFront:self.cViewController.view];
            break;
            
        default:
            break;
    }
    //最後都要把自定義的tabBar添加
    [self.view bringSubviewToFront:_customTabBar];
}

代碼

代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章