MBSegmentControll 0.1.1

MBSegmentControll 0.1.1

Mohammad Ali Birang维护。



MBSegmentControll

Version License Platform

关于

支持从右向左切换的完全可定制的SegmentControll

特性

  • 可变数量
  • 动画过渡
  • 支持从右向左语言
  • 支持对于项目的图片和文本
  • 可拖入Interface Builder进行设计

sample

示例

要运行示例项目,首先克隆仓库,然后从Example目录中运行pod install

要求

  • iOS 9.0+

安装

  • CocoaPods

MBSegmentControll 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中

pod 'MBSegmentControll'

手动

将 MBSegmentControll 文件添加到您的项目中。

使用方法

  • CocoaPods

在您要使用的控制台中导入 'import MBSegmentControll'

import MBSegmentControll 
  • 编程式
lazy var segmetControll: MBSegmentControll = {
    let segmetControll = MBSegmentControll()
    segmetControll.buttonTitles = ["First", "Second", "Third"]
    segmetControll.borderWidth = 1
    segmetControll.borderColor = .lightGray
    segmetControll.roundCorner = true
    segmetControll.isLine = false
    segmetControll.textColor = .cyan
    segmetControll.selectedTextColor = .white
    segmetControll.translatesAutoresizingMaskIntoConstraints = false
    segmetControll.addTarget(self, action: #selector(segmentTapped(_:)), for: .valueChanged)

    return segmetControll
}()

viewDidLoad 中,将 segmetControll 添加为控制器视图的子视图

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(segmetControll)
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v1]|", options: [], metrics: nil, views: ["v1": segmetControll]))
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-40-[v1(50)]", options: [], metrics: nil, views: ["v1": segmetControll]))
    segmetControll.selecteSegment(at: 1)
}

处理选择状态的动作

@objc func segmentTapped(_ sender: MBSegmentControll) {
    // returns currently selected item index
    print(sender.selectedSegmentIndex)
}
  • Interface Builder

添加一个 UIView 并将其类设置为 MBSegmentControll。您可以直接从界面构建器中自定义控件。

除了字体和图片(界面构建器目前不支持),此控件在其他方面均可通过界面构建器进行完全自定义

  • 手动

与 CocoaPods 类似,但无需导入 MBSegmentControll

如何自定义?

查看 pod 内部的示例项目

对于界面构建器之外的定制图像或文本,每个都有相应属性。

// use text as title
segmetControll.buttonTitles = ["First", "Second", "Third"]


// use Image as title placeholder
imageSegment.buttonImages = [
    #imageLiteral(resourceName: "home"), 
    #imageLiteral(resourceName: "edit"), 
    #imageLiteral(resourceName: "message")
]

如果您想要 thumbView 为线型,默认行为设置为这样做或

// by default is true
segmetControll.isLine = true

// for rectangle change it to false
segmetControll.isLine = false

// for oval shape on thumbView
segmetControll.roundCorner = true
segmetControll.isLine = false

为了在初始化后更改已选段,有一个构建方法可用,如果设置了一个动作方法,则可以触发动作方法

segmetControll.selecteSegment(at: 0)

MBSegmentControll 完全支持从右到左的语言。

许可证

MBSegmentControll 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。