您的位置:首页 > 汽车 > 时评 > ios 用JXCategoryView 库实现tab滑动切换viewController

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

2024/11/13 9:40:40 来源:https://blog.csdn.net/WenZhengshi/article/details/142174772  浏览:    关键词: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];// 设置子视图控制器的 framevc.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>@endNS_ASSUME_NONNULL_END

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com