滚动视图的导航条 , 加号点击《思路版》

本文介绍如何使用XLSlideSwitch库在iOS应用中创建滑动切换的界面,包括导入库文件、创建对应的ViewController类、配置滚动视图及按钮交互等步骤。

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

效果图
导入 XLSlideSwitch 文件
推荐 , 热门 , 搞笑 等界面需要创建相对应的ViewController
如图 我有6个界面 所以就需要创建六个继承于ViewController的类
接下来 导入我们的第三方文件 ( XLSlideSwitch文件 )
在ViewController.m界面导入头文件

#import "ViewController.h"
#import "XLSlideSwitch.h"
***!!!!!以下是六个类的名字 , 这只是我起的名字  你需要更改一下!!!!!***
#import "YiDongTongXunViewController.h"
#import "ChuanMeiViewController.h"
#import "RuanGongViewController.h"
#import "WangGongViewController.h"
#import "YunJiSuanViewController.h"
#import "JianZhuViewController.h"

在 **@interface ViewController ()**后面写协议和定义一些需要的东西

@interface ViewController ()<XLSlideSwitchDelegate>
{
    UIView *SomeView;   ///是点击加号按钮出现的View
    UIButton *Btn;   ///按钮
}
@property (nonatomic , strong)XLSlideSwitch *ScrollView;///滚动视图

@end

接下来是ViewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    ///创建一个数组用来存储名字
    NSArray *TitlesArr = @[@"推荐" , @"热门" ,@"搞笑" , @"军事" ,  @"社会" , @"音乐"];
    !!!!!用来存储六个类的名字   需要更改!!!!!!
        NSArray *ControllersArr = @[@"YiDongTongXunViewController" , @"ChuanMeiViewController" , @"RuanGongViewController" , @"WangGongViewController" , @"YunJiSuanViewController" , @"JianZhuViewController"];
    NSMutableArray *ViewControllers = [[NSMutableArray alloc] init];
    for (int i = 0 ; i < TitlesArr.count; i ++) {
        //字符串创建控制器
        UIViewController *VC = [[NSClassFromString(ControllersArr[i])alloc] init];
        [ViewControllers addObject:VC];
    }
    //滚动视图
     _ScrollView = [[XLSlideSwitch alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) Titles:TitlesArr viewControllers:ViewControllers];
    _ScrollView.delegate = self;
    _ScrollView.itemNormalColor = [UIColor darkGrayColor];
    _ScrollView.itemSelectedColor = self.navigationController.navigationBar.tintColor;
    _ScrollView.customTitleSpacing = 30;
    _ScrollView.moreButton = [self moreButton];
    [_ScrollView showInViewController:self];
//    [_ScrollView showInNavigationController:self];
    //View
    SomeView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 150, 104, 140, 200)];
    SomeView.backgroundColor = [UIColor orangeColor];
    
    for (int i = 0 ; i < 1; i ++) {
        [self.view addSubview:SomeView];
        SomeView.hidden = YES;
    }
}

添加按钮的方法

- (UIButton *)moreButton{
    Btn = [[UIButton alloc] init];
//    [button setImage:[UIImage imageNamed:@"channelAdd"] forState:UIControlStateNormal];
    [Btn setTitle:@"➕" forState:UIControlStateNormal];
    [Btn setImageEdgeInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
    [Btn addTarget:self action:@selector(BtnTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    return Btn;
}

按钮的点击方法

- (void)BtnTouchUpInside{
//    NSLog(@"点击了添加按钮");
    if (Btn.selected == YES) {
//        SomeView.hidden = YES;
        Btn.selected = NO;
        SomeView.hidden = YES;
    }else{
        Btn.selected = YES;
        SomeView.hidden = NO;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值