iOS开发-UIScrollView嵌套tableView实现顶部tab横向切换

news2024/9/17 8:42:11

iOS开发-UIScrollView嵌套tableView实现顶部tab横向切换

通过ScollView嵌套两个TableView左右切换功能

在这里插入图片描述

在这里插入图片描述

一、UIScollView

UIScrollView可滚动控件,这里初始化需要设置_backScollView.pagingEnabled = YES;

代码如下

_backScollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
_backScollView.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
_backScollView.showsHorizontalScrollIndicator = NO;
_backScollView.showsVerticalScrollIndicator = NO;
_backScollView.userInteractionEnabled = YES;
_backScollView.pagingEnabled = YES;
_backScollView.exclusiveTouch = YES;
_backScollView.scrollEnabled = YES;
_backScollView.tag = kWBackScrollViewTAG;
_backScollView.bounces = NO;
[self addSubview:_backScollView];

完整代码

SDAppWCourseZoneView.h

#import <UIKit/UIKit.h>
#import "UIColor+Addition.h"
#import "SDBaseControllerView.h"
#import "SDAppWCourseNavbarView.h"

#define kWBackScrollViewTAG 1121

@interface SDAppWCourseZoneView : SDBaseControllerView

@property (nonatomic, strong) UIScrollView *backScollView;

@property (nonatomic, strong) SDAppWCourseNavbarView *navbarView;

@property (nonatomic, weak) id delegate;

@end

SDAppWCourseZoneView.m

#import "SDAppWCourseZoneView.h"

static CGFloat kBtnSize = 50.0;
static CGFloat kVPadding = 25.0;
static CGFloat kHPadding = 20.0;

@interface SDAppWCourseZoneView ()<JXCategoryViewDelegate>

@end

@implementation SDAppWCourseZoneView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
        _backScollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _backScollView.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
        _backScollView.showsHorizontalScrollIndicator = NO;
        _backScollView.showsVerticalScrollIndicator = NO;
        _backScollView.userInteractionEnabled = YES;
        _backScollView.pagingEnabled = YES;
        _backScollView.exclusiveTouch = YES;
        _backScollView.scrollEnabled = YES;
        _backScollView.tag = kWBackScrollViewTAG;
        _backScollView.bounces = NO;
        [self addSubview:_backScollView];
        
        _navbarView = [[SDAppWCourseNavbarView alloc] initWithFrame:CGRectZero];
        _navbarView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_navbarView];
        // 联动(categoryView和pagingView)
        _navbarView.categoryView.contentScrollView = _backScollView;
        // 返回上一页侧滑手势(仅在index==0时有效)
        
        [self bringSubviewToFront:self.navigationBar];
        self.navigationHiden = YES;
        
        [self layoutAllContentViews];
    }
    return self;
}

- (void)layoutAllContentViews {
    CGFloat navbarHeight = [SDAppWCourseNavbarView navbarHeight];
    self.navbarView.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(self.bounds), navbarHeight);
    self.backScollView.frame = CGRectMake(0.0, CGRectGetMaxY(self.navbarView.frame), CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) - CGRectGetMaxY(self.navbarView.frame) - 49.0 - [SDBaseView baseSafeEdgeInsets].bottom);
}

- (void)setDelegate:(id)delegate {
    _delegate = delegate;
    _backScollView.delegate = delegate;
    self.navbarView.categoryView.delegate = self;
}

- (void)dealloc {
    
}

@end

二、分类标题进行切换tab

在切换tab时,使用JXCategoryTitleView

SDAppWCourseNavbarView.h

#import "SDBaseView.h"
#import "JXCategoryView.h"

@interface SDAppWCourseNavbarView : SDBaseView

@property (nonatomic, strong) JXCategoryTitleView *categoryView;

+ (CGFloat)navbarHeight;

@end

SDAppWCourseNavbarView.m

#import "SDAppWCourseNavbarView.h"

static CGFloat kNavbarHeight = 60.0;

static CGFloat kCategoryHeight = 44.0;

@interface SDAppWCourseNavbarView ()

@property (nonatomic, strong) UIImageView *bgImageView;

@end

@implementation SDAppWCourseNavbarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.bgImageView];
        [self.bgImageView addSubview:self.categoryView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.bgImageView.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
    self.categoryView.frame = CGRectMake(0.0, kStatusBarHeight + (kNavbarHeight - kCategoryHeight)/2.0, kScreenWidth*3.0/5.0, kCategoryHeight);
}

+ (CGFloat)navbarHeight {
    return kStatusBarHeight + kNavbarHeight;
}

#pragma mark - LAZY
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _bgImageView.backgroundColor = [UIColor whiteColor];
        _bgImageView.userInteractionEnabled = YES;
    }
    return _bgImageView;
}

/**
 菜单项视图View
 */
-(JXCategoryTitleView *)categoryView{
    if(!_categoryView){
        //
        _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth*3.0/5.0, kCategoryHeight)];
        // 设置菜单项标题数组
        _categoryView.titles = [NSMutableArray arrayWithArray:@[@"我的课程", @"挑选课程"]];;
        // 背景色
        _categoryView.backgroundColor = [UIColor whiteColor];
        // 标题色、标题选中色、标题字体、标题选中字体
        _categoryView.titleColor = [UIColor colorWithHexString:@"666666"];
        _categoryView.titleSelectedColor = [UIColor colorWithHexString:@"f78342"];
        _categoryView.titleFont = SDMediumFontSize(15);
        // 标题色是否渐变过渡
        _categoryView.titleColorGradientEnabled = YES;
        _categoryView.titleLabelZoomEnabled = YES;
        _categoryView.titleLabelZoomScale = 1.2;
        _categoryView.titleLabelAnchorPointStyle = JXCategoryTitleLabelAnchorPointStyleBottom;
        
        // 下划线
        JXCategoryIndicatorImageView *lineView = [[JXCategoryIndicatorImageView alloc] init];
        // 下划线颜色
        lineView.indicatorImageView.image = [UIImage imageNamed:@"img_line_bttom"];
        // 下划线宽度
        lineView.indicatorImageViewRollEnabled = NO;
        lineView.indicatorImageViewSize = CGSizeMake(40, 10);
        _categoryView.indicators = @[lineView];
    }
    
    return _categoryView;
}

@end

三、为UIScrollView添加子控件

UIScrollView添加子控件,Controller添加SubController

SDAppWCourseZoneViewController.h

#import "SDBaseViewController.h"

@interface SDAppWCourseZoneViewController : SDBaseViewController

@end

SDAppWCourseZoneViewController.m

#import "SDAppWCourseZoneViewController.h"
#import "SDAppWCourseZoneView.h"
#import "SDAppStudyViewController.h"
#import "SDAppMyCoursePageViewController.h"

@interface SDAppWCourseZoneViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) SDAppWCourseZoneView *wcourseView;

@property (nonatomic, strong) SDBaseViewController *currentPageController;

@end

@implementation SDAppWCourseZoneViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}

#pragma mark - Configure NavigationBar
- (void)configureNavigationBar {
    SDNavButtonItem *leftButtonItem = [[SDNavButtonItem alloc] initWithTitle:nil image:[UIImage imageNamed:@"ic_nav_back_gray"] target:self action:@selector(leftNavBarButtonAction)];
        
    self.wcourseView.navigationBar.navTitleView = [[SDNavigationTitleView alloc] initWidthTitle:@"消息&通知" subView:nil];
    self.wcourseView.navigationBar.leftNavItem = leftButtonItem;
    self.wcourseView.navigationBar.hidenNavShadow = YES;
}

- (void)leftNavBarButtonAction {
    if (self.routerPresent) {
        [self dismissViewControllerAnimated:YES completion:^{
            // completion
        }];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark - loadView
- (void)loadView {
    [super loadView];
    _wcourseView = [[SDAppWCourseZoneView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _wcourseView.delegate = self;
    self.view = _wcourseView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configureNavigationBar];
    
    [self addColumnController];
    self.currentPageController = self.childViewControllers[0];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}


#pragma mark - 添加子控制器
- (void)addColumnController {
    CGFloat contentWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
    CGFloat contentHeight = CGRectGetHeight([UIScreen mainScreen].bounds) - CGRectGetMaxY(self.wcourseView.navbarView.categoryView.frame) - 49.0 - [SDBaseView baseSafeEdgeInsets].bottom - 20.0;

    SDAppMyCoursePageViewController *pageVC = [[SDAppMyCoursePageViewController alloc] init];
    [self addChildViewController:pageVC];
    pageVC.view.frame = CGRectMake(0*contentWidth, 0.0, contentWidth, contentHeight);
    [self.wcourseView.backScollView addSubview:pageVC.view];

    SDAppStudyViewController *shopPageVC = [[SDAppStudyViewController alloc] init];
    [self addChildViewController:shopPageVC];
    shopPageVC.view.frame = CGRectMake(1*contentWidth, 0.0, contentWidth, contentHeight);
    [self.wcourseView.backScollView addSubview:shopPageVC.view];
    
    self.wcourseView.backScollView.contentSize = CGSizeMake(contentWidth*self.wcourseView.navbarView.categoryView.titles.count, 0.0);
}

#pragma mark - UIScrollViewDelegate
- (void)segmentButtonDidAction:(NSInteger)segmentIndex {
    [self scrollTableViewPage:segmentIndex];
    
    CGRect contentRect = [UIScreen mainScreen].bounds;
    CGFloat contentWidth = CGRectGetWidth(contentRect);
    CGPoint point = CGPointMake(contentWidth*segmentIndex, 0);
    [self.wcourseView.backScollView setContentOffset:point animated:NO];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.tag == kWBackScrollViewTAG) {
        
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.tag == kWBackScrollViewTAG) {
        
    }
}

#pragma mark - scrollTableViewPage
- (void)scrollTableViewPage:(NSInteger)page {
    if ([self.currentPageController isKindOfClass:[SDAppMyCoursePageViewController class]]) {
        SDAppMyCoursePageViewController *myCourseVC = (SDAppMyCoursePageViewController *)self.currentPageController;
        [myCourseVC scrollToTop:YES];

    } else if ([self.currentPageController isKindOfClass:[SDAppStudyViewController class]]) {
        SDAppStudyViewController *remCourseVC = (SDAppStudyViewController *)self.currentPageController;
        [remCourseVC scrollToTop:YES];
    }

    self.currentPageController = self.childViewControllers[page];

    if ([self.currentPageController isKindOfClass:[SDAppMyCoursePageViewController class]]) {
        SDAppMyCoursePageViewController *myCourseVC = (SDAppMyCoursePageViewController *)self.currentPageController;
        [myCourseVC scrollToTop:NO];
        
    } else if ([self.currentPageController isKindOfClass:[SDAppStudyViewController class]]) {
        SDAppStudyViewController *remCourseVC = (SDAppStudyViewController *)self.currentPageController;
        [remCourseVC scrollToTop:NO];
    }
    
    [self.currentPageController reloadData];
}

- (void)switchContentPage:(NSInteger)page {
    [self scrollTableViewPage:page];
    
    CGRect contentRect = [UIScreen mainScreen].bounds;
    CGFloat contentWidth = CGRectGetWidth(contentRect);
    CGPoint point = CGPointMake(contentWidth*page, 0);
    [self.wcourseView.backScollView setContentOffset:point animated:NO];
    
    if ([self.currentPageController isKindOfClass:[SDAppStudyViewController class]]) {
        SDAppStudyViewController *shopVC = (SDAppStudyViewController *)self.currentPageController;
        [shopVC reloadData];
    }
}

#pragma mark - JXCategoryViewDelegate
/**
 选中菜单项后调用

 @param categoryView 菜单项View
 @param index 下表
 */
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    [self switchContentPage:index];
}

/**
 滑动并切换内容视图后调用

 @param categoryView 菜单项View
 @param index 下表
 */
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index{
    [self switchContentPage:index];
}


#pragma mark - UIScrollViewDelegate

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

四、小结

iOS开发-UIScrollView嵌套tableView实现顶部tab横向切换.
通过ScollView嵌套两个TableView左右切换功能。

学习记录,每天不停进步。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/822267.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ARP协议请求

文章目录 作用请求与应答流程数据包ARP协议以太网帧协议具体应用 作用 通过 IP地址 查找 MAC地址。 请求与应答流程 A&#xff1a;数据发送主机 B&#xff1a;目标主机 目前只知道目标主机IP地址&#xff0c;想把数据发送过去&#xff0c;需要查询到目标主机的MAC地址&#x…

构建基于大模型的Autonomous Agents案例(一)

构建基于大模型的Autonomous Agents案例 1.1 Autonomous Agents原理机制 在本节中&#xff0c;我们将聚焦于LangChain上的自治代理&#xff08;Autonomous Agents on LangChain&#xff09;。自治代理是当前业界最热门的话题之一&#xff0c;特别是在企业级应用中。当然&#x…

关于综合能源智慧管理系统的架构及模式规划的研究

安科瑞 华楠 摘 要&#xff1a;探讨了国内外能源互联网的研究发展&#xff0c;分析了有关综合智慧能源管理系统的定位&#xff0c;以及系统的主要特点&#xff0c;研究了综合智慧能源管理系统的构架以及模式规划。 关键词&#xff1a;综合能源&#xff1b;智慧管理系统&#…

MySql操作进阶

目录 1.多表联查 1.1内连接 1.2左连接和右连接 1.3自连接 2.子查询 3.合并查询 1.多表联查 多表联查实际上就是对多张表中的数据合并在一起进行查询&#xff0c;具体合并多张表中的数据方式为&#xff1a;取笛卡尔积的方式进行合并。 但仅是以笛卡尔积的方式合并表较为…

网络安全进阶学习第九课——SQL注入介绍

文章目录 一、什么是注入二、什么是SQL注入三、SQL注入产生的原因四、SQL注入的危害五、SQL注入在渗透中的利用1、绕过登录验证&#xff1a;使用万能密码登录网站后台等。2、获取敏感数据3、文件系统操作4、注册表操作5、执行系统命令 六、如何挖掘SQL注入1、SQL注入漏洞分类按…

无涯教程-Lua - Arrays(数组)

数组是对象的有序排列&#xff0c;可以是包含行集合的一维数组&#xff0c;也可以是包含多行和多列的多维数组。 在Lua中&#xff0c;数组是使用带有整数的索引表实现的。数组的大小不是固定的&#xff0c;并且可以根据无涯教程的要求(取决于内存限制)来增长。 一维数组 一维…

WEB应用程序数据库防守篇之预防SQL漏洞注入

前言: 国内目前几乎市面上所有的web应用程序都使用关系型数据库来存储用户的数据信息&#xff0c;可以这么说&#xff0c;数据库是一个项目&#xff0c;一个互联网公司乃至国家最重要的数据中心。如果数据库被入侵成功&#xff0c;所引发的后果将是灾难性&#xff0c;无法想象…

JavaSE 【类和对象】(3)(重点:代码块、对象的打印)

目录 一、代码块 1.静态代码块 2.构造代码块/实例代码块 3.总结 代码块执行的顺序&#xff1a; 1.静态代码块 2.构造代码块&#xff08;实例代码块&#xff09; 3.执行对应的构造方法 其中&#xff1a;静态的只执行一次 二、 对象的打印 有一个快速打印的方法&#x…

scrapy框架简单实现豆瓣评分爬取案例

豆瓣网址&#xff1a;https://movie.douban.com/top250 1.创建scrapy框架 scrapy startproject 项目名(scrapy_test_one)创建好以后的目录是这样的 2.创建spider文件 在spiders目录下创建一个spider_one.py文件&#xff0c;可以随意命名&#xff0c;该文件主要是让我们进行数…

MaxPatrol SIEM 增加了一套检测供应链攻击的专业技术

我们为 MaxPatrol SIEM 信息安全事件监控系统增加了一套新的专业技术。 该产品可帮助企业防范与供应链攻击相关的威胁。 此类攻击正成为攻击者的首要目标&#xff1a;它们以软件开发商和供应商为目标&#xff0c;网络犯罪分子通过他们的产品进入最终目标的基础设施。 因此&a…

【数据结构】——线性表的相关习题

目录 题型一&#xff08;顺序表的存储结构&#xff09;题型二&#xff08;链表的判空&#xff09;题型三&#xff08;单链表的建立&#xff09;题型四&#xff08;顺序表、单链表的插入删除&#xff09; 题型一&#xff08;顺序表的存储结构&#xff09; 1、线性表的顺序存储结…

Framework开发的热度从未下降~

近几日&#xff0c;统计局也发布了就业相关数据&#xff0c;全国失业青年达600多万&#xff0c;面对此数据&#xff0c;我们能想到的是实际的失业人数肯定会比公布的数据要多很多&#xff0c;尤其是表示 “一周工作一小时以上” 也纳入了就业范围。 反观有一批Android开发在如此…

JavaWeb之HTML基础篇(一)

系列文章目录 HTML基础篇&#xff08;一&#xff09; 文章目录 系列文章目录HTML基础篇&#xff08;一&#xff09;[TOC](文章目录) 前言一、HTML简介1.1介绍1.2HTML文件的书写规范1.3 HTML标签介绍1.4 HTML常见的标签 二、CSS的简介2.1css技术介绍2.2 CSS与HTML结合的三种方式…

org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter-若依二开报错(已解决)

阿丹-问题描述&#xff1a; 今天在复制已经拆解过后的若依system模块删除掉中间的一些东西之后&#xff0c;出现了一个问题。已启动就报错。报错截图如下。 问题解析&#xff1a; 身边不少二开若依的小伙伴都遇到了这个问题。 这个异常是因为在启动应用程序时&#xff0c;Sprin…

杜教筛的小结

总所周知&#xff0c;杜教筛是一个可以快速求积性函数前缀和的工具&#xff0c;为了快速理解杜教筛&#xff0c;自己给自己写了一个文章快速理解。 它可以在O(n2/3)的复杂度快速求出某个积性函数的前缀和。 例如&#xff0c;我们想要知道 f f f函数的前缀和&#xff0c;我们可…

selenium官网文档阅读总结(day 2)

1.selenium元素定位方法 1.1selenium命令 当我们使用chormdriver打开网页后&#xff0c;接下来就要用python操作元素&#xff0c;模拟用户会作出的操作&#xff0c;这些操作元素的方法就是命令。比如 (1) click&#xff1a;点击&#xff08;按钮&#xff0c;单选框&#xff…

STM32(HAL)多串口进行重定向(printf函数发送数据)

目录 1、简介 2.1 基础配置 2.1.1 SYS配置 2.1.2 RCC配置 2.2 串口外设配置 2.3 项目生成 3、KEIL端程序整合 4、效果测试 1、简介 在HAL库中&#xff0c;常用的printf函数是无法使用的。本文通过重映射实现在HAL库多个串口可进行类似printf函数的操作。 2.1 基础配置 2.…

《向量数据库指南》——腾讯云向量数据库Tencent Cloud Vector DB正式上线公测!提供10亿级向量检索能力

8月1日,腾讯云向量数据库(Tencent Cloud Vector DB)已正式上线公测。在腾讯云官网上搜索“向量数据库”,就可以正式体验该产品。 腾讯云向量数据库不仅能为大模型提供外部知识库,提高大模型回答的准确性,还可广泛应用于推荐系统、文本图像检索、自然语言处理等 AI 领域。…

【ARM Coresight 系列文章 2.3 - Coresight 寄存器】

文章目录 Coresight 寄存器介绍1.1 ITCTRL&#xff0c;integration mode control register1.2 CLAIM寄存器1.3 DEVAFF(Device Affinity Registers)1.4 LSR and LAR1.5 AUTHSTATUS(Authentication Status Register) Coresight 寄存器介绍 Coresight 对于每个 coresight 组件&am…

Python 进阶(七):高级文件操作(shutil 模块)

❤️ 博客主页&#xff1a;水滴技术 &#x1f338; 订阅专栏&#xff1a;Python 入门核心技术 &#x1f680; 支持水滴&#xff1a;点赞&#x1f44d; 收藏⭐ 留言&#x1f4ac; 文章目录 1. 简介2. 常用函数2.1 复制文件2.2 复制目录2.3 移动文件或目录2.4 删除文件或目录2.…