自定义导航控制器的titleView实现多控制器的切换

本文介绍如何通过自定义导航控制器的titleView实现多控制器的切换。主要涉及自定义titleView,内含按钮监听事件,设置scrollView内容大小以匹配控制器数量,将控制器视图添加到scrollView,管理子控制器,以及实现scrollView代理方法调整滑动条位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  在很多应用中都有通过自定义navigationItem的titleView,来实现多个控制器的切换管理.主要实现这个功能需要注意一下两个方面:

一,自定义titleView,里面包含两个按钮,监听点击事件,切换到哪个控制器

二,scrollView确定滚动的范围,有几个控制器就设置contentSize有多大

三,切换的控制器view,添加到scrollView上,使得在滚动的时候可以切换控制器

四,设置当前的控制的addChildViewController为要切换的控制器

五,实现scrollView的代理方法,当结束拖动的时候,重新给titleView的下面的滑动条设置位置

代码如下:

#import "ViewController.h"
#import "WTNavViewController.h"
#import "WTOneViewController.h"
#import "WTTwoViewController.h"
/**获取屏幕的宽度NSInteger*/
#define CurrentScreenWidth [UIScreen mainScreen].bounds.size.width
/**获取屏幕的高度NSInteger*/
#define CurrentScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIView *topBgView;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) WTTwoViewController *two;
@property (nonatomic,strong) WTOneViewController *one;

@end

@implementation ViewController
/**
 *  初始化控制器
 */
-(WTTwoViewController *)two
{
    if (_two == nil) {
        _two = [[WTTwoViewController alloc] init];
        _two.view.frame = CGRectMake(CurrentScreenWidth, 0, CurrentScreenWidth, CurrentScreenHeight);
        _two.view.clipsToBounds = YES;
        
    }
    return _two;
}
-(WTOneViewController *)one
{
    if (_one == nil) {
        _one = [[WTOneViewController alloc] init];
        _one.view.frame = CGRectMake(0, 0, CurrentScreenWidth, CurrentScreenHeight);
        _one.view.clipsToBounds = YES;
    }
    return _one;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //创建中间文字
    [self createTitleView];
    //创建scrollView
    [self createScrollView];
}
/**
 * 创建scrollView
 */
-(void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CurrentScreenWidth, CurrentScreenHeight )];
    self.scrollView.contentSize = CGSizeMake(CurrentScreenWidth * 2, CurrentScreenHeight);
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    [self.view addSubview:self.scrollView];
    
    self.scrollView.delegate = self;
    
    //注册控制器
    [self configSonViewController];
}
/**
 *  创建中间文字
 */
-(void)createTitleView
{
    self.topBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 26)];
    self.topBgView.userInteractionEnabled = YES;
    
    NSArray *titleArray = @[@"IOS",@"安卓"];
    int i = 0;
    for (NSString *title in titleArray) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * 53, 0, 58, 26);
        [btn setTitle:title forState:UIControlStateNormal];
        btn.tag = i;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        
        [btn addTarget:self action:@selector(titleViewSele:) forControlEvents:UIControlEventTouchUpInside];
        [self.topBgView addSubview:btn];
        
        i++;
    }
    
    UIView *indView = [[UIView alloc] initWithFrame:CGRectMake(14, 30, 30, 2)];
    indView.backgroundColor = [UIColor redColor];
    indView.tag = 99;
    [self.topBgView addSubview:indView];
    self.navigationItem.titleView = self.topBgView;
}
/**
 *  选择哪个控制器
 */
-(void)titleViewSele:(UIButton *)btn
{
    NSInteger ss = 0;
    switch (btn.tag) {
        case 0:
        {
            ss = 14;
            self.scrollView.contentOffset = CGPointMake(0, 0);
        }
            break;
        case 1:
        {
            ss = 65;
            self.scrollView.contentOffset = CGPointMake(CurrentScreenWidth, 0);
        }
            
        default:
            break;
    }
    UIView *indView = (UIView *)[_topBgView viewWithTag:99];
    CGRect inFrame = indView.frame;
    inFrame.origin.x = ss;
    [UIView animateWithDuration:0.2 animations:^{
        indView.frame = inFrame;
    }];
    
}
#pragma mark - 代理方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger ss = 0;
    if (self.scrollView.contentOffset.x == 0) {
        ss = 14;
    }else{
        ss = 65;
    }
    
    UIView *indView = (UIView *)[_topBgView viewWithTag:99];
    CGRect inFrame = indView.frame;
    inFrame.origin.x = ss;
    [UIView animateWithDuration:0.2 animations:^{
        indView.frame = inFrame;
    }];
}
/**
 *  注册控制器
 */
-(void)configSonViewController
{
    [self.scrollView addSubview:self.one.view];
    [self.scrollView addSubview:self.two.view];
    
    [self addChildViewController:self.one];
    [self addChildViewController:self.two];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值