自定义UITabBarController

 

自定义UITabBarController,首先需要重写UITabBar,UITabBar控制着每一个按钮的布局

#import "YZTabBar.h"

 

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSInteger count = self.items.count;
    CGFloat bunW = self.frame.size.width/(count+1);
    CGFloat bunX = 0;
    NSInteger i = 0;
    for (UIView *tabBarButton in self.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            if (i == 2) {
                i++;
            }
            bunX = i*bunW;
            tabBarButton.frame = CGRectMake(bunX, 0, bunW, self.frame.size.height);
            
            i++;
            
        }
    }
    self.plusButton.center = CGPointMake(self.frame.size.width*0.5, self.frame.size.height*.4);
}

中间凸起的按钮

- (UIButton *)plusButton
{
    if (!_plusButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"1-1"] forState:UIControlStateNormal];
        [button sizeToFit];
        [button addTarget:self action:@selector(plusButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        _plusButton = button;
    }
    return _plusButton;
}

UITabBar的背景及字体颜色

//tabbar背景色
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    //    [UITabBar appearance].translucent = YES;//半透明效果
    //字体设置
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor lightGrayColor], NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateNormal];
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor blueColor], NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateSelected];

 

demo下载地址:https://github.com/yangchengzh/YZTabbarViewController

 

posted @ 2017-05-25 17:09  步凌香  阅读(398)  评论(0)    收藏  举报