溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

UIkit框架(14)自定義標(biāo)簽控制器

發(fā)布時(shí)間:2020-06-18 05:31:05 來(lái)源:網(wǎng)絡(luò) 閱讀:648 作者:ymanmeng123 欄目:移動(dòng)開(kāi)發(fā)
  • 自定義UITabBarController子類

UITabBarController的tabBar往往不能滿足我們的需求

通過(guò)自定義UITabBarController子類自定義標(biāo)簽欄是經(jīng)常采用的方式。

基本步驟:

    1)定義UIButton子類作為標(biāo)簽按鈕

    2)定義模型類,管理每個(gè)頁(yè)面的控制器以及對(duì)應(yīng)標(biāo)簽欄上的數(shù)據(jù)

    3)自定義標(biāo)簽欄

    4)定義UITabBarController子類



  • 定義標(biāo)簽按鈕

添加兩種創(chuàng)建方法:只顯示圖片和文字圖片都顯示的

+ (AMTabBarButton *)tabBarButtonWithTitle:(NSString *)title normalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage
{
    AMTabBarButton * btn = [AMTabBarButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:normalImage forState:UIControlStateNormal];
    [btn setImage:selectedImage forState:UIControlStateSelected];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    btn.titleLabel.font = [UIFont systemFontOfSize:10];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    return btn;
}
+ (AMTabBarButton *)tabBarButtonWithNormalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage
{
    AMTabBarButton * btn = [AMTabBarButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:normalImage forState:UIControlStateNormal];
    [btn setImage:selectedImage forState:UIControlStateSelected];
    [btn setTitle:nil forState:UIControlStateNormal];
    return btn;
}

修改按鈕內(nèi)部label和p_w_picpathView的位置,重寫以下方法即可

- (CGRect)titleRectForContentRect:(CGRect)contentRect
- (CGRect)p_w_picpathRectForContentRect:(CGRect)contentRect

如:

- (CGRect)p_w_picpathRectForContentRect:(CGRect)contentRect
{
    CGFloat x, y, w, h;
  
    if ( [self titleForState:UIControlStateNormal] == nil ) {
        w = h = contentRect.size.height*0.8;
    }
    else {
        w = h = contentRect.size.height*0.45;
    }
    x = contentRect.size.width/2 - w/2;
    y = contentRect.size.height*0.1;
    return CGRectMake(x, y, w, h);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat x, y, w, h;
    if ( [self titleForState:UIControlStateNormal] == nil ) {
        return CGRectZero;
    }
    h = contentRect.size.height*0.25;
    w = contentRect.size.width*0.6;
    x = contentRect.size.width*0.2;
    y = contentRect.size.height*0.65;
    return CGRectMake(x, y, w, h);
}

取消按鈕點(diǎn)擊時(shí)的高亮效果:重寫highlighted屬性的setter方法

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


  • 定義模型類

管理每個(gè)子控制器及標(biāo)簽欄上按鈕的數(shù)據(jù)

@interface AMTabBarItemModel : NSObject

@property (nonatomic, copy) UIImage * normalImage;

@property (nonatomic, copy) UIImage * selectedImage;

@property (nonatomic, copy) NSString * title;

@property (nonatomic, strong) UIViewController * viewController;

+ (instancetype) tabBarItemModelWithNormalImage:(UIImage*) normalImage selectedImage:(UIImage*) selectedImage title:(NSString*) title viewController:(UIViewController*) viewController;

@end

     

  • 定義標(biāo)簽欄

定義一個(gè)UIView的子類作為自定義的標(biāo)簽類

設(shè)置樣式屬性,兩種樣式:只有圖片和文字圖片都有的

typedef enum {
    AMTabBarStyleTitleAndImage,
    AMTabBarStyleImageOnly
}AMTabBarStyle;

創(chuàng)建方法:

+ (instancetype)tabBarViewWithItemModels:(NSArray *)models tabBarStyle:(AMTabBarStyle)tabBarStyle
{
    return [[self alloc] initWithItemModels:models tabBarStyle:tabBarStyle];
}
- (instancetype) initWithItemModels:(NSArray*) models tabBarStyle:(AMTabBarStyle) tabBarStyle
{
    if ( self = [super init] ) {
        self.tabBarStyle = tabBarStyle;
        int i = 1;
        for ( AMTabBarItemModel * model in models ) {
            AMTabBarButton * btn;
            if ( tabBarStyle == AMTabBarStyleTitleAndImage ) {
                btn = [AMTabBarButton tabBarButtonWithTitle:model.title normalImage:model.normalImage selectedImage:model.selectedImage];
            }
            else {
                btn = [AMTabBarButton tabBarButtonWithNormalImage:model.normalImage selectedImage:model.selectedImage];
            }
            
            [self addSubview:btn];
            
            [btn addTarget:self action:@selector(itemBtnClicked:) forControlEvents:UIControlEventTouchDown];
            btn.tag = i;
            i++;
        }
        self.selectedBtn = self.subviews[0];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat x, y, w, h;
    h = self.frame.size.height;
    w = self.frame.size.width/self.subviews.count;
    y = x = 0;
    
    for ( AMTabBarButton * btn in self.subviews ) {
        btn.frame = CGRectMake(x, y, w, h);
        x += w;
    }
}

     參數(shù)items:模型對(duì)象數(shù)組


當(dāng)標(biāo)簽按鈕被點(diǎn)擊時(shí)發(fā)送代理方法:

- (void) itemBtnClicked:(AMTabBarButton*) sender
{
    self.selectedBtn = sender;
    if ( self.delegate && [self.delegate respondsToSelector:@selector(tabBarView:selectedIndex:)]) {
        [self.delegate tabBarView:self selectedIndex:sender.tag];
    }
}
- (void) setSelectedBtn:(AMTabBarButton *)selectedBtn
{
    if ( _selectedBtn != selectedBtn ) {
        if ( _selectedBtn != nil ) {
            _selectedBtn.selected = NO;
        }
        _selectedBtn = selectedBtn;
        _selectedBtn.selected = YES;
    }
}

    其中selectBtn屬性表示當(dāng)前被選擇的按鈕


  • 定義UITabBarController子類

添加屬性:樣式、標(biāo)簽欄、模型數(shù)組

@property (nonatomic, assign) AMTabBarStyle tabBarStyle;
@property (nonatomic, weak) AMTabBarView * myTabBar;
@property (nonatomic, strong) NSArray * items;


添加創(chuàng)建控制器子類的方法:

+ (instancetype)tabBarControllerWithItems:(NSArray *)items tabBarStyle:(AMTabBarStyle)tabBarStyle
{
    AMTabBarController * tabBarController = [[UIStoryboard storyboardWithName:@"AMTabBarController" bundle:nil] instantiateInitialViewController];
    
    tabBarController.items = items;
    tabBarController.tabBarStyle = tabBarStyle;
    
    NSMutableArray * arr = [NSMutableArray array];
    
    for ( AMTabBarItemModel * model in items ) {
        [arr addObject:model.viewController];
        model.viewController = nil;
    }
    tabBarController.viewControllers = [arr copy];
    
    return tabBarController;
}


創(chuàng)建自定義的標(biāo)簽欄,添加到tabBar視圖上

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupTabBar];
}
- (void) setupTabBar
{
    AMTabBarView * myTabBar = [AMTabBarView tabBarViewWithItemModels:self.items tabBarStyle:self.tabBarStyle];
    
    self.myTabBar = myTabBar;
    
    [self.tabBar addSubview:self.myTabBar];
    self.myTabBar.delegate = self;
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    self.myTabBar.frame = self.tabBar.bounds;
}


實(shí)現(xiàn)自定義標(biāo)簽欄的代理方法,切換頁(yè)面

- (void)tabBarView:(AMTabBarView *)tabBarView selectedIndex:(NSInteger)index
{
    self.selectedIndex = index-1;
}




向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI