iOS之自訂UITabBar替換系統預設的(添加“+”號按鈕)

來源:互聯網
上載者:User

標籤:

自訂UITabBar替換系統預設的,目的是為了在UITabBar中間位置添加一個“+號按鈕”,下面我們來聊聊具體的實現。

1、自訂WBTabBar,讓其繼承自UITabBar,代碼如下:

////  WBTabBar.h//  SinaWeibo////  Created by android_ls on 15/5/21.//  Copyright (c) 2015年 android_ls. All rights reserved.//#import <UIKit/UIKit.h>@interface WBTabBar : UITabBar@end


2、tabBar是UITabBarController的唯讀成員變數(屬性),是不讓修改的,在UITabBarController.h檔案中的聲明如下:

@property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0);

針對於這種情況,我們可以使用KVC的方式,更換系統內建的UITabBar,實現代碼如下:

    WBTabBar *tabBar = [[WBTabBar alloc] init];    [self setValue:tabBar forKeyPath:@"tabBar"];


3、添加一個UIButton到WBTabBar中,實現代碼如下:

- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // 添加一個按鈕到tabbar中        UIButton *plusBtn = [[UIButton alloc] init];        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_background_icon_add"] forState:UIControlStateNormal];        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];        plusBtn.size = plusBtn.currentBackgroundImage.size;        [plusBtn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];        [self addSubview:plusBtn];        self.plusBtn = plusBtn;    }    return self;}


4、設定加號按鈕的位置,調整WBTabBar中各個UITabBarButton的位置和寬度,具體實現代碼如下:

- (void)layoutSubviews{    [super layoutSubviews];        // 1.設定加號按鈕的位置    self.plusBtn.centerX = self.width * 0.5;    self.plusBtn.centerY = self.height * 0.5;        // 2.設定其它UITabBarButton的位置和尺寸    CGFloat tabbarButtonW = self.width / 5;    CGFloat tabbarButtonIndex = 0;    for (UIView *child in self.subviews) {        Class class = NSClassFromString(@"UITabBarButton");        if ([child isKindOfClass:class]) {            // 設定寬度            child.width = tabbarButtonW;            // 設定x            child.x = tabbarButtonIndex * tabbarButtonW;                        // 增加索引            tabbarButtonIndex++;            if (tabbarButtonIndex == 2) {                tabbarButtonIndex++;            }        }    }}


5、定義WBTabBarDelegate協議,聲明WBTabBar的代理,代碼如下:

////  WBTabBar.h//  SinaWeibo////  Created by android_ls on 15/5/21.//  Copyright (c) 2015年 android_ls. All rights reserved.//#import <UIKit/UIKit.h>#pragma mark 因為在UITabBar中已經聲明過一個UITabBarDelegate協議,#pragma mark 我們若想新增一個對外的代理函數,可以讓我們自訂的協議繼承自UITabBarDelegate,添加一個擴充函數。@class WBTabBar;@protocol WBTabBarDelegate <UITabBarDelegate>@optional- (void)tabBarDidClickPlusButton:(WBTabBar *)tabBar;@end@interface WBTabBar : UITabBar@property (nonatomic, weak) id<WBTabBarDelegate> tabBarDelegate;@end


6、在加號按鈕的點擊事件處理器中,通知代理

#pragma mark 加號按鈕點擊事件處理器- (void)plusClick{    // 通知代理    if ([self.tabBarDelegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {        [self.tabBarDelegate tabBarDidClickPlusButton:self];    }}


7、在WBTabBarController中設定WBTabBar的代理,具體實現如下:

 // 2、使用KVC的方式,更換系統內建的UITabBar    WBTabBar *tabBar = [[WBTabBar alloc] init];    tabBar.tabBarDelegate = self;    [self setValue:tabBar forKeyPath:@"tabBar"];

 

#pragma mark - HWTabBarDelegate代理方法- (void)tabBarDidClickPlusButton:(WBTabBar *)tabBar{    ComposeViewController *composeViewController= [[ComposeViewController alloc] init];    UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:composeViewController];    [self presentViewController:navigationController animated:YES completion:nil];}

iOS之自訂UITabBar替換系統預設的(添加“+”號按鈕)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.