From 10d7dfe394e1b489a366c07230fe8649a0da1e48 Mon Sep 17 00:00:00 2001 From: Andriy Gordiychuk Date: Wed, 10 May 2017 18:14:06 +0300 Subject: [PATCH] Horizontal margin update Margin of 2 pixels is too small. The default is now 4 and it can be controlled vi GLXSegmentAppearance. --- GLXSegmentedControl.podspec | 4 +- GLXSegmentedControl/GLXSegment.swift | 42 ++++++++++--------- .../GLXSegmentAppearance.swift | 5 ++- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/GLXSegmentedControl.podspec b/GLXSegmentedControl.podspec index 2ea0dfb..41d4dae 100644 --- a/GLXSegmentedControl.podspec +++ b/GLXSegmentedControl.podspec @@ -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 @@ -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" diff --git a/GLXSegmentedControl/GLXSegment.swift b/GLXSegmentedControl/GLXSegment.swift index 548c666..d671120 100644 --- a/GLXSegmentedControl/GLXSegment.swift +++ b/GLXSegmentedControl/GLXSegment.swift @@ -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)->())? @@ -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) } @@ -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 @@ -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 @@ -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 } } @@ -132,15 +134,15 @@ 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 } } @@ -148,7 +150,7 @@ open class GLXSegment: UIView { // MARK: Handle touch override open func touchesBegan(_ touches: Set, with event: UIEvent?) { if self.isSelected == false { - self.backgroundColor = self.appearance?.segmentTouchDownColor + self.backgroundColor = self.appearance.segmentTouchDownColor } } diff --git a/GLXSegmentedControl/GLXSegmentAppearance.swift b/GLXSegmentedControl/GLXSegmentAppearance.swift index b616481..78010bb 100644 --- a/GLXSegmentedControl/GLXSegmentAppearance.swift +++ b/GLXSegmentedControl/GLXSegmentAppearance.swift @@ -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 @@ -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