如圖,導航標題欄是一個自定義view苏章,通過點擊手勢來切換不同的控制器寂嘉。首先,來看下這個自定義view的實現(xiàn):
#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];
}
}
在控制器中枫绅,實現(xiàn)代理方法泉孩。控制器.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