当前位置: 首页 > article >正文

ios 用JXCategoryView 库实现tab滑动切换viewController

先Pod导入安装

pod 'JXCategoryView'

.m文件

//
//  OrderViewController.m
//  scxhgh2
//
//  Created by xmkjsoft on 2024/9/9.
//

#import "OrderViewController.h"
#import "NavigationBarUtils.h"
#import <JXCategoryView/JXCategoryView.h>

#import "AllOrderViewController.h"
#import "WaitingPaymentViewController.h"
#import "WaitingShipmentViewController.h"
#import "WaitingReceiptViewController.h"
#import "RefundAndAfterSaleViewController.h"

@interface OrderViewController ()



@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  self.title=@"我的订单";
  self.view.backgroundColor=[UIColor whiteColor];
  
  
  #pragma mark -标题栏
  [NavigationBarUtils setupNavigationBarStyleForViewController:self];
  #pragma mark -返回键
  [NavigationBarUtils setupCustomBackButtonForViewController:self action:@selector(customBackAction)];
  
  
  
     JXCategoryTitleView *categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 50)];
     categoryView.titles = @[@"全部", @"待付款", @"待发货", @"待收货", @"退款/售后"];
     categoryView.titleSelectedColor = [UIColor orangeColor];
     categoryView.titleColor = [UIColor grayColor];
     categoryView.titleFont = [UIFont systemFontOfSize:15];
     categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:16];
     
     JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
     lineView.indicatorColor = [UIColor orangeColor];
     lineView.indicatorWidth = JXCategoryViewAutomaticDimension;
     categoryView.indicators = @[lineView];
     
     [self.view addSubview:categoryView];
  
     
    // 假设你有五个自定义的 ViewController 类,如 AllViewController, WaitingPaymentViewController 等
    NSArray *viewControllers = @[
        [[AllOrderViewController alloc] init],
        [[WaitingPaymentViewController alloc] init],
        [[WaitingShipmentViewController alloc] init],
        [[WaitingReceiptViewController alloc] init],
        [[RefundAndAfterSaleViewController alloc] init]
    ];

    // 创建一个 UIScrollView 来管理页面内容的滑动
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height - 50)];
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 5, self.view.frame.size.height - 50);
    scrollView.delegate = self;

    for (int i = 0; i < viewControllers.count; i++) {
        UIViewController *vc = viewControllers[i];
        
        // 设置子视图控制器的 frame
        vc.view.frame = CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height - 50);
        
        // 将子视图控制器的 view 添加到 scrollView 中
        [scrollView addSubview:vc.view];
        
        // 将子视图控制器添加到当前视图控制器中,以便管理生命周期
        [self addChildViewController:vc];
    }

    [self.view addSubview:scrollView];

    // 设置 JXCategoryTitleView 的内容滚动视图
    categoryView.contentScrollView = scrollView;

     

}

- (void)customBackAction {
    if (self.navigationController && self.navigationController.viewControllers.count > 1) {
        // 如果有导航控制器并且当前控制器不是根视图控制器,则使用 pop 返回上一页
        [self.navigationController popViewControllerAnimated:YES];
    } else if (self.presentingViewController) {
        // 如果当前视图控制器是以模态形式呈现的,则使用 dismiss 关闭页面
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}



@end

 .h文件

//
//  OrderViewController.h
//  scxhgh2
//
//  Created by xmkjsoft on 2024/9/9.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface OrderViewController : UIViewController <UIScrollViewDelegate>

@end

NS_ASSUME_NONNULL_END


http://www.kler.cn/news/306293.html

相关文章:

  • 破解AI生成检测:如何用ChatGPT降低论文的AIGC率
  • Redis Universe: 探索无边界的数据处理星系
  • 网络安全 day6 --- 抓包技术HTTPS协议小程序PC应用WEB转发联动
  • Spring Boot-分布式系统问题
  • 运算符学习
  • Selenium元素定位:深入探索与实践
  • 【Java EE】线程安全问题的原因与解决方案
  • 滚雪球学SpringCloud[3.2讲]:Hystrix:熔断与降级详解
  • 基于JDK1.8和Maven的GeoTools 28.X源码自主构建实践
  • Python基础语法(3)下
  • 计算机毕业设计 网上体育商城系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试
  • 实验一 番外篇 虚拟机联网与DHCP服务器
  • 实战千问2大模型第三天——Qwen2-VL-7B(多模态)视频检测和批处理代码测试
  • 【UI】element ui table(表格)expand实现点击一行展开功能
  • Blue Screen of Death(BSOD)
  • Presto
  • 使用容器技术快速入门MinIO
  • Python 中 Locale.Error: Unsupported Locale Setting 错误
  • iCAM06: A refined image appearance model for HDR image rendering
  • 分享Vue3.5最新变化
  • C++高性能线性代数库Armadillo入门
  • 【算法专题】穷举vs暴搜vs深搜vs回溯vs剪枝
  • [Linux]:进程间通信(上)
  • 【重学 MySQL】二十九、函数的理解
  • 通过Docker实现openGauss的快速容器化安装
  • 基于Keil软件实现修改主频(江协科技HAL库)
  • STM32的IAP原理及其操作流程分析
  • C++20中支持的非类型模板参数
  • QT多线程编程(基础概念以及示例)
  • 【深度学习】搞懂卷积神经网络(一)