前言
有时候系统自动的导航栏往往不能满足项目要求,如上图,导航栏上需要左右两个按钮,并且中间的按钮是可以滑动的,这就需要我们自己封装了.
上效果图
上代码
YYTopBarView是封装的导航栏,在控制器调接口即可
//
// YYTopBarView.h
// YYTopBarView
//
// Created by yyMae on 16/3/17.
// Copyright © 2016年 yyMae. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface YYTopBarView : UIView
@property (nonatomic, strong) UIButton *leftBtn;//左边的按钮
@property (nonatomic, strong) UIButton *rightBtn;//右边的按钮
- (instancetype)initWithFrame:(CGRect)frame withButtons:(NSArray *)btn_array;
@end
//
// YYTopBarView.m
// YYTopBarView
//
// Created by yyMae on 16/3/17.
// Copyright © 2016年 yyMae. All rights reserved.
//
#import "YYTopBarView.h"
#define kWIDTH self.bounds.size.width
#define kHEIGHT self.bounds.size.height
#define kBtnWidth 40
@interface YYTopBarView ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *btn_array;
@end
@implementation YYTopBarView
- (instancetype)initWithFrame:(CGRect)frame withButtons:(NSArray *)btn_array{
self = [super initWithFrame:frame];
if (self) {
self.btn_array = btn_array;
self.backgroundColor = [UIColor colorWithRed:228/255.0 green:11/255.0 blue:23/255.0 alpha:1];
[self initButton];
[self addSubview:self.scrollView];
[self addButtonToScrollView];
}
return self;
}
- (void)initButton{
self.leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.leftBtn.frame = CGRectMake(10, (kHEIGHT - 40) * 0.5, 40, 40);
[self.leftBtn setBackgroundImage:[UIImage imageNamed:@"2222"] forState:UIControlStateNormal];
[self addSubview:self.leftBtn];
self.rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.rightBtn.frame = CGRectMake(kWIDTH - 50, (kHEIGHT - 40) * 0.5, 40, 40);
[self.rightBtn setBackgroundImage:[UIImage imageNamed:@"1111"] forState:UIControlStateNormal];
[self addSubview:self.rightBtn];
}
-(UIScrollView *)scrollView{
if (!_scrollView) {
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.leftBtn.frame), 0, kWIDTH - self.leftBtn.frame.size.width - self.rightBtn.frame.size.width - 20, kHEIGHT)];
_scrollView.delegate = self;
_scrollView.showsHorizontalScrollIndicator = YES;//隐藏滚动条
_scrollView.pagingEnabled = YES;//设置分页
_scrollView.contentSize = CGSizeMake(kBtnWidth * self.btn_array.count, 0);//设置可滑尺寸
_scrollView.contentOffset = CGPointMake(0, 0);//设置初始偏移量
}
return _scrollView;
}
-(NSArray *)btn_array{
if (!_btn_array) {
_btn_array = [NSArray array];
}
return _btn_array;
}
- (void)addButtonToScrollView{
for (int i = 0; i < self.btn_array.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(kBtnWidth * i, (kHEIGHT - kBtnWidth) * 0.5, kBtnWidth, kBtnWidth);
[btn setTitle:self.btn_array[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.tag = 1000 + i;
[btn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:btn];
}
}
//可滑动的button的点击事件,可以自主根据button.tag加条件语句
- (void)didClickBtn:(UIButton *)button{
NSLog(@"点击了第%ld个可滑动的按钮",button.tag - 1000);
}
@end
//
// ViewController.m
// YYTopBarView
//
// Created by yyMae on 16/3/17.
// Copyright © 2016年 yyMae. All rights reserved.
//
#import "ViewController.h"
#import "YYTopBarView.h"
#define kWIDTH self.view.bounds.size.width
#define kHEIGHT self.view.bounds.size.height
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array = @[@"头条", @"订阅", @"广州", @"服装", @"雪松", @"公司", @"通宵", @"周五", @"晚上"];
self.view.backgroundColor = [UIColor whiteColor];
YYTopBarView *TBV = [[YYTopBarView alloc]initWithFrame:CGRectMake(0, 20, kWIDTH, 64) withButtons:array];
[self.view addSubview:TBV];
//给左右的button添加点击事件
[TBV.leftBtn addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
[TBV.rightBtn addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
}
- (void)leftAction{
NSLog(@"点击了左按钮");
}
- (void)rightAction{
NSLog(@"点击了右按钮");
}
@end