iOS(三)实现App底部TabBar的切换:二

本文介绍如何在iOS应用中实现自定义TabBar,通过创建ItemView类设置按钮样式,并在ViewController中处理点击事件,实现TabBar的切换效果。详细步骤包括创建ItemView,设置图片和文字,以及在TabBarController中处理选中状态。
摘要由CSDN通过智能技术生成

上一篇讲述了iOS自带的TabBar,但在我所见到的很多App源码中大多用了自己写的TabBar,惯例先上图:

                   

这只是一个最简单的TabBar,但重在原理,虽然是我懒。。。。


HomeViewController.h
HomeViewController.m

ClassViewController.h
ClassViewController.m

PrivateViewController.h
PrivateViewController.m
这六个文件详情见上一篇

添加自定义的TabBar文件

ItemView.h

#import <UIKit/UIKit.h>

//用来定义tabbar按钮大小位置,图片,被选中时的颜色

@interface ItemView :UIControl

{

    UIImageView *Itemimageview;

    UILabel *Itemlabel;

}


-(void)setItemImage:(UIImage *)image forState:(UIControlState)state;//某种状态下的图片

-(void)setItemTitle:(NSString *)title;//tabbar按钮下的文字

-(void)setItemSelected:(BOOL)isSelected;//当定义的按钮被点击时,告诉被选中了

@end


ItemView.m

#import "ItemView.h"

@implementation ItemView

-(instancetype)initWithFrame:(CGRect)frame{

    self=[superinitWithFrame:frame];

    if (self) {

        CGFloat itemHeight=CGRectGetHeight(frame);

        CGFloat itemWidth =CGRectGetWidth(frame);

        Itemimageview=[[UIImageViewalloc]initWithFrame:CGRectMake((itemWidth-22)/2,10.f, 22.f,20.f)];

        Itemimageview.contentMode=UIViewContentModeScaleAspectFit;

        [self addSubview:Itemimageview];

        //定义iamgeView的位值大小,以及图片contentMode属性

        

        Itemlabel=[[UILabelalloc]initWithFrame:CGRectMake(0.f, itemHeight-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值