Skip to content

Commit

Permalink
Horizontal margin update
Browse files Browse the repository at this point in the history
Margin of 2 pixels is too small. The default is now 4 and it can be
controlled vi GLXSegmentAppearance.
  • Loading branch information
AndriyGo committed May 10, 2017
1 parent 19b242c commit 10d7dfe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions GLXSegmentedControl.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "GLXSegmentedControl"
s.version = "2.2.2"
s.version = "2.2.3"
s.summary = "Custom segmented control for iOS 9 and above"

s.description = <<-DESC
Expand All @@ -21,7 +21,7 @@ Pod::Spec.new do |s|
s.source = {
:git => "https://github.com/glenmax-ltd/GLXSegmentedControl.git",
:branch => "master",
:tag => "v2.2.2"}
:tag => "v2.2.3"}


s.source_files = "GLXSegmentedControl/*.swift"
Expand Down
42 changes: 22 additions & 20 deletions GLXSegmentedControl/GLXSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ open class GLXSegment: UIView {
open var offSelectionImage: UIImage?

// Appearance
open var appearance: GLXSegmentAppearance?
open var appearance: GLXSegmentAppearance

internal var didSelectSegment: ((_ segment: GLXSegment)->())?

Expand All @@ -51,7 +51,13 @@ open class GLXSegment: UIView {
// Init
internal init(appearance: GLXSegmentAppearance?) {

self.appearance = appearance
if let app = appearance {
self.appearance = app
}

else {
self.appearance = GLXSegmentAppearance()
}

super.init(frame: CGRect.zero)
}
Expand All @@ -63,9 +69,7 @@ open class GLXSegment: UIView {
internal func setupUIElements() {

var verticalMargin: CGFloat = 0.0
if let appearance = self.appearance {
verticalMargin = appearance.contentVerticalMargin
}
verticalMargin = appearance.contentVerticalMargin

let imagePresent = (self.offSelectionImage != nil) || (self.onSelectionImage != nil)
var titlePresent = false
Expand Down Expand Up @@ -98,7 +102,7 @@ open class GLXSegment: UIView {
self.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
self.topAnchor.constraint(lessThanOrEqualTo: view.topAnchor, constant:-verticalMargin).isActive = true
self.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
self.leadingAnchor.constraint(lessThanOrEqualTo: view.leadingAnchor, constant:-2).isActive = true
self.leadingAnchor.constraint(lessThanOrEqualTo: view.leadingAnchor, constant:-appearance.contentHorizontalMargin).isActive = true
}
else if imagePresent {
// only image is present
Expand All @@ -115,15 +119,13 @@ open class GLXSegment: UIView {
self.centerXAnchor.constraint(equalTo: self.label.centerXAnchor).isActive = true
self.centerYAnchor.constraint(equalTo: self.label.centerYAnchor).isActive = true
self.topAnchor.constraint(lessThanOrEqualTo: self.label.topAnchor, constant: -verticalMargin).isActive = true
self.leadingAnchor.constraint(lessThanOrEqualTo: self.label.leadingAnchor, constant:-2).isActive = true
self.leadingAnchor.constraint(lessThanOrEqualTo: self.label.leadingAnchor, constant:-appearance.contentHorizontalMargin).isActive = true
}

if let appearance = self.appearance {
self.backgroundColor = appearance.segmentOffSelectionColor
if titlePresent {
self.label.font = appearance.titleOffSelectionFont
self.label.textColor = appearance.titleOffSelectionColor
}
self.backgroundColor = appearance.segmentOffSelectionColor
if titlePresent {
self.label.font = appearance.titleOffSelectionFont
self.label.textColor = appearance.titleOffSelectionColor
}

}
Expand All @@ -132,23 +134,23 @@ open class GLXSegment: UIView {
internal func setSelected(_ selected: Bool) {
self.isSelected = selected
if selected == true {
self.backgroundColor = self.appearance?.segmentOnSelectionColor
self.label.textColor = self.appearance?.titleOnSelectionColor
self.label.font = self.appearance?.titleOnSelectionFont
self.backgroundColor = self.appearance.segmentOnSelectionColor
self.label.textColor = self.appearance.titleOnSelectionColor
self.label.font = self.appearance.titleOnSelectionFont
self.imageView.image = self.onSelectionImage
}
else {
self.backgroundColor = self.appearance?.segmentOffSelectionColor
self.label.textColor = self.appearance?.titleOffSelectionColor
self.label.font = self.appearance?.titleOffSelectionFont
self.backgroundColor = self.appearance.segmentOffSelectionColor
self.label.textColor = self.appearance.titleOffSelectionColor
self.label.font = self.appearance.titleOffSelectionFont
self.imageView.image = self.offSelectionImage
}
}

// MARK: Handle touch
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if self.isSelected == false {
self.backgroundColor = self.appearance?.segmentTouchDownColor
self.backgroundColor = self.appearance.segmentTouchDownColor
}
}

Expand Down
5 changes: 4 additions & 1 deletion GLXSegmentedControl/GLXSegmentAppearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ open class GLXSegmentAppearance {
open var titleOffSelectionFont: UIFont

open var contentVerticalMargin: CGFloat
open var contentHorizontalMargin: CGFloat
open var dividerWidth: CGFloat

open var dividerColor: UIColor
Expand All @@ -55,14 +56,16 @@ open class GLXSegmentAppearance {
self.titleOffSelectionFont = UIFont.systemFont(ofSize: 17.0)

self.contentVerticalMargin = 5.0
self.contentHorizontalMargin = 4.0

self.dividerWidth = 1.0
self.dividerColor = UIColor.lightGray
}

public init(contentVerticalMargin: CGFloat, segmentOnSelectionColor: UIColor, segmentOffSelectionColor: UIColor, titleOnSelectionColor: UIColor, titleOffSelectionColor: UIColor, titleOnSelectionFont: UIFont, titleOffSelectionFont: UIFont, dividerWidth:CGFloat) {
public init(contentVerticalMargin: CGFloat, contentHorizontalMargin:CGFloat, segmentOnSelectionColor: UIColor, segmentOffSelectionColor: UIColor, titleOnSelectionColor: UIColor, titleOffSelectionColor: UIColor, titleOnSelectionFont: UIFont, titleOffSelectionFont: UIFont, dividerWidth:CGFloat) {

self.contentVerticalMargin = contentVerticalMargin
self.contentHorizontalMargin = contentHorizontalMargin

self.segmentOnSelectionColor = segmentOnSelectionColor
self.segmentOffSelectionColor = segmentOffSelectionColor
Expand Down

0 comments on commit 10d7dfe

Please sign in to comment.