知识点总结:02-自定义TabarController

1.tabarViewController的viewWillAppear方法需要注意的

  • 每次切换视图的时候,会调用tabarViewController的viewWillAppear
/*实现自定义plusBtn*/

/** 发布按钮, 懒加载 */
- (UIButton *)publishButton
{
    if (!_publishButton) {
        _publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _publishButton.backgroundColor = XMGRandomColor;
        [_publishButton setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [_publishButton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        _publishButton.frame = CGRectMake(0, 0, self.tabBar.frame.size.width / 5, self.tabBar.frame.size.height);
        _publishButton.center = CGPointMake(self.tabBar.frame.size.width * 0.5, self.tabBar.frame.size.height * 0.5);
        [_publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _publishButton;
}


/**
 *  为什么要在viewWillAppear:方法中添加发布按钮?
 *  当viewWillAppear:方法被调用的时候, tabBar内部已经添加了5个UITabBarButton,在viewDidLoad方法中,5个5个UITabBarButton尚未添加,这样才能确保自定义的plusBtn最后被添加,盖上上面
 *  就可以实现一个效果 : [发布按钮]盖在其他UITabBarButton上面
 */
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    /**** 方法一: 懒加载, 增加一个发布按钮;多次对同一个控件addSubview,只会添加一次 ****/
    [self.tabBar addSubview:self.publishButton];
 
    /**** 方法二: 一次性代码,整个程序确保只添加一次****/   
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        /**** 增加一个发布按钮 ****/
//        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        publishButton.backgroundColor = XMGRandomColor;
//        [publishButton setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
//        [publishButton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
//        publishButton.frame = CGRectMake(0, 0, self.tabBar.frame.size.width / 5, self.tabBar.frame.size.height);
//        publishButton.center = CGPointMake(self.tabBar.frame.size.width * 0.5, self.tabBar.frame.size.height * 0.5);
//        [publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
//        [self.tabBar addSubview:publishButton];
//    });
}
  • tabarViewController的viewDidLoad方法中并没有将UITabBarButton添加到self.tabar里面去

2.对于控件是否需要使用Strong

  • 一般要考虑控件removeFromSuperview后是否需要销毁,如果不需要销毁,则要Strong,否则就可能销毁
    如:

1.一般的控件都使用weak属性,因为一般控件都会添加到控制器的view中,即addSubview,所以有个强指针指向控件,因此控件一般不用使用Strong

2.但是控制器的view是使用Strong属性的,因此,在换肤的情况下,将控制器的view,removeFromSuperview(从当前的主窗口被移除)以后是不会被销毁的,再次添加即可将原先的view重新展示

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UISwitch *s = [[UISwitch alloc] init];
    s.center = CGPointMake(100, 100);
    s.on = YES;
    [self.view addSubview:s];
    ...添加N个switch控件到view上
}

// 点击btn实现换肤
- (IBAction)changeColor:(UISegmentedControl *)sender {
    switch (sender.selectedSegmentIndex) {
        case 0: // 蓝色
          // 拿到开关的全局外观,设置颜色,实现一行代码改变一类控件
            [UISwitch appearance].onTintColor = [UIColor blueColor];
            break;
            
        case 1: // 红色
            [UISwitch appearance].onTintColor = [UIColor redColor];
            break;
            
        case 2: // 橙色
            [UISwitch appearance].onTintColor = [UIColor orangeColor];
            break;
    }
    
    // 方案一: 移除view, 控制器的view是Strong属性,因此移除后view未被销毁
    [self.view removeFromSuperview];
    // 重新添加控件
    [[UIApplication sharedApplication].keyWindow addSubview:self.view];
  
//  方案二: 移除switch, 如果这里选择移除开关,则开关switch需要使用Strong属性,否则,   [self.switch removeFromSuperview]后, self.switch指向的对象因为没有强指针指向,可能会被销毁
//    [self.switch removeFromSuperview];
//    [self.view addSubview:self.s];
}
  • 使用全局外观,改变对象属性的条件: 属性中有 UI_APPEARANCE_SELECTOR
@property(nullable, nonatomic, strong) UIColor *onTintColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

3.替换UITabBarController内部的tabBar

  • 为什么要替换系统内部的tabBar?


    知识点总结:02-自定义TabarController_第1张图片
    D2380F74-785F-4FF7-A062-FAEF550BC7D1.png

    因为在UITabBarController设置TabBar中的tabBarButton的frame的后,无法修改,可能是修改以后内部又调用了layoutSubview方法,覆盖了我们的修改,所以要自定义tabBarButton,所以我们要通过自定义tabBar来解决上述问题.

  • 将系统的UITabBar替换成自定义TabBar的时候要用KVC,self.tabBar是readOnly,苹果不暴露出来给我们用,self是UITabBarController.


    5AE496F3-52DB-4B3C-B74D-ED0739CF20E5.png
// 这里的self是UITabBarController
// 因为self.tabBar是readOnly,所以只有使用kvc才能赋值,即"setValue....forKeyPath..."
[self setValue:[[XMGTabBar alloc] init] forKeyPath:@"tabBar"];
  • 如果TabBar自定义程度高,则要定义类继承UIVIEW,添加自定义button,如:TabBarButton左边显示图片,右边显示文字,则去掉系统TabBar后用自定义类替代,注意自定义类最好继承UIView.

你可能感兴趣的:(知识点总结:02-自定义TabarController)