当您组合 UINavigationBar 和 UISegmentedControl 会发生什么?FilterBar。
FilterBar 是 UISegmentedControl 的一个精美实现,旨在在导航栏、搜索字段以及应用中的其他任何地方看起来都很好。
它设计用来填充其父视图的宽度,并很好地处理选项卡布局。
With CocoaPods
pod 'FilterBar', '~>2.0.0'
Cocoapods 要求 iOS 8.0 或更高。
如果您支持 iOS 7 或有其他偏好,您可以直接将 FilterBar.swift
拖放到您的项目中。
很简单
let filter : FilterBar = FilterBar()
这就是全部。我们将使用我们的 filter
来完成本 README 的其余部分。
要选择在过滤器栏上显示哪些标题,请设置过滤器栏的 titles
属性。然后 FilterBar 将触发一个布局更新并自动生成选项卡。例如:
filter.titles = ["Albus", "Bathilda", "Charlie", "Harry"] // Harry Potter!
FilterBar 使用了内联尺寸和一对布局约束来确保它始终在其父视图内居中并延伸。您只需提供一个 Y 位置约束。
// Create a constraint that attaches the filter bar to the top layout guide.
let topConstraint : NSLayoutConstraint = NSLayoutConstraint(item: filter, attribute: .Top, relatedBy: .Equal, toItem: self.topLayoutGuide, attribute: .Bottom, multiplier: 1.0, constant: 8.0)
// Add the constraint to the filter view.
self.view.addConstraint(topConstraint)
从工厂生成的每个 FilterBar 都会调用自身的 setTranslatesAutoresizingMaskIntoConstraints
,因此您不必这样做。
要设置 FilterBar 的淡化颜色,请使用 barTintColor
。例如:
// Color the filter bar
filter.barTintColor = UIColor.whiteColor()
这将会使过滤器栏背景变为白色。
注意:之前,barTintColor
被称为 color
。现在已弃用 color
属性。现在设置 color
属性会设置 barTintColor
属性,但在未来的版本中,将删除 color
属性。
FilterBar 可以是不透明的或透明的。使用 translucent
属性打开或关闭透明度。
// Enable translucency
filter.translucent = true
要设置 FilterBar 中选项卡文字的颜色,请使用 tintColor
属性。
// Color the text
filter.tintColor = UIColor.whiteColor()
FilterBar 提供了一个 0.5 点的黑色透明边框。
FilterBar的早期版本允许设置borderColor属性。这不再受支持,因为FilterBar现在更贴近UINavigationBar的外观。
borderColor
属性已经弃用,设置它没有任何效果。
FilterBar是UIControl
的子类,并使用.ValueChanged
事件来处理变化。
filter.addTarget(self, action: "segmentChanged:", forControlEvents: .ValueChanged)
这假设您有一个名为segmentChanged:
的处理程序,其代码如下所示
func segmentChanged(sender: AnyObject) {
// Handle changes here
}
使用selectedSegmentIndex
属性。
FilterBar以多种方式支持界面构建器。您可以设置条的颜色,FilterBar将在界面构建器中显示预览。
要使用界面构建器与FilterBar一起使用,将UIView拖放到视图中(控制器),然后将类更改为FilterBar。
FilterBar按照MIT授权协议发布。有关详细信息,请参阅LICENSE。