Custom animated segmented control written in Swift 5.2
- Xcode 11.4
- iOS 11.x+
- Swift 5.2
.package(
url: "https://github.com/mednoor/MNSwitchi.git",
from: "0.2.0"
)
import MNSwitchi
You can initialize a MNSwitchi
instance from code:
let switchiView = MNSwitchiView(frame: CGRect(x: 0, y: 0, width: 240, height: 36))
view.addSubview(switchiView)
switchiView.render(
content: MNSwitchiContent,
appearance: MNSwitchiAppearance,
options: MNSwitchiOptions
)
In order to set items you need to pass a tuple of type MNSwitchiContent
:
let items: MNSwitchiContent = (first: "First", second: "Second")
In order to set the styling you need to pass an object of type MNSwitchiAppearance
:
let appearance = MNSwitchiAppearance(backgroundColor: UIColor,
cornersRounded: Bool,
selectedViewBackgroundColor: UIColor,
states: MNSwitchiStates)
then create an instance of MNSwitchiStates
:
let states = MNSwitchiStates(
default: MNSwitchiStates.MNSwitchiState(textColor: UIColor,
titleFont: UIFont),
selected: MNSwitchiStates.MNSwitchiState(textColor: UIColor,
titleFont: UIFont),
highlighted: MNSwitchiStates.MNSwitchiState(textColor: UIColor,
titleFont: UIFont)
)
MNSwitchi
can be customized by passing an instance of MNSwitchiOptions
:
let options = MNSwitchiOptions(defaultSelectedItem: Int,
enableAnimation: Bool)
switchiView.switchedTo = { index in
print("Selected item at: \(index)")
}