ALAccordion 是一个为 iOS 优化的折叠风格容器视图,用于管理一组可展开/折叠的内容部分。当部分展开时,它将消耗整个屏幕(见截图)。
每个折叠的部分都与一个自定义视图控制器相关联,该控制器管理其自己的标题和内容视图。ALAccordion 依赖于自动布局来为标题视图提供固有内容大小。这使得拥有可以由每个部分修改和动画处理的自定义部分标题变得非常容易。
ALAccordion 还允许可选的标题和页脚视图,当部分展开时它们将隐藏。
Swift 2.2 使用版本 0.3
注意: ALAccordion 使用 ARC
要运行示例项目,先克隆仓库,然后在示例目录中首先运行 pod install
将 ALAccordion/Classes 目录中的文件添加到您的项目中
以下显示了 ALAccordion 的基本用法。对于更全面的示例,请参阅示例项目。
ALAccordionController 是一个用于管理部分的 UIViewController 子类容器视图。
首先,创建一个继承自 ALAccordionController 的视图控制器。
class ALHomeViewController: ALAccordionController
{
override func viewDidLoad()
{
super.viewDidLoad()
}
}
您可以通过设置 headerView
和 footerView
属性来为手风琴添加可选的头部和/或尾部。
注意: 手风琴使用头部和尾部视图的内建内容大小进行布局,请确保您正确设置自动布局约束。
override func viewDidLoad()
{
...
// Header
let header = UILabel()
header.text = "Accordion Header"
self.headerView = header
// Footer
let footer = UILabel()
footer.text = "Accordion Footer"
self.footerView = footer
}
要创建您的分区,您必须按它们将显示的顺序将一组视图控制器传递给 ALAccordion。视图控制器可以从故事板解析,也可以通过代码创建。
注意: 视图控制器必须遵守 ALAccordionSectionDelegate
协议(见下文)
override func viewDidLoad()
{
...
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let section1 = storyboard.instantiateViewControllerWithIdentifier("firstVC") as! Section1ViewController
let section2 = storyboard.instantiateViewControllerWithIdentifier("secondVC") as! Section2ViewController
let section3 = storyboard.instantiateViewControllerWithIdentifier("thirdVC") as! Section3TableViewController
self.setViewControllers(section1, section2, section3)
}
您应该为手风琴控制器提供一个新的 UIViewController 实例来表示手风琴中的每个分区。
分区视图控制器必须遵守 ALAccordionSectionDelegate
协议。这意味着它必须提供一个头部视图。同样,像 ALAccordionController 的头部和尾部视图一样,头部视图必须使用自动布局来提供内建内容大小。通过更改头部视图的约束,您可以轻松地更改头部的大小,例如在分区打开时显示更多详细信息。
class ALFirstSectionViewController: UIViewController, ALAccordionSectionDelegate
{
override func viewDidLoad()
{
super.viewDidLoad()
}
// MARK: - ALAccordionControllerDelegate
// Required
lazy var headerView: UIView =
{
let header = UILabel()
header.text = "Section 1 Header"
return header
}()
// Optional
func sectionWillOpen(#animated: Bool)
{
}
// Optional
func sectionWillClose(#animated: Bool)
{
}
// Optional
func sectionDidOpen()
{
}
// Optional
func sectionDidClose()
{
}
}
注意: 您必须手动指定每个分区如何打开。下面的示例展示了如何在头部视图上使用点击手势来打开分区。
lazy var headerView: UIView =
{
let header = UILabel()
header.text = "Section 1 Header"
// Add a tap gesture recogniser to open the section
let tapGR = UITapGestureRecognizer(target: self, action: "headerTapped:")
header.addGestureRecognizer(tapGR)
return header
}()
func headerTapped(recognizer: UITapGestureRecognizer)
{
// Get the section for this view controller
if let sectionIndex = self.accordionController?.sectionIndexForViewController(self)
{
// If this section is open, close it - otherwise, open it
if self.accordionController!.openSectionIndex == sectionIndex
{
self.accordionController?.closeSectionAtIndex(sectionIndex, animated: true)
}
else
{
self.accordionController?.openSectionAtIndex(sectionIndex, animated: true)
}
}
}
viewDidLoad
方法中调用 self.view.layoutIfNeeded()
。ALAccordion 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。
Alliants Limited