在工作当中经常要写一些公共的类,所以在这里做一个小小的总结,下面是代码
PublicCell,主要用于定义背景颜色
点h
#import <UIKit/UIKit.h>
@interface PublicCell : UITableViewCell
@property (nonatomic ,strong)UIView *selectView;
@end
点m
#import "PublicCell.h"
@implementation PublicCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//定义原来的背景颜色为空
self.selectionStyle = UITableViewCellSelectionStyleNone;
//自定义想要的颜色,一开始将背景颜色设置为空
_selectView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
_selectView.backgroundColor = RGBCOLOR(234, 215, 184);
_selectView.alpha = 0.0;
[self.contentView addSubview:_selectView];
}
return self;
}
- (void)showBackGroundColor{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
_selectView.alpha = kAlpha;
_selectView.alpha = 0.0;
[UIView commitAnimations];
}
//点击cell的时候触发方法
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected)
{
[self showBackGroundColor];
}
}
PublicNavigationController,定义bar
点m
- (void)viewDidLoad
{
[super viewDidLoad];
//自定义bar背景
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"topbar"] forBarMetrics:UIBarMetricsDefault];
}
PublicTabBarController,自定义整个tabBar,代码较多
点h
#import <UIKit/UIKit.h>
#import "PublicNavigationController.h"
//代理,当点击bar的时候触发
@protocol PublicTabBarControllerDelegate <NSObject>
@optional
-(void)didSelectTabBarWithIndex:(int)index;
@end
@interface PublicTabBarController : UITabBarController
{
@private
UIImageView *_tabBarBG;
}
@property(nonatomic,strong)PublicNavigationController *moreNav;
@property(nonatomic,unsafe_unretained)id<PublicTabBarControllerDelegate>Odelegate;
//显示bar的方法
- (void)showTabBar;
//隐藏bar的方法
- (void)hiddenTabBar;
@end
点m
@interface PublicTabBarController ()
@end
@implementation PublicTabBarController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:@selector(loadViewControllers)];
[self performSelector:@selector(loadCustomTabBarView)];
}
//加载视图
- (void)loadViewControllers{
OnLineViewController *onLineVC = [[OnLineViewController alloc]initWithNibName:@"OnLineViewController" bundle:nil];
self.Odelegate = onLineVC;
PublicNavigationController *onLineNav = [[PublicNavigationController alloc]initWithRootViewController:onLineVC];
DownLoadViewController *downLoadVC = [[DownLoadViewController alloc]initWithNibName:@"DownLoadViewController" bundle:nil];
self.Odelegate = downLoadVC;
PublicNavigationController *downLoadNav = [[PublicNavigationController alloc]initWithRootViewController:downLoadVC];
MoreViewController *moreVC = [[MoreViewController alloc]initWithNibName:@"MoreViewController" bundle:nil];
_moreNav = [[PublicNavigationController alloc]initWithRootViewController:moreVC];
NSArray *viewControllers = @[onLineNav, downLoadNav, _moreNav];
[self setViewControllers:viewControllers animated:YES];
}
//创建自定义背景
- (void)loadCustomTabBarView
{
// 初始化自定义TabBar背景
if (iPhone5) {
_tabBarBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 516, 320, 52)];
}else{
_tabBarBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 480-52, 320, 52)];
}
_tabBarBG.userInteractionEnabled = YES;
_tabBarBG.image = [UIImage imageNamed:@"deepbar"];
[self.view addSubview:_tabBarBG];
NSArray *selectArray = @[@"home press",@"download press",@"more press"];
NSArray *normalArray = @[@"home normal",@"download normal",@"more normal"];
// 初始化自定义TabBarItem -> UIButton
float coordinateX = 0;
for (int index = 0; index < 3; index++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(-1+coordinateX, 3 , 107, 49);
[button setBackgroundImage:[UIImage imageNamed:[normalArray objectAtIndex:index]] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:[selectArray objectAtIndex:index]] forState:UIControlStateSelected];
[button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
button.tag = index+100;
[_tabBarBG addSubview:button];
coordinateX += 107;
}
UIButton *btn1 = (UIButton *)[self.view viewWithTag:100];
btn1.selected = YES;
}
- (void)changeViewController:(UIButton *)button
{
UIButton *btn1 = (UIButton *)[self.view viewWithTag:100];
UIButton *btn2 = (UIButton *)[self.view viewWithTag:101];
UIButton *btn3 = (UIButton *)[self.view viewWithTag:102];
self.selectedIndex = button.tag-100;
button.selected = YES;
//判断选中状态
switch (button.tag-100) {
case 0:{
btn1.selected = YES;
btn2.selected = NO;
btn3.selected = NO;
if ([self.Odelegate respondsToSelector:@selector(didSelectTabBarWithIndex:)]) {
[self.Odelegate didSelectTabBarWithIndex:button.tag];
}
}
break;
case 1:{
btn1.selected = NO;
btn2.selected = YES;
btn3.selected = NO;
if ([self.Odelegate respondsToSelector:@selector(didSelectTabBarWithIndex:)]) {
[self.Odelegate didSelectTabBarWithIndex:button.tag];
}
}
break;
case 2:{
btn1.selected = NO;
btn2.selected = NO;
btn3.selected = YES;
}
break;
default:
break;
}
}
- (void)showTabBar
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.34];
if (iPhone5) {
_tabBarBG.frame = CGRectMake(0, 516, 320, 52);
}else{
_tabBarBG.frame = CGRectMake(0, 480-52, 320, 52);
}
[UIView commitAnimations];
}
- (void)hiddenTabBar
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35];
if (iPhone5) {
_tabBarBG.frame = CGRectMake(-320, 516, 320, 52);
}else{
_tabBarBG.frame = CGRectMake(-320, 480-52, 320, 52);
}
[UIView commitAnimations];
}