Full customization of UITabBarViewController and mvcviewcontroller

Source: Internet
Author: User

Full customization of UITabBarViewController and mvcviewcontroller

Custom UITabBarViewController

 

Effect

 

 

Source code

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

/// CustomTabBarViewController. h // TotalCustomTabBarController // Created by YouXianMing on 16/6/2. // Copyright©2016 YouXianMing. all rights reserved. // # import "CustomViewController. h "@ class Identifier; @ protocol identifier <NSObject> @ optional-(BOOL) customTabBarController :( mtmtabbarviewcontroller *) tabBarController identifier :( UIViewController *) viewController selectedIndex :( NSInteger) index;-(void) customTabBarController :( CustomTabBarViewController *) tabBarController preview :( UIViewController *) viewController selectedIndex :( NSInteger) index; @ end @ interface CustomTabBarViewController: CustomViewController/*** CustomTabBarViewController's delegate. */@ property (nonatomic, weak) id <mtmtabbarviewcontrollerdelegate> delegate;/*** TabBar's height, default is 49. f. */@ property (nonatomic) CGFloat tabBarHeight;/*** The controller's index that loaded and show by mtmtabbarviewcontroller 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 "mtmtabbarviewcontroller. 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. 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: Unknown 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

 

Description

For details, see the demo project. The Custom button must inherit the controller and add related events to the heavy load buildItems method.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.