TBMultiAppearanceButton 1.2.0

TBMultiAppearanceButton 1.2.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后一个发布2015 年 7 月
SPM支持 SPM

Clay Smith 维护。



Swift 框架,用于配置和切换 UIButton 的多种外观,由 Talking Bibles InternationalClay Smith 提供

使用方法

此框架为 UIButton 添加了一个 TBMultiAppearanceButton 子类。这个新类允许在按钮的多个外观之间切换,每个外观都有其自己的完整 UIButton 内核属性集。每个外观都可以为所有 UIControlStates(包括高亮和选择状态)进行配置。

可能的外观是符合 TBControlAppearanceType 协议且原始类型为 Int 的枚举的实例。这个枚举可用于控制多个按钮的外观。

enum PlayerButtonAppearance: Int, TBControlAppearanceType {
  case Play = 2
  case Pause = 3
}

按钮必须以编程方式创建(至少目前是这样),因为新的按钮类是泛型的。它必须使用外观枚举进行指定。

let button = TBMultiAppearanceButton<PlayerButtonAppearance>(frame: someFrame)

现在可以为每个外观配置按钮的属性。更改存储属性将更新外观,前提是当前显示的是指定的外观和状态。

button.setTitle("Play", forAppearance: .Play, andState: .Normal)
button.setTitle("Play", forAppearance: .Pause, andState: .Normal)

button.setBackgroundImage(UIImage.imageWithColor(UIColor.blueColor()), forAppearance: .Play, andState: .Normal)
button.setBackgroundImage(UIImage.imageWithColor(UIColor.lightGrayColor()), forAppearance: .Play, andState: .Normal)

按钮可以接收与 UIButton 相同的所有消息。但是,当按钮更改外观或状态时,按钮的显示属性将被任何存储的外观属性覆盖。因此,建议通过提供的方法仅更改按钮的显示属性。

// Bad :(
// Will probably be overwritten
self.setTitle("Play", forState: .Normal)

// Good :)
button.setTitle("Play", forAppearance: .Play, andState: .Normal)

在不同外观之间切换很简单。按钮将保留其当前状态,任何配置给外观的属性将在按钮上更改。

// Select an appearance
button.activateAppearance(.Play)

// Later... select another appearance
button.activateAppearance(.Pause)

此框架对 UIButton 的修改仅限于增加外观控制。子类化的按钮可以在任何内置按钮可以使用的位置安全使用。

安装

此项目与 Cocoapods 和 Carthage 兼容。(以下说明假设您选择的方法已经安装。)

许可证

查看 许可证