1. 程式人生 > >UITabBar UITabBarItem 自定義

UITabBar UITabBarItem 自定義

UITabBar 自定義詳細解讀

不得不說UITabBar神一般的存在

使用過UITabbar的人都知道, 很多App開發都挺喜歡用UITabbar,系統的UITabbar的確很方便,省事,強大的iOS 的api給開發帶來很多便利,我們看看企鵝開發的微信就知道了 。。。。 微信的iOS 版就使用了UITabbar,而且可以猜測,八成是用的系統自帶的 為啥敢這樣下結論,微信不會浪費效能在花哨的UI上的,支付寶的大框架也是UITabbar,還有美團等等 。。。。。
不管是支付寶,微信還是美團 , 他們的UITabbar都有個特點 。。。
這裡寫圖片描述

第一: 每個UITabBarItem 都包含圖片和文字 ,而且包含兩種狀態,選中和未選中
第二:無論選中或者未選中,圖片和文字的佈局都不變
這種UITabbar是程式猿最喜歡的,因為程式碼最少,效能最高


But , 還有一種 UITabBarItem 只有圖片 , 有代表性的是 Facebook
這裡寫圖片描述

以上兩種都比較好辦,只有圖片的話其實通過移動 系統 UITabBarItem 中圖片的位置也可以處理好的 , 但是有些比較 噁心的 UITabBarItem ,觀賞下 。。。
這裡寫圖片描述

自定義UITabBarItem

1.通常情況下設計師不懂技術更不懂所謂的效能,模仿的居多,看到別人這樣TA也這樣;企鵝微信的UI是我見過最簡潔且比較容易實現的UI , UITabBarItem為啥微信不自定義了 ? 朋友圈的圖片為啥限制在9張之內了 ? 為啥都採用iOS 系統的UITableView了 ? 只能說企鵝的設計師還是有兩把刷子的,至少是很懂iOS系統的 。。。。
2.大部分小公司的設計呵呵, 能力沒有,懟起來牛逼,你見過高度是 55 的 UITabBar 我就見過,而且語不驚人死不休,來一句“等比放大” 。。。。 無需多吐槽,懂得自然懂。。。

1.首先定義UITabBarItem#####

簡單的說,把單個的Item當做一個View來看待就可以 ,圖片,文字,大圖等 。。。。。
就以我上面截圖來說

#import <UIKit/UIKit.h>

@interface HbTabBarItem : UIView{
    UILabel *textLabel;
    UIImage *select,*unselect;
    UIImageView *iconImage;
    UIImageView *bigImage;

}
-(instancetype)initWithTitle:(NSString
*)title unselectImage:(NSString * )unsimage selectedImage:(NSString *)selectImg size:(CGSize)imgSize; -(void)setItemSelected:(BOOL)itemSelected tag:(NSInteger)tag; @end
#import "HbTabBarItem.h"
#import "Masonry.h"


#define RGBA(R,G,B,A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
#define RGB(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]


#define HEXRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define HEXRGBA(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]


#define COLOR_666666 HEXRGB(0x666666)
#define COLOR_abaaaa HEXRGB(0xabaaaa)

#define FONT_MEDIUM @"PingFangSC-Medium"
#define FONT_REGULAR @"PingFangSC-Regular"
#define FONT_SEMIBOLD @"PingFangSC-Semibold"
#define FONT_SC @"PingFangSC"


@implementation HbTabBarItem

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)init{
    if(self=[super init]){

    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];

}

-(void)setup{
    textLabel = [[UILabel alloc] init];
    textLabel.font = [UIFont fontWithName:FONT_REGULAR size:11];
    textLabel.text = @"  ";
    [self addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-4);
        make.centerX.equalTo(self);
    }];
    iconImage = [[UIImageView alloc] init];
    iconImage.image = [UIImage imageNamed:@"bar1_selected"];
    [self addSubview:iconImage];
    [iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(16, 16));
        make.bottom.equalTo(self).offset(-26);
        make.centerX.equalTo(self);
    }];

    bigImage = [[UIImageView alloc] init];
    bigImage.image = [UIImage imageNamed:@"bar1_selectImage"];
    [self addSubview:bigImage];
    [bigImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(42, 42));
        make.center.equalTo(self);
    }];
    bigImage.hidden = YES;
}

-(instancetype)initWithTitle:(NSString*)title unselectImage:(NSString * )unsimage selectedImage:(NSString *)selectImg size:(CGSize)imgSize{
    self=[super init];
    [self setup];


    select = [UIImage imageNamed:selectImg];
    unselect = [UIImage imageNamed: unsimage];
    textLabel.text = title;
    [iconImage setImage:select];
    [iconImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(imgSize);
        make.bottom.equalTo(self).offset(-24);
        make.centerX.equalTo(self);
    }];
    return self;
}

-(void)setItemSelected:(BOOL)itemSelected tag:(NSInteger)tag{


    if (itemSelected && tag == 0) {
        bigImage.hidden = NO;
        textLabel.hidden = YES;
        iconImage.hidden = YES;
    }else{
        bigImage.hidden = !NO;
        textLabel.hidden = !YES;
        iconImage.hidden = !YES;
    }

    if (itemSelected) {
        iconImage.image = select;
        textLabel.textColor = COLOR_666666;
    }else{
        iconImage.image = unselect;
        textLabel.textColor = COLOR_abaaaa;
    }

}
@end

以上是定義單個的 Item ,然後就想把Item放在一起
也就是自定定義tabbar

#import <UIKit/UIKit.h>

@class HbTabBarItem;

@interface HbTabbar : UITabBar
@property(strong, nonatomic) NSArray <HbTabBarItem*> *tabItems;
-(void)setCurrentIndex:(NSInteger)tag;
@end
#import "HbTabbar.h"
#import "HbTabBarItem.h"


@implementation HbTabbar

-(instancetype)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame])
    {
        self.translucent=NO;

    }
    return self;
}
-(void)clearView{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}

-(void)setTabItems:(NSArray *)tabItems{
    _tabItems=tabItems;
    [self clearView];
    for (int i=0; i<tabItems.count; i++) {

        HbTabBarItem *barItem=tabItems[i];
        [self addSubview:barItem];
    }

    [self setCurrentIndex:0];
}


-(void)setCurrentIndex:(NSInteger)tag{

    for (int i=0; i<self.tabItems.count; i++) {
        HbTabBarItem *barItem=self.tabItems[i];
        [barItem setItemSelected:i==tag tag:tag];

    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self setupTabBarItems];
}
- (void)setupTabBarItems
{
    CGFloat w = self.frame.size.width;
    CGFloat h = self.frame.size.height;

    int count = (int)self.tabItems.count;
    CGFloat itemY = 0;
    CGFloat itemW = w / count;
    CGFloat itemH = h;

    for (int index = 0; index < count; index++) {
        HbTabBarItem *tabBarItem = self.tabItems[index];
        CGFloat itemX = index * itemW;
        tabBarItem.frame = CGRectMake(itemX, itemY, itemW, itemH);
    }
}

@end

不用多解釋 ,複製程式碼就行 ,
然後就是使用了 。。。。。。

#import "ViewController.h"

#import "HbTabbar.h"
#import "HbTabBarItem.h"

#import "V1ViewController.h"
#import "V2ViewController.h"
#import "V3ViewController.h"
#import "V4ViewController.h"
#define AppFrameWidth  [UIScreen mainScreen].bounds.size.width
#define AppFrameHight [UIScreen mainScreen].bounds.size.height




@interface ViewController ()<UITabBarControllerDelegate>{
    UITabBarController  *viewController;
    HbTabbar *hbTabbar;

}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    V1ViewController *h1 = [[V1ViewController alloc] init];
    HbTabBarItem *t1 = [[HbTabBarItem alloc] initWithTitle:@"首頁" unselectImage:@"bar1_unselected" selectedImage:@"bar1_unselected" size:CGSizeMake(18, 16)];


     V2ViewController *h2 = [[V2ViewController alloc] init];
     HbTabBarItem *t2 = [[HbTabBarItem alloc] initWithTitle:@"聯絡人" unselectImage:@"bar2_unselected" selectedImage:@"bar2_selected" size:CGSizeMake(16, 16)];



     V3ViewController *h3 = [[V3ViewController alloc] init];
    HbTabBarItem *t3 = [[HbTabBarItem alloc] initWithTitle:@"收藏" unselectImage:@"bar3_unselected" selectedImage:@"bar3_selected" size:CGSizeMake(18, 16)];



     V4ViewController *h4 = [[V4ViewController alloc] init];
     HbTabBarItem *t4 = [[HbTabBarItem alloc] initWithTitle:@"其他" unselectImage:@"bar4_unselected" selectedImage:@"bar4_selected" size:CGSizeMake(17, 19)];





    UINavigationController *h11 = [[UINavigationController alloc] initWithRootViewController:h1];
    UINavigationController *h22 = [[UINavigationController alloc] initWithRootViewController:h2];
    UINavigationController *h33 = [[UINavigationController alloc] initWithRootViewController:h3];
    UINavigationController *h44  = [[UINavigationController alloc] initWithRootViewController:h4];


    NSArray *viewControllers = [NSArray arrayWithObjects:h11,h22,h33,h44, nil];


    viewController = [[UITabBarController alloc] init];


    hbTabbar = [[HbTabbar alloc] initWithFrame:viewController.tabBar.frame];
    //    hbTabbar.ydelegate=self;
    [viewController setValue:hbTabbar forKeyPath:@"tabBar"];
    hbTabbar.tabItems[email protected][t1,t2,t3,t4];

    viewController.delegate = self;
    viewController.viewControllers = viewControllers;
    viewController.view.frame = CGRectMake(0, 0, AppFrameWidth, AppFrameHight);
    [viewController.tabBar setClipsToBounds:YES];
//    [viewController.tabBar insertSubview:backView atIndex:0];
    viewController.tabBar.opaque = YES;
    [self.view addSubview:viewController.view];

}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

    [hbTabbar setCurrentIndex:tabBarController.selectedIndex];

}

最後,小公司的APP不存在效率問題—因為沒人用

相關推薦

UITabBar UITabBarItem 定義

UITabBar 自定義詳細解讀 不得不說UITabBar神一般的存在 使用過UITabbar的人都知道, 很多App開發都挺喜歡用UITabbar,系統的UITabbar的確很方便,省事,強大的iOS 的api給開發帶來很多便利,我們看看企鵝開發的微信就

iOS 開發 中級:UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem定義方法總結

對於UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem這幾種控制元件的自定義,因為具備共同性,因此放在一起討論。 通常有兩種

UITabBarItem定義圖片(去除灰色)

在專案中使用UITabBarController的時候, 每個UITabBarItem都可以展示對應的圖片。 UITabBarItem有兩種狀態, 即 選中/ 非選中狀態。 如果用storyboard設定圖片, 那麼設定的是選中狀態的圖片, 並且, 它會自動的填充為深

iOS開發- UITabBarItem定義圖片

1:如圖一下效果 程式碼如下: QRcodeViewController *threeViewController     = [[QRcodeViewControlleralloc]init]; UINavigationController *threeNavig

UItabbarController定義 常用架構 UITabBar UITabBaritem

(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions { self.window = [[UIWind

UITabBar定義方式三(中間有按鈕)

1.MainTableViewController #import "MainTabBarViewController.h" #import "IWHomeViewController.h" #import "IWMessageViewController.h" #i

ios定義中間凸出 UITabBar,so easy……

IOS 自定義UITabBar ,中間按鈕凸出 很多專案中都有中間凸出的UI設計,這個不規則的設計才坑爹,必須要自定義。在安卓上 做這個也是坑,現在只說ios的實現。 示意圖:  問題和難點: 橫線,凸出部分上的黑色半圓線;超出UITabbr部分的點選事件;UIT

iOS如何定義的設定UITabbarItem的badge

本文的程式碼實現效果如下: 除了第三個tabbarItem顯示數字,其他的都顯示紅點 程式碼如下: //tabbar訊息未讀數量 - (void)showUnreadCountViewItemNO:(NSInteger)index unReadCountSum:(NS

Swift定義UITabBar

前言 很多時候,系統原生的 UITabBar 並不能滿足我們的需求,譬如我們想要給圖示做動態的改變,或者比較炫一點的展示,原生的處理起來都很麻煩。所以很多時候都需要自定義一個 UITabBar,裡面的圖示、顏色、背景等等都可以根據需求去改變。 效果展示:

定義UITabBarController、UITabBar和UIButton

  通常情況下,在實際開發過程中經常需要自定義UITabBarController,並且很有可能還涉及到自定義UITabBar和UIButton的情況。就以閒魚為例,我們嘗試著模仿一下它。   為了更好的演示和說明,整個演示專案都將使用純程式碼來搭建。所以

UITabBar和UINavigation組合使用與定義樣式(iOS)

UITabBarController和UINavigationController組合使用與自定義樣式(iOS)環境版本:mac 10.10 / ios7+ / xcode 6.3大部分APP的介面框架都需要UITabBarController和UINavigationCon

iOS 定義UITabbar新增系統毛玻璃效果

UIImageView *backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TabBar_Height_1)]; backView.image = [UIIm

WF4.0以上使用代碼完整定義動態生成執行工作流Xaml文件

load 控制 brush 類型 rpv 返回 cap 並且 sco 給大家分享一下,如何完全使用代碼自定義的創建生成工作流文件(用代碼創建Xaml文件),並且動態加載運行所生成的工作流。 工作流生成後 在Xaml文件裏的主要節點如下: 輸入輸出參數 <x

定義maven變量以及maven內置常量

文件 rect 需要 nal tool tdi cto packaging 構建 本文轉自:http://zheng12tian.iteye.com/blog/1770909 在創建Maven工程後,插件配置中通常會用到一些Maven變量,因此需要找個地方對這些變量進行統一

WPF定義輪播控件

cnblogs log .cn 蛋疼 源碼 logs alt files 一個      閑得蛋疼做了一個WPF制作輪播動畫,勉強可以看,寫個隨筆留個腳印。      源碼:有需要的可留言。              效果圖:   WPF

定義MySQL函數

ffffff 現在 sql code begin 函數 limit round 其他 1、MySQL創建函數語法: CREATE   [DEFINER = { user | CURRENT_USER }] FUNCTION sp_name ([func_parameter

FastReport調用Delphi中的定義函數

int 加載 自定義函數 調用 create phi tro fast creat //定義一個函數; function SmallToMoney(akey: real): string; begin //‘1234500‘ end; //此處為fastreport

日本程序開發式定義的malloc/free函數(三)-源代碼(ソースコード)

size span 鏈表 pan ppp 附近 efi ret system 這篇文章終於是貼出了我們的源代碼,實現每個功能也是花費了許多時間,大家在編寫的時候可以多花點時間,多嘗試,多看就能寫出來。 老師的要求不能在程序裏面使用malloc,new什麽的,有要求使用鏈表,

vue.js2.0 定義組件初體驗

最新 解綁 然而 blog bool template 警告 rem 組件 理解 組件(Component)是 Vue.js 最強大的功能之一。組件可以擴展 HTML 元素,封裝可重用的代碼。在較高層面上,組件是自定義元素, Vue.js 的編譯器為它添加特殊功能。在有些

angular創建定義指令的四種方式

htm mil 成功 評論 utf-8 例如 angularjs size 限制   angular除了內置的部分指令,還可以通過.directive來自定義指令。要調用自定義指令,HTML 元素上需要添加自定義指令名。使用駝峰法來命名一個指令:nsHeader,在調用時使