自定义控件iOS开发好文自定义tabbar

iOS 自定义Tabbar

2015-06-01  本文已影响1420人  cb6a1e2768d1

最终效果:


最终效果

思路就是
1.移除原来的tabbar,自定义一个view
2.覆盖原来的tabbar

======华丽的分割线====
演示覆盖
1.拉一个Tab bar Controller进storyboard


图1

2.command + N创建文件继承自UITarbarController
3.command + N在创建继承自UIView
如下图所示


我就是如下图
4.在Controller中加入view,并且加入UIButton
#import "PLTabBarController.h"
#import "TabBarView.h"

@interface PLTabBarController ()<TabBarViewDelegate>


@end

@implementation PLTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    TabBarView * tabbar = [[TabBarView alloc]init];
    tabbar.frame = self.tabBar.bounds;
    tabbar.delegate = self;
    
    NSString * imageName = nil;
    NSString * selName = nil;
    for (int i = 0; i < self.childViewControllers.count; i++) {
        imageName = [NSString stringWithFormat:@"%d",i+1];
        selName = [NSString stringWithFormat:@"s%d",i+1];
        [tabbar addTarBarButtonWithName:imageName selName:selName];
    }
    [self.tabBar addSubview:tabbar];
    
}

- (void)tabbar:(TabBarView *)tabbar didSelectedIndex:(NSInteger)selectIndex
{
    self.selectedIndex = selectIndex;
}


@end

5.UIButton实现点击效果,代理切换控制器
.h文件

#import <UIKit/UIKit.h>

@class TabBarView;

@protocol TabBarViewDelegate<NSObject>

@optional
- (void)tabbar:(TabBarView*)tabbar didSelectedIndex:(NSInteger)selectIndex;

@end

@interface TabBarView : UIView

@property (nonatomic, weak) id <TabBarViewDelegate> delegate;

- (void)addTarBarButtonWithName:(NSString *)name selName:(NSString *)selName;

@end

.m

#import "TabBarView.h"

@interface TabBarView ()

@property (nonatomic, weak) UIButton *selectedBtn;

@end

@implementation TabBarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)addTarBarButtonWithName:(NSString *)name selName:(NSString *)selName
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selName] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:button];
}

- (void)btnClicked:(UIButton *)button
{
    self.selectedBtn.selected = NO;
    button.selected = YES;
    self.selectedBtn = button;
    
    if ([self.delegate respondsToSelector:@selector(tabbar:didSelectedIndex:)]) {
        [self.delegate tabbar:self didSelectedIndex:button.tag];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = self.frame.size.width /self.subviews.count;
    CGFloat btnH = self.frame.size.height;
    
    for (int i = 0; i < self.subviews.count; i++) {
        UIButton * btn =  self.subviews[i];
        btn.tag = i;
        btnX = i * btnW;
        [btn setFrame:CGRectMake(btnX, btnY, btnW, btnH)];
        if (i == 0) {
            [self btnClicked:btn];
        }
    }
}

@end
上一篇 下一篇

猜你喜欢

热点阅读