自定义tablebar

该博客详细介绍了如何自定义一个TablebarView,包括设置背景颜色、按钮布局、图片加载以及按钮点击事件处理。通过创建不同状态的按钮并设置代理方法,实现了导航栏在不同页面间的跳转功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



//  TablebarView.h


#import <UIKit/UIKit.h>

#pragma mark - 延展

@protocol TablebarViewDelegate <NSObject>


- (void)tablebarViewDelegate:(NSInteger )tag;


@end


@interface TablebarView : UIView


@property (nonatomic, strong) id<TablebarViewDelegate>delegate;

@property (nonatomic, strong) UIButton *button1;

@property (nonatomic, strong) UIButton *button2;

@property (nonatomic, strong) UIButton *button3;

@property (nonatomic, strong) UIButton *button4;

@property (nonatomic, strong) UIButton *button5;


@end




//  TablebarView.m

#import "TablebarView.h"

#define W [UIScreen mainScreen].bounds.size.width


@implementation TablebarView


- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        

        self.backgroundColor = [UIColor colorWithRed:233/255.f green:156/255.f blue:175/255.f alpha:1];

        

        self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];

        self.button1.frame = CGRectMake(10, 0, W / 5 - 5 , 44);

        [self.button1 setImage:[UIImage imageNamed:@"home"] forState:UIControlStateNormal];

        self.button1.tag = 1;

        [self.button1 addTarget:self action:@selector(tableBarDelegate:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:self.button1];

        

        self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];

        self.button2.frame = CGRectMake(W / 5 + 5  , 0, W / 5 - 5, 44);

        [self.button2 setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];

        self.button2.tag = 2;

        [self.button2 addTarget:self action:@selector(tableBarDelegate:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:self.button2];

        

        self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];

        self.button3.frame = CGRectMake(W / 2 - W / 5 / 2 , 0, W / 5 , 44);

        [self.button3 setImage:[UIImage imageNamed:@"相机"] forState:UIControlStateNormal];

        self.button3.tag = 3;

        [self.button3 addTarget:self action:@selector(tableBarDelegate:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:self.button3];

        

        self.button4 = [UIButton buttonWithType:UIButtonTypeCustom];

        self.button4.frame = CGRectMake(W / 2 - W / 5 / 2 + W / 5, 0, W / 5 - 5, 44);

        [self.button4 setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];

        self.button4.tag = 4;

        [self.button4 addTarget:self action:@selector(tableBarDelegate:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:self.button4];

        

        self.button5 = [UIButton buttonWithType:UIButtonTypeCustom];

        self.button5.frame = CGRectMake(W - W / 5 - 10, 0, W / 5 - 5, 44);

        [self.button5 setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];

        self.button5.tag = 5;

        [self.button5 addTarget:self action:@selector(tableBarDelegate:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:self.button5];

        

    }

    return self;

}


- (void)tableBarDelegate:(UIButton *)buttonTag {

    [self.delegate tablebarViewDelegate:buttonTag.tag];

}


@end




//  ViewController.m

#import "ViewController.h"
#import "TablebarView.h"

@interface ViewController ()<TablebarViewDelegate>

@property (nonatomic, strong) UIView *buttonView;
@end

@implementation ViewController
- (void)bar {
   
TablebarView *tableBar = [[TablebarView alloc] init];
    tableBar.
frame = CGRectMake(0, HEIGHT - 44, self.view.frame.size.width, 44);
    tableBar.
button1.backgroundColor = [UIColor colorWithRed:244/255.f green:209/255.f blue:228/255.f alpha:0.7];
    tableBar.
button2.backgroundColor = [UIColor clearColor];
    tableBar.
button3.backgroundColor = [UIColor colorWithRed:218/255.f green:59/255.f blue:99/255.f alpha:1];
    tableBar.
button4.backgroundColor = [UIColor clearColor];
    tableBar.
button5.backgroundColor = [UIColor clearColor];
    tableBar.
delegate = self;
   
    [
self.view addSubview:tableBar];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self bar];
}

- (
void)tablebarViewDelegate:(NSInteger)tag {
   
if (tag == 2) {
       
SearchViewController *sousuo = [[SearchViewController alloc] init];
       
        [
self.navigationController pushViewController:sousuo animated:YES];
    }
   
if (tag == 3) {
       
PhoneViewController *phone = [[PhoneViewController alloc] init];
        [
self.navigationController pushViewController:phone animated:YES];
    }
   
if (tag == 4) {
       
LikeViewController *like = [[LikeViewController alloc] init];
        [
self.navigationController pushViewController:like animated:YES];
    }
   
if (tag == 5) {
       
MyViewController *my = [[MyViewController alloc] init];
        [
self.navigationController pushViewController:my animated:YES];
    }
}




#import "SearchViewController.h"

#import "TablebarView.h"


#import "MyViewController.h"

#import "LikeViewController.h"

#import "PhoneViewController.h"

#import "ViewController.h"


@interface SearchViewController ()<TablebarViewDelegate>


@end


@implementation SearchViewController


- (void)setNavigationBar {

    self.navigationItem.title = @"POCKET IDOL";

    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:244/255.0f green:162/255.0f blue:184/255.0f alpha:1]];

    /* 标题颜色的更改 */

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"A_LANCETRGH" size:0.0], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil];

    

    self.navigationController.navigationBar.titleTextAttributes = dictionary;

}


- (void)viewWillAppear:(BOOL)animated {

    //隐藏导航栏

    self.navigationController.navigationBarHidden = NO;

    //    self.navigationController.tabBarItem = NO;

}


- (void)bar {

    TablebarView *tableBar = [[TablebarView alloc] init];

    tableBar.frame = CGRectMake(0, HEIGHT - 44, self.view.frame.size.width, 44);

    tableBar.button1.backgroundColor = [UIColor clearColor];

    tableBar.button2.backgroundColor = [UIColor colorWithRed:244/255.f green:209/255.f blue:228/255.f alpha:0.7];

    tableBar.button3.backgroundColor = [UIColor colorWithRed:218/255.f green:59/255.f blue:99/255.f alpha:1];

    tableBar.button4.backgroundColor = [UIColor clearColor];

    tableBar.button5.backgroundColor = [UIColor clearColor];

    tableBar.delegate = self;

    

    [self.view addSubview:tableBar];

}



- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

  self.view.backgroundColor = [UIColor whiteColor];

    [self setNavigationBar];

    [self bar];

}



- (void)tablebarViewDelegate:(NSInteger)tag {

    if (tag == 3) {

        PhoneViewController *phone = [[PhoneViewController alloc] init];

        [self.navigationController pushViewController:phone animated:YES];

    }

    if (tag == 4) {

        LikeViewController *like = [[LikeViewController alloc] init];

        [self.navigationController pushViewController:like animated:YES];

    }

    if (tag == 5) {

        MyViewController *my = [[MyViewController alloc] init];

        [self.navigationController pushViewController:my animated:YES];

    }

    if (tag == 1) {

        [self.navigationController popToRootViewControllerAnimated:YES];//返回第一页

    }

    if (tag == 2) {

        SearchViewController *sear = [[SearchViewController alloc] init];

        [self.navigationController pushViewController:sear animated:YES];

    }

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值