尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

SwipeViewController核心API完全指南:pages、startIndex等10个关键属性一网打尽

SwipeViewController核心API完全指南:pages、startIndex等10个关键属性一网打尽 SwipeViewController核心API完全指南pages、startIndex等10个关键属性一网打尽【免费下载链接】SwipeViewControllerSwipeViewController is a Swift modification of RKSwipeBetweenViewControllers - navigate between pages / ViewControllers项目地址: https://gitcode.com/gh_mirrors/sw/SwipeViewControllerSwipeViewController 是一个用 Swift 重写的 iOS 滑动翻页组件源于 Objective-C 项目 RKSwipeBetweenViewControllers用于在多个页面 / ViewController 之间平滑滑动导航。它是 UIPageViewController 与 UINavigationController 的结合体顶部自动生成可点击的滑动按钮底部带跟随滑动的选中指示条。本篇文章面向 Swift 新手用最少的代码带你一次搞懂 SwipeViewController 核心 API 中最常用的 10 个关键属性从此配置翻页界面不再迷茫。为什么选择 SwipeViewController 做页面导航在 iOS 开发中实现顶部标签 左右滑动翻页是最常见的需求之一比如消息、联系人、动态这类多 Tab 页面。SwipeViewController 核心 API 设计得非常轻量你只需要传入页面数组再通过几个属性就能完成全部定制无需手写 UIPageViewController 的 DataSource 和 Delegate。它的实现集中在两个源文件核心类 SwipeViewController.swift约 480 行和辅助结构 SwipeButtonWithImage.swift整个库只有两个文件非常便于学习和改造。第一步pages 属性——构建翻页页面的基础pages 是 SwipeViewController 核心 API 中最重要的属性它是一个[UIViewController]数组用来存放所有参与翻页的控制器。它只能在初始化时通过构造方法传入let firstVC UIViewController() let secondVC UIViewController() let swipeVC SwipeViewController(pages: [firstVC, secondVC])需要注意的是pages 是private(set)只读属性运行期间无法直接增删页面。每个页面的title会自动成为顶部按钮的文字所以记得先设置标题再传入。第二步startIndex 属性——指定默认显示的起始页startIndex 用于设置应用打开时默认展示第几页索引从 0 开始。比如希望默认显示第二个页面只需一行swipeVC.startIndex 1设置后组件会自动把对应按钮标记为选中态并同步当前页的背景色。这个属性在上次浏览位置记忆默认落地页等场景非常实用。第三步定制选中指示条——selectionBarHeight、selectionBarWidth、selectionBarColor选中条Selection Bar是 SwipeViewController 的标志性视觉元素它像一条游标跟随手指滑动在按钮下方移动让用户随时知道自己在第几页。三个核心 API 属性各司其职selectionBarHeight指示条高度如 3selectionBarWidth指示条宽度如 80selectionBarColor指示条颜色如主题蓝swipeVC.selectionBarWidth 80 swipeVC.selectionBarHeight 3 swipeVC.selectionBarColor UIColor(red: 0.23, green: 0.55, blue: 0.92, alpha: 1.0)官方示例 AppDelegate.swift 中就是这么配置的效果是顶部三个按钮加一条跟随滑动的蓝色游标。第四步按钮三件套——buttonFont、buttonColor、selectedButtonColor顶部按钮的文字样式由三个属性控制它们是定制外观时最高频的组合buttonFont按钮文字字体默认 18 号系统字体buttonColor未选中按钮的颜色默认黑色selectedButtonColor当前选中按钮的颜色默认绿色swipeVC.buttonFont UIFont.boldSystemFont(ofSize: 16) swipeVC.buttonColor .gray swipeVC.selectedButtonColor .blue在滑动过程中按钮颜色会随手指位置实时渐变切换交互反馈非常细腻这段逻辑位于 SwipeViewController.swift 的changeButtonColor方法中。第五步导航栏定制——navigationBarColor 与左右 BarButtonItemSwipeViewController 继承了 UINavigationController因此导航栏也可以一键换色swipeVC.navigationBarColor UIColor.blue同时它还暴露了 leftBarButtonItem 和 rightBarButtonItem 两个属性让你像使用普通导航栏一样添加添加设置等按钮let barButton UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(push)) swipeVC.leftBarButtonItem barButton注意这两个属性建议在 viewDidLoad 中视图出现之前设置示例见 ViewController.swift。第六步进阶微调——offset、bottomOffset、equalSpaces 等附加属性除了上面 8 个核心属性还有几个附加属性值得了解offset按钮距离屏幕左右边缘的间距默认 40bottomOffset按钮与选中条底部的间距默认 0equalSpaces按钮是否等间距分布奇数个按钮建议 true偶数个建议 false保证文字居中buttonsWithImages把标题按钮换成图片按钮配合 SwipeButtonWithImage 结构体使用例如把红心/黄心做成点赞切换效果这些属性源码定义都能在 SwipeViewController.swift 顶部一次性看到全部是可读写 public 属性改起来非常直观。10 个关键属性速查清单属性类型作用pages[UIViewController]翻页页面数组只读startIndexInt默认显示的起始页索引selectionBarHeightCGFloat选中条高度selectionBarWidthCGFloat选中条宽度selectionBarColorUIColor选中条颜色buttonFontUIFont顶部按钮字体buttonColorUIColor未选中按钮颜色selectedButtonColorUIColor选中按钮颜色navigationBarColorUIColor导航栏背景色left/rightBarButtonItemUIBarButtonItem导航栏左右按钮新手常见疑问速答SwipeViewController 如何安装推荐使用 CocoaPods在 Podfile 中添加pod SwipeViewController即可也可以直接把 SwipeViewController 目录下的两个 Swift 文件拖进工程。页面标题不显示怎么办检查是否在传入 pages 之前设置了每个控制器的title属性。按钮点击也能翻页吗可以点击顶部按钮会调用switchTabs触发带动画的翻页且组件内部处理了重复点击保护不会出现动画错乱。想深入了解实现原理直接阅读 SwipeViewController.swift 源码即可核心逻辑集中在scrollViewDidScroll滑动跟随、scrollToNextViewController点击跳转和updateButtonsLayout按钮布局三个方法中。SwipeViewController 核心 API 全部介绍完毕从 pages 到 startIndex从选中条到导航栏10 个关键属性加起来不到 20 行配置代码就能实现一个体验流畅的多页面滑动导航界面。赶快动手试一试吧【免费下载链接】SwipeViewControllerSwipeViewController is a Swift modification of RKSwipeBetweenViewControllers - navigate between pages / ViewControllers项目地址: https://gitcode.com/gh_mirrors/sw/SwipeViewController创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表