前言
有時候系統(tǒng)自動的導(dǎo)航欄往往不能滿足項目要求,如上圖,導(dǎo)航欄上需要左右兩個按鈕,并且中間的按鈕是可以滑動的,這就需要我們自己封裝了.
上效果圖
上代碼
YYTopBarView是封裝的導(dǎo)航欄,在控制器調(diào)接口即可
//
// 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;//設(shè)置分頁
_scrollView.contentSize = CGSizeMake(kBtnWidth * self.btn_array.count, 0);//設(shè)置可滑尺寸
_scrollView.contentOffset = CGPointMake(0, 0);//設(shè)置初始偏移量
}
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的點擊事件,可以自主根據(jù)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