测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布上次发布 | 2017年1月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Abraham Done 维护。
一个简单的 Swift 标签栏控制器
按钮必须实现该协议
public protocol LKTabBarButtonView {
var selected: Bool { get set }
var index: Int { get set }
var delegate: LKButtonBarIndexDelegate? { get set }
}
按钮的任何其他自定义都是设计决定。
按钮将通过代理控制标签栏
public protocol LKButtonBarIndexDelegate {
func buttonTapped(index: Int)
var index: Int { get set }
}
需要将该按钮与视图控制器配对,如下所示
public typealias TabButtonViewPair = (button: LKTabBarButtonView, viewController: UIViewController)
将传递一个 TabButtonViewPair
数组到 LKTabBarController
的初始化器,就这样。
只要它符合 LKTabBarButtonBarView
协议,按钮可以在任何视图中创建。视图中也应有按钮或手势识别器,用于某种交互,但视图可以具有图像、文本等。
这是一个示例
class ButtonView: UIView, LKTabBarButtonView {
// required elements
// The button doesn't do anything to set these, just have them available for the tab bar to set up
// The delegate is set when it is set as a button/view pair in the LKTabBarController's init function
var delegate: LKButtonBarIndexDelegate?
// The index is also set up when it is initialized
var index: Int = 0
// The actions that can happen when the button's state is changed by the LKTabBarController.
var selected: Bool = false {
didSet {
if selected {
button.setTitleColor(self.color, forState: .Normal)
button.enabled = false
colorBar.backgroundColor = self.color
} else {
button.setTitleColor(UIColor.grayColor(), forState: .Normal)
button.enabled = true
colorBar.backgroundColor = UIColor.grayColor()
}
}
}
// user elements
@IBAction func buttonTapped() {
// This is the action that will set the LKTabBarController's index and will change the view
delegate?.buttonTapped(index)
}
...
}
按钮将与视图控制器配对
let buttonViewPairs: [LKTabBarController.TabButtonViewPair] = []
let viewController = SomeViewController()
let button = ButtonView()
buttonViewPairs.append((button, viewController))
当所有视图和按钮设置完毕后,才能初始化 LKTabBarController
。
let tabController = LKTabBarController(buttonPairs: buttonViewPairs, parientView: self, containerView: contentView)
然后,LKTabBarController
将负责其提供容器视图内的视图切换
按钮的实际位置和外观完全取决于与项目兼容的设计。
LKTabBarController 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile 中
pod "LKTabBarController"
有问题的,或者想要请求一个功能?在 github 上创建一个问题。
想要贡献?将自己添加到作者列表中,并创建一个 pull request。
Abraham Done,[email protected]
LKTabBarController受MIT许可证的保护。更多信息,请参阅LICENSE文件。