iOS知识收录IOS技术解决方案iOS开发常用

UITabbarController控制器使用心得

2016-07-05  本文已影响1420人  木头与琉璃

** UITabbarController控制器可以将软件按照模块进行很好的划分,也是主流软件架构的window的根控制器**
工作中,我一般会声明一个UITabbarController的子类作为window的根控制器,并实现其代理方法。

UITabBarController.h分析

属性

子控制器数组
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
设置子控制器数组
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated;
当前选择控制器
@property(nullable, nonatomic, assign)UIViewController *selectedViewController;
当前选择控制器索引
@property(nonatomic) NSUInteger selectedIndex;
这个没用过 也不晓得干嘛使
@property(nonatomic, readonly) UINavigationController *moreNavigationController;
这个没用过 也不晓得干嘛使
@property(nullable, nonatomic, copy) NSArray *customizableViewControllers;
tabbar底部标签栏
@property(nonatomic,readonly) UITabBar *tabBar ;
代理
@property(nullable, nonatomic,weak) id<UITabBarControllerDelegate> delegate;

代理方法

/**
 *  点击Item时调用:控制哪些 ViewController 的标签栏能被点击
 *
 *  @param tabBarController 当前tabbar控制器
 *  @param viewController   将要点击的目标控制器
 *
 *  @return YES:允许点击 NO:不允许点击
 */
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0);
/**
 *  点击Item时调用
 *
 *  @param tabBarController 当前tabbar控制器
 *  @param viewController   将要点击的目标控制器
 */
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
/**
 *  监控开始初始化子控制器
 *
 *  @param tabBarController  当前tabbar控制器
 *  @param viewControllers   子控制器
 */
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
/**
 *  监控初始化子控制器完成
 *
 *  @param tabBarController  当前tabbar控制器
 *  @param viewControllers   子控制器
 *  @param changed           是否改变
 */
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
/**
 *  监控初始化子控制器完成
 *
 *  @param tabBarController  当前tabbar控制器
 *  @param viewControllers   子控制器
 *  @param changed           是否改变
 */
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed __TVOS_PROHIBITED;
/**
 *  监控横竖屏切换
 *
 *  @param tabBarController tabbar控制器
 *
 *  @return 横竖屏切换枚举
 */
- (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
/**
 *  监控横竖屏切换
 *
 *  @param tabBarController tabbar控制器
 *
 *  @return 横竖屏切换枚举
 */
- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
/**
 *  定制转场动画
 *
 *  @param tabBarController    当前tabbar控制器
 *  @param animationController 动画控制器
 *
 *  @return 转场动画
 */
- (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
                               interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController NS_AVAILABLE_IOS(7_0);
/**
 *  定制转场动画
 *
 *  @param tabBarController 当前tabbar控制器
 *  @param fromVC           起点控制器
 *  @param toVC             目标控制器
 *
 *  @return 转场动画
 */
- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
                     animationControllerForTransitionFromViewController:(UIViewController *)fromVC
                                                       toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);

UIViewController分类

给每一个视图控制器拓展一个tabBarItem和tabBarController

@interface UIViewController (UITabBarControllerItem)

@property(null_resettable, nonatomic, strong) UITabBarItem *tabBarItem; // Automatically created lazily with the view controller's title if it's not set explicitly.

@property(nullable, nonatomic, readonly, strong) UITabBarController *tabBarController; // If the view controller has a tab bar controller as its ancestor, return it. Returns nil otherwise.

@end

UITabBarItem分析

** UITabBarItem继承于UIBarItem ,我们先分析下UIBarItem**

/**
 *  是否可点击
 */
@property(nonatomic,getter=isEnabled) BOOL enabled;      // default is YES
/**
 *  item的title
 */
@property(nullable, nonatomic,copy) NSString *title;        // default is nil
/**
 *  item的Image
 */
@property(nullable, nonatomic,strong) UIImage *image;        // default is nil
@property(nullable, nonatomic,strong) UIImage *landscapeImagePhone NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED; // default is nil
/**
 *  item图片的边框
 */
@property(nonatomic) UIEdgeInsets imageInsets;  // default is UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets landscapeImagePhoneInsets NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;  // default is UIEdgeInsetsZero. These insets apply only when the landscapeImagePhone property is set.
@property(nonatomic) NSInteger tag;          // default is 0

/* You may specify the font, text color, and shadow properties for the title in the text attributes dictionary, using the keys found in NSAttributedString.h.
 */
/**
 *  设置title属性
 *
 *  @param attributes 文字属性
 *  @param state      item状态
 */
- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
/**
 *  获取item属性字典
 *
 *  @param state item状态
 *
 *  @return item属性字典
 */
- (nullable NSDictionary<NSString *,id> *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

Demo(Objective-C)

AppDelegate设置window的根控制器

#import "AppDelegate.h"
#import "MYFTabbarController.h"
@interface AppDelegate ()
@property (nonatomic, strong) MYFTabbarController *tabbar;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.tabbar = [[MYFTabbarController alloc]init];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:self.tabbar];
    [self.window makeKeyAndVisible];
    return YES;
}

自定义TabbarController

#import "MYFTabbarController.h"

@interface MYFTabbarController ()

@end

@implementation MYFTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //声明子控制器
    [self setChildVC];
}
- (void)setChildVC{
    UIViewController *vc1 = [[UIViewController alloc]init];
    vc1.view.backgroundColor = [UIColor yellowColor];
    vc1.title = @"vc1";
    vc1.tabBarItem.title = @"vc1";
    vc1.tabBarItem.image = [self imageFromColor:[UIColor yellowColor] andFrame:CGRectMake(0, 0, 33, 33)];
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:vc1];
    
    UIViewController *vc2 = [[UIViewController alloc]init];
    vc2.title = @"vc2";
    vc2.tabBarItem.title = @"vc2";
    vc2.view.backgroundColor = [UIColor blueColor];
    vc2.tabBarItem.image = [self imageFromColor:[UIColor blueColor] andFrame:CGRectMake(0, 0, 33, 33)];
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:vc2];
    
    UIViewController *vc3 = [[UIViewController alloc]init];
    vc3.title = @"vc3";
    vc3.tabBarItem.title = @"vc3";
    vc3.view.backgroundColor = [UIColor redColor];
    vc3.tabBarItem.image = [self imageFromColor:[UIColor redColor] andFrame:CGRectMake(0, 0, 33, 33)];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:vc3];
    
    self.viewControllers = @[nav1,nav2,nav3];
}

- (UIImage *)imageFromColor:(UIColor *)color andFrame:(CGRect)frame{
    
    CGRect rect = frame;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); 
    return img;
}
@end

效果

1.gif

这时候你会发现item的select状态下是经过渲染的,开发中我们也大部分用自定义item的图片,而不用渲染后的结果

UITabbarItem禁止渲染

通过UIImage的方法来禁止图片渲染

// Create a version of this image with the specified rendering mode. By default, images have a rendering mode of UIImageRenderingModeAutomatic.
/**
 *  设置图片Model
 *
 *  @param renderingMode 
 typedef NS_ENUM(NSInteger, UIImageRenderingMode) {
             UIImageRenderingModeAutomatic,          // Use the default rendering mode for the context where the image is used  默认模式,自动渲染
             UIImageRenderingModeAlwaysOriginal,     // Always draw the original image, without treating it as a template 原始模式,原始图片
             UIImageRenderingModeAlwaysTemplate,     // Always draw the image as a template image, ignoring its color information 简易模式,忽视图片颜色
 } NS_ENUM_AVAILABLE_IOS(7_0);
 *
 *  @return <#return value description#>
 */
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode NS_AVAILABLE_IOS(7_0);
    vc1.tabBarItem.selectedImage = [[self imageFromColor:[UIColor yellowColor] andFrame:CGRectMake(0, 0, 33, 33)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc1.tabBarItem.image = [[self imageFromColor:[UIColor lightGrayColor] andFrame:CGRectMake(0, 0, 33, 33)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
2.gif
上一篇 下一篇

猜你喜欢

热点阅读