可滚动分段控制面板 1.5.0

可滚动分段控制面板 1.5.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
Released最后发布2019年4月
SPM支持 SPM

Goce Petrovski 维护。



可滚动分段控制面板

CI Status Version License Platform

可滚动分段控制面板是UI容易控制面板的可替代品,当地段的总宽度超出控制面板本身的宽度时,它将变得可滚动。

可滚动分段控制面板支持4种段式样式:纯文本、纯图标、带图标居上和带图标居左的文本。

纯文本 纯图标
Demo Demo
图标居上 图标居左
Demo Demo

支持

  • Xcode 10.2+
  • Swift 5.0+
  • iOS 9.0+

使用方法

可滚动分段控制面板设计成可以像UI容易控制面板一样使用。方法和属性故意命名为与在UI容易控制面板中找到的相似或相同。

@IBOutlet weak var segmentedControl: ScrollableSegmentedControl!
    
override func viewDidLoad() {
    super.viewDidLoad()

    segmentedControl.segmentStyle = .textOnly
    segmentedControl.insertSegment(withTitle: "Segment 1", image: #imageLiteral(resourceName: "segment-1"), at: 0)
    segmentedControl.insertSegment(withTitle: "Segment 2", image: #imageLiteral(resourceName: "segment-2"), at: 1)
    segmentedControl.insertSegment(withTitle: "Segment 3", image: #imageLiteral(resourceName: "segment-3"), at: 2)
    segmentedControl.insertSegment(withTitle: "Segment 4", image: #imageLiteral(resourceName: "segment-4"), at: 3)
    segmentedControl.insertSegment(withTitle: "Segment 5", image: #imageLiteral(resourceName: "segment-5"), at: 4)
    segmentedControl.insertSegment(withTitle: "Segment 6", image: #imageLiteral(resourceName: "segment-6"), at: 5)
        
    segmentedControl.underlineSelected = true
        
    segmentedControl.addTarget(self, action: #selector(TableViewController.segmentSelected(sender:)), for: .valueChanged)

    // change some colors 
    segmentedControl.segmentContentColor = UIColor.white
    segmentedControl.selectedSegmentContentColor = UIColor.yellow
    segmentedControl.backgroundColor = UIColor.black
    
    // Turn off all segments been fixed/equal width. 
    // The width of each segment would be based on the text length and font size. 
    segmentedControl.fixedSegmentWidth = false
}
    
func segmentSelected(sender:ScrollableSegmentedControl) {
    print("Segment at index \(sender.selectedSegmentIndex)  selected")
}

使用外观代理来更改一些颜色

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let segmentedControlAppearance = ScrollableSegmentedControl.appearance()
    segmentedControlAppearance.segmentContentColor = UIColor.white
    segmentedControlAppearance.selectedSegmentContentColor = UIColor.yellow
    segmentedControlAppearance.backgroundColor = UIColor.black

    return true
}

通过setTitleTextAttributes方法配置自定义标题属性

let largerRedTextAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16), NSAttributedStringKey.foregroundColor: UIColor.red]
let largerRedTextHighlightAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16), NSAttributedStringKey.foregroundColor: UIColor.blue]
let largerRedTextSelectAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16), NSAttributedStringKey.foregroundColor: UIColor.orange]

segmentedControl.setTitleTextAttributes(largerRedTextAttributes, for: .normal)
segmentedControl.setTitleTextAttributes(largerRedTextHighlightAttributes, for: .highlighted)
segmentedControl.setTitleTextAttributes(largerRedTextSelectAttributes, for: .selected)

请参阅演示项目以了解如何使用示例。

安装

CocoaPods

安装 ScrollableSegmentedControl 最简单的方法是通过 CocoaPods.

pod 'ScrollableSegmentedControl', '~> 1.5.0'

旧式方法

只需将 ScrollableSegmentedControl.swift 添加到您的项目中。