如图,导航标题栏是一个自定义view,通过点击手势来切换不同的控制器。首先,来看下这个自定义view的实现:
#import <UIKit/UIKit.h>
@protocol SegmentedViewDelegate <NSObject>
/**
* 自定义segment的点击代理方法
*
* @param index 选中按钮的index;
*/
- (void)selectAtIndex:(NSUInteger)index;
@end
@interface SegmentedView : UIImageView
@property(nonatomic,weak) id<SegmentedViewDelegate> delegate;
@end
#import "SegmentedView.h"
@interface SegmentedView () {
UIImageView *_leftView;
UIImageView *_rightView;
UILabel *_leftLabel;
UILabel *_rightLabel;
}
@end
@implementation SegmentedView
- (id)init {
if (self = [super init]) {
[self prepareUI];
}
return self;
}
- (void)selectIndex:(NSUInteger)index {
if (index == 0) {
_leftView.image = [UIImage imageNamed:@"ZXselect_left"];
_rightView.image = nil;
_leftLabel.textColor = [UIColor whiteColor];
_rightLabel.textColor = [UIColor blackColor];
_leftLabel.userInteractionEnabled = NO;
_rightLabel.userInteractionEnabled = YES;
}
else if (index == 1) {
_leftView.image = nil;
_rightView.image = [UIImage imageNamed:@"ZXselect_right"];
_leftLabel.textColor = [UIColor blackColor];
_rightLabel.textColor = [UIColor whiteColor];
_leftLabel.userInteractionEnabled = YES;
_rightLabel.userInteractionEnabled = NO;
}
}
- (void)prepareUI {
__weak SegmentedView *weakSelf = self;
self.userInteractionEnabled = YES;
self.image = [UIImage imageNamed:@"ZXselect_bg"];
_leftView = [UIImageView new];
_leftView.image = [UIImage imageNamed:@"ZXselect_left"];
[self addSubview:_leftView];
[_leftView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(weakSelf);
make.width.equalTo(weakSelf).multipliedBy(0.5);
make.height.equalTo(weakSelf);
}];
_rightView = [UIImageView new];
[self addSubview:_rightView];
[_rightView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.right.equalTo(weakSelf);
make.width.equalTo(weakSelf).multipliedBy(0.5);
make.height.equalTo(weakSelf);
}];
_leftLabel = [UILabel new];
_leftLabel.userInteractionEnabled = YES;
_leftLabel.text = @"资讯";
_leftLabel.textAlignment = NSTextAlignmentCenter;
_leftLabel.textColor = [UIColor whiteColor];
_leftLabel.font = [UIFont systemFontOfSize:14];
[self addSubview:_leftLabel];
[_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.right.equalTo(_leftView);
}];
UITapGestureRecognizer *tapL = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSegmentedView:)];
[_leftLabel addGestureRecognizer:tapL];
_rightLabel = [UILabel new];
_rightLabel.userInteractionEnabled = YES;
_rightLabel.text = @"消息";
_rightLabel.textAlignment = NSTextAlignmentCenter;
_rightLabel.textColor = [UIColor blackColor];
_rightLabel.font = [UIFont systemFontOfSize:14];
[self addSubview:_rightLabel];
[_rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.right.equalTo(_rightView);
}];
UITapGestureRecognizer *tapR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSegmentedView:)];
[_rightLabel addGestureRecognizer:tapR];
}
- (void)tapSegmentedView:(UITapGestureRecognizer *)tap {
NSInteger index;
if (tap.view == _leftView) {
index = 0;
}else{
index = 1;
}
[self selectIndex:index];
if (_delegate && [_delegate respondsToSelector:@selector(selectAtIndex:)]) {
[_delegate selectAtIndex:index];
}
}
在控制器中,实现代理方法。控制器.m文件:
#import "InformationController.h"
#import "SegmentedView.h"
#import "InfoChildController.h"
#import "MessageChildController.h"
#import "UITabBarController+HideTabBar.h"
@interface InformationController () <SegmentedViewDelegate> {
SegmentedView *_segmentedView;
InfoChildController *_infoChildController;
MessageChildController *_messageChildController;
}
@end
@implementation InformationController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.fd_prefersNavigationBarHidden = NO;
[self prepareNavigationBar];
[self prepareChildViewController];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tabBarController setTabBarHidden:NO myClass:self];
}
- (void)prepareNavigationBar {
self.navigationItem.leftBarButtonItem = nil;
_segmentedView = [[SegmentedView alloc] init];
_segmentedView.delegate = self;
_segmentedView.bounds = CGRectMake(0, 0, 210, 25);
self.navigationItem.titleView = _segmentedView;
}
- (void)prepareChildViewController {
_infoChildController = [[InfoChildController alloc] init];
[self addChildViewController:_infoChildController];
[self.view addSubview:_infoChildController.view];
_messageChildController = [[MessageChildController alloc] init];
[self addChildViewController:_messageChildController];
[self.view addSubview:_messageChildController.view];
_messageChildController.view.hidden = YES;
}
#pragma mark - SegmentedViewDelegate
- (void)selectAtIndex:(NSUInteger)index {
if (index == 0) {
_infoChildController.view.hidden = NO;
_messageChildController.view.hidden = YES;
}
else {
_infoChildController.view.hidden = YES;
_messageChildController.view.hidden = NO;
}
}
@end