完全定製UITabBarViewController

来源:http://www.cnblogs.com/YouXianMing/archive/2016/06/03/5556584.html
-Advertisement-
Play Games

完全定製UITabBarViewController 效果 源碼 https://github.com/YouXianMing/iOS-Project-Examples 中的 TotalCustomTabBarController 說明 詳細細節請參考演示項目,定製按鈕需要繼承控制器,在重載buil ...


完全定製UITabBarViewController

 

效果

 

 

源碼

https://github.com/YouXianMing/iOS-Project-Examples 中的 TotalCustomTabBarController

//
//  CustomTabBarViewController.h
//  TotalCustomTabBarController
//
//  Created by YouXianMing on 16/6/2.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "CustomViewController.h"
@class CustomTabBarViewController;

@protocol CustomTabBarViewControllerDelegate <NSObject>

@optional

- (BOOL)customTabBarController:(CustomTabBarViewController *)tabBarController
    shouldSelectViewController:(UIViewController *)viewController
                 selectedIndex:(NSInteger)index;

- (void)customTabBarController:(CustomTabBarViewController *)tabBarController
       didSelectViewController:(UIViewController *)viewController
                 selectedIndex:(NSInteger)index;

@end

@interface CustomTabBarViewController : CustomViewController

/**
 *  CustomTabBarViewController's delegate.
 */
@property (nonatomic, weak) id <CustomTabBarViewControllerDelegate> delegate;

/**
 *  TabBar's height, default is 49.f.
 */
@property (nonatomic) CGFloat tabBarHeight;

/**
 *  The controller's index that loaded and show by CustomTabBarViewController at the first time.
 */
@property (nonatomic) NSInteger  firstLoadIndex;

/**
 *  ViewControllers.
 */
@property(nonatomic, strong) NSArray <__kindof CustomViewController *> *viewControllers;

/**
 *  Hide TabBarView or not.
 *
 *  @param hide     Hide or not.
 *  @param animated Animated or not.
 */
- (void)hideTabBarView:(BOOL)hide animated:(BOOL)animated;

#pragma mark - Used by subClass.

/**
 *  TabBarView, you should add view on it.
 */
@property (nonatomic, strong, readonly) UIView  *tabBarView;

/**
 *  Will select index, used by subClass.
 *
 *  @param index Index.
 *
 *  @return Will selected or not.
 */
- (BOOL)willSelectIndex:(NSInteger)index;

/**
 *  Did selected index, used by subClass.
 *
 *  @param index Index.
 */
- (void)didSelectedIndex:(NSInteger)index;

/**
 *  Build items in the tabBarView.
 */
- (void)buildItems;

@end
//
//  CustomTabBarViewController.m
//  TotalCustomTabBarController
//
//  Created by YouXianMing on 16/6/2.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "CustomTabBarViewController.h"

@interface CustomTabBarViewController ()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *tabBarView;

@property (nonatomic, weak)   UIViewController  *currentViewController;

@end

@implementation CustomTabBarViewController

- (instancetype)init {
    
    if (self = [super init]) {
        
        _tabBarHeight   = 49.f;
        _firstLoadIndex = 0;
    }
    
    return self;
}

- (void)setup {
    
    [super setup];
    
    // Add controller's view.
    self.contentView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.contentView];
    
    // Add tabBarView.
    self.tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - _tabBarHeight,
                                                               self.view.frame.size.width, _tabBarHeight)];
    [self.view addSubview:self.tabBarView];
    
    // Add ChildViewController.
    for (int i = 0; i < self.viewControllers.count; i++) {
        
        CustomViewController *customViewController = self.viewControllers[i];
        [self addChildViewController:customViewController];
    }
    
    // Build items.
    [self buildItems];
    
    // Load first show controller.
    [self.viewControllers[_firstLoadIndex] didMoveToParentViewController:self];
    [self.contentView addSubview:self.viewControllers[_firstLoadIndex].view];
    self.currentViewController = self.viewControllers[_firstLoadIndex];
    [self didSelectedIndex:_firstLoadIndex];
}

- (void)buildItems {
    
    // Overwrite by subClass.
}

- (BOOL)willSelectIndex:(NSInteger)index {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(customTabBarController:shouldSelectViewController:selectedIndex:)]) {
        
        return [self.delegate customTabBarController:self shouldSelectViewController:self.viewControllers[index] selectedIndex:index];
        
    } else {
        
        return YES;
    }
}

- (void)didSelectedIndex:(NSInteger)index {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(customTabBarController:didSelectViewController:selectedIndex:)]) {
        
        [self.delegate customTabBarController:self didSelectViewController:self.viewControllers[index] selectedIndex:index];
    }
    
    if ([self.currentViewController isEqual:self.viewControllers[index]]) {
        
        return;
    }
    
    [self transitionFromViewController:self.currentViewController toViewController:self.viewControllers[index] duration:0
                               options:UIViewAnimationOptionTransitionNone
                            animations:nil completion:^(BOOL finished) {
                                
                                self.currentViewController = self.viewControllers[index];
                            }];
}

- (void)hideTabBarView:(BOOL)hide animated:(BOOL)animated {
    
    CGRect  frame    = self.tabBarView.frame;
    CGFloat duration = 0.5f;
    
    if (hide) {
        
        if (animated) {
            
            [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                
                self.tabBarView.frame = CGRectMake(0, self.view.bounds.size.height, frame.size.width, frame.size.height);
                self.tabBarView.alpha = 0.f;
                
            } completion:nil];
            
        } else {
            
            self.tabBarView.frame = CGRectMake(0, self.view.bounds.size.height, frame.size.width, frame.size.height);
            self.tabBarView.alpha = 0.f;
        }
        
    } else {
        
        if (animated) {
            
            [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                
                self.tabBarView.frame = CGRectMake(0, self.view.bounds.size.height - frame.size.height,
                                                   frame.size.width, frame.size.height);
                self.tabBarView.alpha = 1.f;
                
            } completion:nil];
            
        } else {
            
            self.tabBarView.frame = CGRectMake(0, self.view.bounds.size.height - frame.size.height,
                                               frame.size.width, frame.size.height);
            self.tabBarView.alpha = 1.f;
        }
    }
}

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
}

@end

 

說明

詳細細節請參考演示項目,定製按鈕需要繼承控制器,在重載buildItems方法中添加相關事件即可。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 昨天因為需要開始學習Pomelo 做H5游戲的服務端。 因為個人學習習慣,我從來不適合去跟著文檔看。一般我直接是看下大概的API,但是Pomelo的API全部都是英文的。 昨天我就告訴自己用一下午時間去做一個最基本的通信功能的DEMO。 安裝NODE.JS Phython VS 一切就緒之後就開始了 ...
  • 這篇文章是講解 Ioinc中怎麼實現 下拉刷新和上拉載入的。也是我們日常做項目是必不可少的功能。有興趣的小伙伴可以來學習一下。 更多關於 IONIC 的資源: http://www.aliyue.net/?s=ionic HTML部分 JS部分 on-refresh 下拉觸發的函數 函數執行結束之前 ...
  • AJAX 在現代瀏覽器上寫AJAX主要依靠XMLHttpRequest對象: function success(text) { var textarea = document.getElementById('test-response-text'); textarea.value = text; } ...
  • window window對象不但充當全局作用域,而且表示瀏覽器視窗。 window對象有innerWidth和innerHeight屬性,可以獲取瀏覽器視窗的內部寬度和高度。內部寬高是指除去菜單欄、工具欄、邊框等占位元素後,用於顯示網頁的凈寬高。還有一個outerWidth和outerHeight ...
  • 昨天的《移動 Web 開發技巧》的這篇文章,大家反響不錯,因為這些問題在大家日常寫移動端的頁面時經常遇到的。所以那個文章還是超級實用的,那麼我們今天繼續來分享一下移動端的web開發技巧吧,希望對大家有所幫助。 PS:不要讓小伙伴第一次寫移動端像下麵這位一臉的蒙逼哈哈… … 第一、啟用 WebApp ...
  • 迭代器(iterator)是一個可以順序存取數據集合的對象。其一個典型的API是next方法。該方法獲得序列中的下一個值。 迭代器示例 測試代碼好下: 初步編碼 用上面的測試代碼進行測試 錯誤分析 代碼運行結果並不正確,下麵就對初始的編碼程式進行分析。 這裡的指代錯誤,很像是另一個讓人頭痛的對象th ...
  • 什麼,你現在還在看knockoutjs?這貨都已經落後主流一千年了!趕緊去學Angular、React啊,再不趕緊的話,他們也要變out了哦。身旁的90後小伙伴,嘴裡還塞著山東的狗不理大蒜包,卻依然振振有詞地喋喋不休,一臉真誠。是啊,前端發展太快,那邊前幾年出的框架已是無人問津的半老徐娘,而這邊各種... ...
  • 目錄: TweenMax動畫庫學習(一) TweenMax動畫庫學習(二) 之前在做HTML5移動端開發的時候,用的都是Animate.css,這個插件封裝的的確很好,但是在做一些緩動方面的動畫,它也有一定的不足之處,比如手要寫一個連續的動畫,需要不停的去重覆寫函數,使得代碼嚴重的冗餘,再比如要獲取 ...
一周排行
    -Advertisement-
    Play Games
  • 概述:本文代碼示例演示瞭如何在WPF中使用LiveCharts庫創建動態條形圖。通過創建數據模型、ViewModel和在XAML中使用`CartesianChart`控制項,你可以輕鬆實現圖表的數據綁定和動態更新。我將通過清晰的步驟指南包括詳細的中文註釋,幫助你快速理解並應用這一功能。 先上效果: 在 ...
  • openGauss(GaussDB ) openGauss是一款全面友好開放,攜手伙伴共同打造的企業級開源關係型資料庫。openGauss採用木蘭寬鬆許可證v2發行,提供面向多核架構的極致性能、全鏈路的業務、數據安全、基於AI的調優和高效運維的能力。openGauss深度融合華為在資料庫領域多年的研 ...
  • openGauss(GaussDB ) openGauss是一款全面友好開放,攜手伙伴共同打造的企業級開源關係型資料庫。openGauss採用木蘭寬鬆許可證v2發行,提供面向多核架構的極致性能、全鏈路的業務、數據安全、基於AI的調優和高效運維的能力。openGauss深度融合華為在資料庫領域多年的研 ...
  • 概述:本示例演示了在WPF應用程式中實現多語言支持的詳細步驟。通過資源字典和數據綁定,以及使用語言管理器類,應用程式能夠在運行時動態切換語言。這種方法使得多語言支持更加靈活,便於維護,同時提供清晰的代碼結構。 在WPF中實現多語言的一種常見方法是使用資源字典和數據綁定。以下是一個詳細的步驟和示例源代 ...
  • 描述(做一個簡單的記錄): 事件(event)的本質是一個委托;(聲明一個事件: public event TestDelegate eventTest;) 委托(delegate)可以理解為一個符合某種簽名的方法類型;比如:TestDelegate委托的返回數據類型為string,參數為 int和 ...
  • 1、AOT適合場景 Aot適合工具類型的項目使用,優點禁止反編 ,第一次啟動快,業務型項目或者反射多的項目不適合用AOT AOT更新記錄: 實實在在經過實踐的AOT ORM 5.1.4.117 +支持AOT 5.1.4.123 +支持CodeFirst和非同步方法 5.1.4.129-preview1 ...
  • 總說周知,UWP 是運行在沙盒裡面的,所有許可權都有嚴格限制,和沙盒外交互也需要特殊的通道,所以從根本杜絕了 UWP 毒瘤的存在。但是實際上 UWP 只是一個應用模型,本身是沒有什麼許可權管理的,許可權管理全靠 App Container 沙盒控制,如果我們脫離了這個沙盒,UWP 就會放飛自我了。那麼有沒... ...
  • 目錄條款17:讓介面容易被正確使用,不易被誤用(Make interfaces easy to use correctly and hard to use incorrectly)限制類型和值規定能做和不能做的事提供行為一致的介面條款19:設計class猶如設計type(Treat class de ...
  • title: 從零開始:Django項目的創建與配置指南 date: 2024/5/2 18:29:33 updated: 2024/5/2 18:29:33 categories: 後端開發 tags: Django WebDev Python ORM Security Deployment Op ...
  • 1、BOM對象 BOM:Broswer object model,即瀏覽器提供我們開發者在javascript用於操作瀏覽器的對象。 1.1、window對象 視窗方法 // BOM Browser object model 瀏覽器對象模型 // js中最大的一個對象.整個瀏覽器視窗出現的所有東西都 ...