//
// Header.h
// 04-TabBar
//
// Created by andezhou on 16/1/6.
// Copyright (c) 2016年 周安德. All rights reserved.
//
// 屏幕高度
#define kViewHeight [UIScreen mainScreen].bounds.size.height
// 屏幕宽度
#define kViewWidth [UIScreen mainScreen].bounds.size.width
// RGB颜色
#define RGBA(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
/********************************************************/
//
// SXTTabBar.h
// 04-TabBar
//
// Created by andezhou on 16/1/6.
// Copyright (c) 2016年 周安德. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Header.h"
//typedef enum _SXTTabBarType1{
// SXTTabBarTypeHome = 0,
// SXTTabBarTypeMessage,
// SXTTabBarTypeContact,
// SXTTabBarTypeMe
//} SXTTabBarType1;
// 定义一个枚举
typedef NS_OPTIONS(NSUInteger, SXTTabBarType) {
SXTTabBarTypeHome = 1000, // 跟buttton的tag相同
SXTTabBarTypeMessage,
SXTTabBarTypeContact,
SXTTabBarTypeMe
};
// 定义一个block
typedef void(^SXTTabBarBlock)(SXTTabBarType type);
@class SXTTabBar;
@protocol SXTTabBarDelegate <NSObject>
@optional
- (void)tabBar:(SXTTabBar *)tabBar selectedIndex:(SXTTabBarType)type;
@end
@interface SXTTabBar : UIView
@property (weak, nonatomic) id<SXTTabBarDelegate> delegate;
// block回调方法
- (void)tabBarSelectedIndexBlock:(SXTTabBarBlock)block;
@end
/******************************************************/
//
// SXTTabBar.m
// 04-TabBar
//
// Created by andezhou on 16/1/6.
// Copyright (c) 2016年 周安德. All rights reserved.
//
#import "SXTTabBar.h"
#import "SXTDockItem.h"
static NSUInteger kTag = 1000;
@interface SXTTabBar ()
@property (strong, nonatomic) SXTDockItem *selectedBtn; // 全局button
@property (strong, nonatomic) NSArray *dataList; // 存放文字和图片的数组
@property (copy, nonatomic) SXTTabBarBlock block; // block
@end
@implementation SXTTabBar
#pragma mark -
#pragma mark init methods
- (NSArray *)dataList
{
if (!_dataList) {
NSString *file = [[NSBundle mainBundle] pathForResource:@"TabBarPlist" ofType:@"plist"];
_dataList = [NSArray arrayWithContentsOfFile:file];
}
return _dataList;
}
#pragma mark -
#pragma mark lifecycle
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 设置背景颜色
self.backgroundColor = [UIColor blackColor];
// 获取button的宽度
CGFloat width = kViewWidth / self.dataList.count;
for (NSUInteger idx = 0; idx < self.dataList.count; idx ++) {
// 获取存放文字和图片的字典
NSDictionary *dict = self.dataList[idx];
// 获取btn的X坐标
CGFloat pointX = width * idx;
// 初始化一个btn
SXTDockItem *btn = [SXTDockItem buttonWithType:UIButtonTypeCustom];
// 设置frame
btn.frame = CGRectMake(pointX, 0, width, CGRectGetHeight(self.frame));
// 设置文字
[btn setTitle:dict[@"title"] forState:UIControlStateNormal];
// 设置文字颜色
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[btn setTitleColor:RGBA(128, 102, 103, 1) forState:UIControlStateNormal];
// 设置图片
[btn setImage:[UIImage imageNamed:dict[@"normal"]] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:dict[@"selected"]] forState:UIControlStateSelected];
// 添加事件响应
[btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
// 设置tag
btn.tag = kTag + idx;
// 第一个按钮默认选中
if (!idx) {
self.selectedBtn = btn;
btn.selected = YES;
}
[self addSubview:btn];
}
}
return self;
}
// 按钮事件响应方法
- (void)buttonAction:(SXTDockItem *)btn
{
// 把以前选中的button设置为不选中
self.selectedBtn.selected = NO;
// 把当前选中的button设置为选中
btn.selected = YES;
// 把当前选中的button赋值给全局button
self.selectedBtn = btn;
// delegate
if ([self.delegate respondsToSelector:@selector(tabBar:selectedIndex:)]) {
[self.delegate tabBar:self selectedIndex:btn.tag];
}
// block
if (self.block) {
self.block(btn.tag);
}
}
// block回调
- (void)tabBarSelectedIndexBlock:(SXTTabBarBlock)block
{
self.block = block;
}
@end
/***********************************************************/
//
// SXTDockItem.h
// 04-TabBar
//
// Created by andezhou on 16/1/6.
// Copyright (c) 2016年 周安德. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SXTDockItem : UIButton
@end
/***********************************************************/
//
// SXTDockItem.m
// 04-TabBar
//
// Created by andezhou on 16/1/6.
// Copyright (c) 2016年 周安德. All rights reserved.
//
#import "SXTDockItem.h"
static CGFloat kImageScale = 0.64f;
@implementation SXTDockItem
#pragma mark -
#pragma mark lifecycle
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 设置文字字体大小
self.titleLabel.font = [UIFont systemFontOfSize:14];
// 设置文字居中
self.titleLabel.textAlignment = NSTextAlignmentCenter;
// 调整图片
self.imageView.contentMode = UIViewContentModeCenter;
}
return self;
}
// 调整文字的frame contentRect:button的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat pointX = 0;
CGFloat pointY = contentRect.size.height * kImageScale;
CGFloat width = contentRect.size.width;
CGFloat height = contentRect.size.height * (1 - kImageScale);
return CGRectMake(pointX, pointY, width, height);
}
// 调整图片的frame contentRect:button的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat pointX = 0;
CGFloat pointY = 0;
CGFloat width = contentRect.size.width;
CGFloat height = contentRect.size.height * kImageScale;
return CGRectMake(pointX, pointY, width, height);
}
@end
/*****************************************************/
//
// ViewController.m
// 04-TabBar
//
// Created by andezhou on 16/1/6.
// Copyright (c) 2016年 周安德. All rights reserved.
//
/*
1. 初始化一个用来显示Button的View
2. 自定义四个按钮
3. 处理响应事件
*/
/*
手机屏幕
4\4s 3.5寸 {{0, 0}, {320, 480}}
5\5s\5c 4寸 {{0, 0}, {320, 568}}
6\6s 4.7寸 {{0, 0}, {375, 667}}
6plus\6splus 5.5寸 {{0, 0}, {414, 736}}
4寸\4.7寸\5.5寸 比例都是16:9
*/
#import "ViewController.h"
#import "SXTTabBar.h"
@interface ViewController () <SXTTabBarDelegate>
@property (strong, nonatomic) SXTTabBar *backgroundView; // 背景view
@property (weak, nonatomic) IBOutlet UILabel *textLab;
@end
@implementation ViewController
#pragma mark -
#pragma mark init methods
- (SXTTabBar *)backgroundView
{
if (!_backgroundView) {
_backgroundView = [[SXTTabBar alloc] initWithFrame:CGRectMake(0, kViewHeight - 64, kViewWidth, 64)];
_backgroundView.delegate = self;
// [_backgroundView tabBarSelectedIndexBlock:^(SXTTabBarType type) {
// [self showWithType:type];
// }];
}
return _backgroundView;
}
#pragma mark -
#pragma mark SXTTabBarDelegate
- (void)tabBar:(SXTTabBar *)tabBar selectedIndex:(SXTTabBarType)type
{
[self showWithType:type];
}
- (void)showWithType:(SXTTabBarType)type
{
switch (type) {
case SXTTabBarTypeHome:
self.textLab.text = @"首页";
break;
case SXTTabBarTypeMessage:
self.textLab.text = @"消息";
break;
case SXTTabBarTypeContact:
self.textLab.text = @"通讯录";
break;
case SXTTabBarTypeMe:
self.textLab.text = @"我";
break;
default:
break;
}
}
#pragma mark -
#pragma mark lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.backgroundView];
}
- (void)bounds
{
// 获取屏幕的size(包含状态栏)
CGRect bounds = [UIScreen mainScreen].bounds;
// 获取屏幕的size(不包含状态栏)
CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;
NSLog(@"applicationFrame:%@", NSStringFromCGRect(applicationFrame));
NSLog(@"bounds:%@", NSStringFromCGRect(bounds));
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
/************************************************************/