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
相关推荐
陈皮话梅糖@17 小时前
iOS 集成ffmpeg
ios·ffmpeg
幽夜落雨17 小时前
ios老版本应用安装方法
ios
胖虎11 天前
实现 iOS 自定义高斯模糊文字效果的 UILabel(文末有Demo)
ios·高斯模糊文字·模糊文字
_可乐无糖2 天前
Appium 检查安装的驱动
android·ui·ios·appium·自动化
胖虎13 天前
iOS 网络请求: Alamofire 结合 ObjectMapper 实现自动解析
ios·alamofire·objectmapper·网络请求自动解析·数据自动解析模型
开发者如是说3 天前
破茧英语路:我的经验与自研软件
ios·创业·推广
假装自己很用心3 天前
iOS 内购接入StoreKit2 及低与iOS 15 版本StoreKit 1 兼容方案实现
ios·swift·storekit·storekit2
iOS阿玮3 天前
“小红书”海外版正式更名“ rednote”,突然爆红的背后带给开发者哪些思考?
ios·app·apple
刘小哈哈哈3 天前
iOS UIScrollView的一个特性
macos·ios·cocoa
忆江南的博客5 天前
iOS 性能优化:实战案例分享
ios