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
相关推荐
恋猫de小郭9 小时前
什么?Flutter 可能会被 SwiftUI/ArkUI 化?全新的 Flutter Roadmap
flutter·ios·swiftui
网安墨雨13 小时前
iOS应用网络安全之HTTPS
web安全·ios·https
福大大架构师每日一题15 小时前
37.1 prometheus管理接口源码讲解
ios·iphone·prometheus
BangRaJun1 天前
LNCollectionView-替换幂率流体
算法·ios·设计
刘小哈哈哈1 天前
iOS 多个输入框弹出键盘处理
macos·ios·cocoa
靴子学长1 天前
iOS + watchOS Tourism App(含源码可简单复现)
mysql·ios·swiftui
一如初夏丿2 天前
xcode15 报错 does not contain ‘libarclite‘
ios·xcode
杨武博2 天前
ios 混合开发应用白屏问题
ios
BangRaJun2 天前
LNCollectionView
android·ios·objective-c
二流小码农3 天前
鸿蒙元服务项目实战:终结篇之备忘录搜索功能实现
android·ios·harmonyos