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

ios 用JXCategoryView 库实现tab滑动切换viewController_第1张图片

先Pod导入安装

pod 'JXCategoryView'

.m文件

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

#import "OrderViewController.h"
#import "NavigationBarUtils.h"
#import 

#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 

NS_ASSUME_NONNULL_BEGIN

@interface OrderViewController : UIViewController 

@end

NS_ASSUME_NONNULL_END

你可能感兴趣的:(ios)