forked from JoeCoy/CustomSegmentedControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomSegmentedControl.swift
More file actions
44 lines (34 loc) · 1.42 KB
/
CustomSegmentedControl.swift
File metadata and controls
44 lines (34 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// CustomSegmentedControl.swift
// SVSP-iOS
//
// Created by jpcoy on 4/14/17.
// Copyright © 2017 SEP. All rights reserved.
//
import UIKit
class CustomSegmentedControl: UISegmentedControl {
let selectedBackgroundColor = UIColor(red: 19/255, green: 59/255, blue: 85/255, alpha: 0.5)
var sortedViews: [UIView]!
var currentIndex: Int = 0
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
private func configure() {
sortedViews = self.subviews.sorted( by: { $0.frame.origin.x < $1.frame.origin.x } )
changeSelectedIndex(to: currentIndex)
self.tintColor = UIColor.white
self.layer.cornerRadius = 4
self.clipsToBounds = true
let unselectedAttributes = [NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont.systemFont(ofSize: 13, weight: UIFontWeightRegular)]
self.setTitleTextAttributes(unselectedAttributes, for: .normal)
self.setTitleTextAttributes(unselectedAttributes, for: .selected)
}
func changeSelectedIndex(to newIndex: Int) {
sortedViews[currentIndex].backgroundColor = UIColor.clear
currentIndex = newIndex
self.selectedSegmentIndex = UISegmentedControlNoSegment
sortedViews[currentIndex].backgroundColor = selectedBackgroundColor
}
}