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
相关推荐
1024小神9 小时前
在swiftui中使用实现类似微信右上角的功能菜单
ios
北京自在科技10 小时前
iOS 18.4修复多个核心安全漏洞,间接增强Find My服务的数据保护能力
科技·ios·iphone·蓝牙·find my·北京自在科技
货拉拉技术14 小时前
iOS货运用户App组件路由器设计与实践
ios·架构
布多14 小时前
iOS分类中的关联对象:如何用运行时突破Category的存储限制
ios·面试·源码
鸿蒙布道师1 天前
鸿蒙NEXT开发数值工具类(TS)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
程序员老刘1 天前
谨慎升级macOS 15.4,规避 ITMS-90048 错误
flutter·macos·ios
90后的晨仔1 天前
iOS 蓝牙开发基础知识梳理
ios
一牛1 天前
Metal 进阶:读取可绘制对象的像素
ios·swift·计算机图形学
90后的晨仔1 天前
什么是WebSocket ?ios 中如何使用?
ios
鸿蒙布道师2 天前
鸿蒙NEXT开发日期工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei