From 33346e033af365dd17ca7d38538d695064f91a45 Mon Sep 17 00:00:00 2001 From: Arman Zoghi Date: Sun, 14 Feb 2021 12:01:20 +0330 Subject: [PATCH 1/5] Added an example to test the new features --- .../Base.lproj/LaunchScreen.xib | 25 ++++++++++++------- .../ViewController.swift | 17 +++++++++++++ Example/Podfile.lock | 6 ++--- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Example/BetterSegmentedControl/Base.lproj/LaunchScreen.xib b/Example/BetterSegmentedControl/Base.lproj/LaunchScreen.xib index ccaafc2..90ce89b 100644 --- a/Example/BetterSegmentedControl/Base.lproj/LaunchScreen.xib +++ b/Example/BetterSegmentedControl/Base.lproj/LaunchScreen.xib @@ -1,8 +1,10 @@ - - + + + - - + + + @@ -11,20 +13,20 @@ - - + @@ -38,4 +40,9 @@ + + + + + diff --git a/Example/BetterSegmentedControl/ViewController.swift b/Example/BetterSegmentedControl/ViewController.swift index 6b77686..068ecd2 100644 --- a/Example/BetterSegmentedControl/ViewController.swift +++ b/Example/BetterSegmentedControl/ViewController.swift @@ -79,6 +79,23 @@ class ViewController: UIViewController { index: -1) noSelectedSegmentControl.addTarget(self, action: #selector(segmentedControl1ValueChanged(_:)), for: .valueChanged) view.addSubview(noSelectedSegmentControl) + + // Control 8: Icons and labels + let iconAndLabelSegmentControl = BetterSegmentedControl( + frame: CGRect(x: 0, y: 550, width: view.bounds.width - 32.0, height: 80), + segments: LabelSegment.segments(withTitles: ["Keyli kam", "Keyli motevaset", "Keyli ziad"], + normalFont: .systemFont(ofSize: 14.0), + normalTextColor: .cyan, + selectedBackgroundColor: .cyan, + selectedTextColor: .darkGray), + index: 1) + iconAndLabelSegmentControl.center.x = view.center.x + iconAndLabelSegmentControl.backgroundColor = .darkGray + iconAndLabelSegmentControl.layer.cornerRadius = 10 + iconAndLabelSegmentControl.cornerRadius = 10 + iconAndLabelSegmentControl.indicatorView.backgroundColor = .cyan + iconAndLabelSegmentControl.addTarget(self, action: #selector(segmentedControl1ValueChanged(_:)), for: .valueChanged) + view.addSubview(iconAndLabelSegmentControl) } // MARK: - Action handlers diff --git a/Example/Podfile.lock b/Example/Podfile.lock index b60d72d..890ba67 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - BetterSegmentedControl (2.0) + - BetterSegmentedControl (2.0.1) - iOSSnapshotTestCase (5.0.2): - iOSSnapshotTestCase/SwiftSupport (= 5.0.2) - iOSSnapshotTestCase/Core (5.0.2) @@ -31,7 +31,7 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - BetterSegmentedControl: ce9b68b81c963991211c561026ed2cf711f9ac63 + BetterSegmentedControl: 09607b27861d49cbce48b7673b74f9150a3d371a iOSSnapshotTestCase: 2d51aa06775e95cecb0a1fb9c5c159ccd1dd4596 Nimble: 051e3d8912d40138fa5591c78594f95fb172af37 Nimble-Snapshots: bbd1ab264bacc24a9ce24a8363bc05aac783aeb0 @@ -39,4 +39,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 609c5442f2ad21fe0424eaab055f9caa5c2944a1 -COCOAPODS: 1.9.3 +COCOAPODS: 1.10.1 From 85e4f40eb48e5b8410f0031fd0d57e5d1762d542 Mon Sep 17 00:00:00 2001 From: Arman Zoghi Date: Mon, 15 Feb 2021 09:08:39 +0330 Subject: [PATCH 2/5] Created the IconWithLabelView and made the icon work --- .../ViewController.swift | 25 ++-- Pod/Classes/Segments/IconSegment.swift | 2 + .../Segments/IconWithLabelSegment.swift | 114 ++++++++++++++++++ Pod/Classes/Segments/IconWithLabelView.swift | 72 +++++++++++ Pod/Classes/Segments/LabelSegment.swift | 2 + 5 files changed, 202 insertions(+), 13 deletions(-) create mode 100644 Pod/Classes/Segments/IconWithLabelSegment.swift create mode 100644 Pod/Classes/Segments/IconWithLabelView.swift diff --git a/Example/BetterSegmentedControl/ViewController.swift b/Example/BetterSegmentedControl/ViewController.swift index 068ecd2..c76806e 100644 --- a/Example/BetterSegmentedControl/ViewController.swift +++ b/Example/BetterSegmentedControl/ViewController.swift @@ -81,21 +81,20 @@ class ViewController: UIViewController { view.addSubview(noSelectedSegmentControl) // Control 8: Icons and labels - let iconAndLabelSegmentControl = BetterSegmentedControl( + let iconWithLabelSegmentControl = BetterSegmentedControl( frame: CGRect(x: 0, y: 550, width: view.bounds.width - 32.0, height: 80), - segments: LabelSegment.segments(withTitles: ["Keyli kam", "Keyli motevaset", "Keyli ziad"], - normalFont: .systemFont(ofSize: 14.0), - normalTextColor: .cyan, - selectedBackgroundColor: .cyan, - selectedTextColor: .darkGray), + segments: IconWithLabelSegment.segments(withIcons: [UIImage(named: "facebook")!, UIImage(named: "twitter")!], + iconSize: CGSize(width: 30.0, height: 30.0), + normalIconTintColor: .cyan, + selectedIconTintColor: .darkGray), index: 1) - iconAndLabelSegmentControl.center.x = view.center.x - iconAndLabelSegmentControl.backgroundColor = .darkGray - iconAndLabelSegmentControl.layer.cornerRadius = 10 - iconAndLabelSegmentControl.cornerRadius = 10 - iconAndLabelSegmentControl.indicatorView.backgroundColor = .cyan - iconAndLabelSegmentControl.addTarget(self, action: #selector(segmentedControl1ValueChanged(_:)), for: .valueChanged) - view.addSubview(iconAndLabelSegmentControl) + iconWithLabelSegmentControl.center.x = view.center.x + iconWithLabelSegmentControl.backgroundColor = .darkGray + iconWithLabelSegmentControl.layer.cornerRadius = 10 + iconWithLabelSegmentControl.cornerRadius = 10 + iconWithLabelSegmentControl.indicatorView.backgroundColor = .cyan + iconWithLabelSegmentControl.addTarget(self, action: #selector(segmentedControl1ValueChanged(_:)), for: .valueChanged) + view.addSubview(iconWithLabelSegmentControl) } // MARK: - Action handlers diff --git a/Pod/Classes/Segments/IconSegment.swift b/Pod/Classes/Segments/IconSegment.swift index 01286db..d5cd78e 100644 --- a/Pod/Classes/Segments/IconSegment.swift +++ b/Pod/Classes/Segments/IconSegment.swift @@ -56,6 +56,7 @@ open class IconSegment: BetterSegmentedControlSegment { backgroundColor: selectedBackgroundColor, iconTintColor: selectedIconTintColor) }() + #warning("here") private func createView(withIcon icon: UIImage, iconSize: CGSize, backgroundColor: UIColor, @@ -76,6 +77,7 @@ open class IconSegment: BetterSegmentedControlSegment { view.addSubview(imageView) return view } + #warning("and here") } public extension IconSegment { diff --git a/Pod/Classes/Segments/IconWithLabelSegment.swift b/Pod/Classes/Segments/IconWithLabelSegment.swift new file mode 100644 index 0000000..5997de3 --- /dev/null +++ b/Pod/Classes/Segments/IconWithLabelSegment.swift @@ -0,0 +1,114 @@ +// +// IconWithLabelSegment.swift +// BetterSegmentedControl +// +// Created by Arman Zoghi on 2/14/21. +// + +#if canImport(UIKit) + +import UIKit + +open class IconWithLabelSegment: BetterSegmentedControlSegment { + // MARK: Constants + private struct DefaultValues { + static let normalBackgroundColor: UIColor = .clear + static let normalTextColor: UIColor = .black + static let normalFont: UIFont = .systemFont(ofSize: 13) + static let selectedBackgroundColor: UIColor = .clear + static let selectedTextColor: UIColor = .black + static let selectedFont: UIFont = .systemFont(ofSize: 13, weight: .medium) + } + + // MARK: Properties + public var icon: UIImage + public var iconSize: CGSize + public let text: String? + + public var normalIconTintColor: UIColor + public let normalFont: UIFont + public let normalTextColor: UIColor + public var normalBackgroundColor: UIColor + + public var selectedIconTintColor: UIColor + public var selectedBackgroundColor: UIColor + + private let numberOfLines: Int + private let accessibilityIdentifier: String? + + // MARK: Lifecycle + public init(icon: UIImage, + iconSize: CGSize, + normalBackgroundColor: UIColor? = nil, + normalIconTintColor: UIColor, + selectedBackgroundColor: UIColor? = nil, + selectedIconTintColor: UIColor) { + self.icon = icon.withRenderingMode(.alwaysTemplate) + self.iconSize = iconSize + self.normalBackgroundColor = normalBackgroundColor ?? DefaultValues.normalBackgroundColor + self.normalIconTintColor = normalIconTintColor + self.selectedBackgroundColor = selectedBackgroundColor ?? DefaultValues.selectedBackgroundColor + self.selectedIconTintColor = selectedIconTintColor + } + + // MARK: BetterSegmentedControlSegment + public var intrinsicContentSize: CGSize? { nil } + + public lazy var normalView: UIView = { + return createView(withIcon: icon, + iconSize: iconSize, + backgroundColor: normalBackgroundColor, + iconTintColor: normalIconTintColor) + }() + public lazy var selectedView: UIView = { + return createView(withIcon: icon, + iconSize: iconSize, + backgroundColor: selectedBackgroundColor, + iconTintColor: selectedIconTintColor) + }() + #warning("here") + private func createView(withIcon icon: UIImage, + iconSize: CGSize, + backgroundColor: UIColor, + iconTintColor: UIColor, + withText text: text, + font: selectedFont, + textColor: selectedTextColor, + accessibilityIdentifier: accessibilityIdentifier) -> UIView { + let view = IconWithLabelView() + view.backgroundColor = backgroundColor + view.imageViewConfig(icon: icon, + width: iconSize.width, + height: iconSize.height, + contentMode: .scaleAspectFit, + tintColor: iconTintColor) + view.labelConfig(text: text, + numberOfLines: numberOfLines, + font: font, + lineBreakMode: .byTruncatingTail, + textAlignment: .center, + accessibilityIdentifier: accessibilityIdentifier) + return view + } + #warning("and here") +} + +public extension IconWithLabelSegment { + class func segments(withIcons icons: [UIImage], + iconSize: CGSize, + normalBackgroundColor: UIColor? = nil, + normalIconTintColor: UIColor, + selectedBackgroundColor: UIColor? = nil, + selectedIconTintColor: UIColor) -> [BetterSegmentedControlSegment] { + return icons.map { + IconWithLabelSegment(icon: $0, + iconSize: iconSize, + normalBackgroundColor: normalBackgroundColor, + normalIconTintColor: normalIconTintColor, + selectedBackgroundColor: selectedBackgroundColor, + selectedIconTintColor: selectedIconTintColor) + } + } +} + +#endif diff --git a/Pod/Classes/Segments/IconWithLabelView.swift b/Pod/Classes/Segments/IconWithLabelView.swift new file mode 100644 index 0000000..6f980d3 --- /dev/null +++ b/Pod/Classes/Segments/IconWithLabelView.swift @@ -0,0 +1,72 @@ +// +// IconWithLabelView.swift +// BetterSegmentedControl +// +// Created by Arman Zoghi on 2/14/21. +// + +import UIKit + +class IconWithLabelView: UIView { + // MARK: Subviews + public var imageView: UIImageView? + public var label: UILabel? + + // MARK: Lifecycle + override init(frame: CGRect) { + super.init(frame: frame) +// self.createView() + } + + public required init?(coder: NSCoder) { + super.init(coder: coder) +// self.createView() + } + + // MARK: Create View + fileprivate func createView() { +// self.imageViewConfig() +// self.labelConfig() + } + + // MARK: Image View + //config + public func imageViewConfig(icon: UIImage, width: CGFloat, height: CGFloat, contentMode: UIView.ContentMode, tintColor: UIColor) { + self.imageView = UIImageView(image: icon) + self.imageView?.tintColor = tintColor + self.addSubview(self.imageView!) + self.imageViewConstraints(width: width, height: height) + } + //constraints + fileprivate func imageViewConstraints(width: CGFloat, height: CGFloat) { + self.imageView!.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint(item: self.imageView!, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 0.65, constant: 0).isActive = true + NSLayoutConstraint(item: self.imageView!, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0).isActive = true + NSLayoutConstraint(item: self.imageView!, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0, constant: width).isActive = true + NSLayoutConstraint(item: self.imageView!, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: height).isActive = true + } + + // MARK: Label + //config + func labelConfig(text: String, numberOfLines: Int, font: UIFont, textColor: UIColor, lineBreakMode: NSLineBreakMode, textAlignment: NSTextAlignment, accessibilityIdentifier: String) { + self.label = UILabel() + self.addSubview(self.label!) + self.labelConstraints() + self.label?.sizeToFit() + self.label?.text = text + self.label?.numberOfLines = numberOfLines + self.label?.font = font + self.label?.textColor = textColor + self.label?.lineBreakMode = lineBreakMode + self.label?.textAlignment = textAlignment + self.label?.accessibilityIdentifier = accessibilityIdentifier + } + //constraints + fileprivate func labelConstraints() { + self.label!.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint(item: self.label!, attribute: .top, relatedBy: .equal, toItem: self.imageView, attribute: .bottom, multiplier: 1, constant: 7.5).isActive = true + NSLayoutConstraint(item: self.label!, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0).isActive = true + NSLayoutConstraint(item: self.label!, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1, constant: 0).isActive = true + } + +} diff --git a/Pod/Classes/Segments/LabelSegment.swift b/Pod/Classes/Segments/LabelSegment.swift index f32f64c..cb5298b 100644 --- a/Pod/Classes/Segments/LabelSegment.swift +++ b/Pod/Classes/Segments/LabelSegment.swift @@ -74,6 +74,7 @@ open class LabelSegment: BetterSegmentedControlSegment { textColor: selectedTextColor, accessibilityIdentifier: accessibilityIdentifier) }() + #warning("here") open func createLabel(withText text: String?, backgroundColor: UIColor, font: UIFont, @@ -90,6 +91,7 @@ open class LabelSegment: BetterSegmentedControlSegment { label.accessibilityIdentifier = accessibilityIdentifier return label } + #warning("here") } public extension LabelSegment { From 0dfec4573e3799fd464b11cec8254bd55bc1904b Mon Sep 17 00:00:00 2001 From: Arman Zoghi Date: Mon, 15 Feb 2021 11:18:36 +0330 Subject: [PATCH 3/5] Added and tested the label support for IconWithLabelSegment --- .../ViewController.swift | 15 ++-- .../Segments/IconWithLabelSegment.swift | 69 +++++++++++++++---- Pod/Classes/Segments/IconWithLabelView.swift | 6 +- 3 files changed, 69 insertions(+), 21 deletions(-) diff --git a/Example/BetterSegmentedControl/ViewController.swift b/Example/BetterSegmentedControl/ViewController.swift index c76806e..19aa509 100644 --- a/Example/BetterSegmentedControl/ViewController.swift +++ b/Example/BetterSegmentedControl/ViewController.swift @@ -82,17 +82,20 @@ class ViewController: UIViewController { // Control 8: Icons and labels let iconWithLabelSegmentControl = BetterSegmentedControl( - frame: CGRect(x: 0, y: 550, width: view.bounds.width - 32.0, height: 80), - segments: IconWithLabelSegment.segments(withIcons: [UIImage(named: "facebook")!, UIImage(named: "twitter")!], - iconSize: CGSize(width: 30.0, height: 30.0), - normalIconTintColor: .cyan, - selectedIconTintColor: .darkGray), + frame: CGRect(x: 0, y: 550, width: 200, height: 40), + segments: IconWithLabelSegment.segments(withIconsAndLabels: [IconWithLabel(icon: UIImage(named: "facebook")!, title: "Facebook"), IconWithLabel(icon: UIImage(named: "twitter")!, title: "Twitter")], + iconSize: CGSize(width: 20.0, height: 20.0), + normalIconTintColor: .systemTeal, + normalFont: .systemFont(ofSize: 10), + normalTextColor: .systemTeal, + selectedIconTintColor: .darkGray, + selectedFont: .systemFont(ofSize: 10)), index: 1) iconWithLabelSegmentControl.center.x = view.center.x iconWithLabelSegmentControl.backgroundColor = .darkGray iconWithLabelSegmentControl.layer.cornerRadius = 10 iconWithLabelSegmentControl.cornerRadius = 10 - iconWithLabelSegmentControl.indicatorView.backgroundColor = .cyan + iconWithLabelSegmentControl.indicatorView.backgroundColor = .systemTeal iconWithLabelSegmentControl.addTarget(self, action: #selector(segmentedControl1ValueChanged(_:)), for: .valueChanged) view.addSubview(iconWithLabelSegmentControl) } diff --git a/Pod/Classes/Segments/IconWithLabelSegment.swift b/Pod/Classes/Segments/IconWithLabelSegment.swift index 5997de3..1c987a9 100644 --- a/Pod/Classes/Segments/IconWithLabelSegment.swift +++ b/Pod/Classes/Segments/IconWithLabelSegment.swift @@ -31,6 +31,8 @@ open class IconWithLabelSegment: BetterSegmentedControlSegment { public var normalBackgroundColor: UIColor public var selectedIconTintColor: UIColor + public let selectedFont: UIFont + public let selectedTextColor: UIColor public var selectedBackgroundColor: UIColor private let numberOfLines: Int @@ -38,17 +40,31 @@ open class IconWithLabelSegment: BetterSegmentedControlSegment { // MARK: Lifecycle public init(icon: UIImage, + text: String? = nil, iconSize: CGSize, + numberOfLines: Int = 1, normalBackgroundColor: UIColor? = nil, normalIconTintColor: UIColor, + normalFont: UIFont? = nil, + normalTextColor: UIColor? = nil, selectedBackgroundColor: UIColor? = nil, - selectedIconTintColor: UIColor) { + selectedIconTintColor: UIColor, + selectedFont: UIFont? = nil, + selectedTextColor: UIColor? = nil, + accessibilityIdentifier: String? = nil) { self.icon = icon.withRenderingMode(.alwaysTemplate) + self.text = text self.iconSize = iconSize + self.numberOfLines = numberOfLines self.normalBackgroundColor = normalBackgroundColor ?? DefaultValues.normalBackgroundColor self.normalIconTintColor = normalIconTintColor + self.normalFont = normalFont ?? DefaultValues.normalFont + self.normalTextColor = normalTextColor ?? DefaultValues.normalTextColor self.selectedBackgroundColor = selectedBackgroundColor ?? DefaultValues.selectedBackgroundColor + self.selectedFont = selectedFont ?? DefaultValues.selectedFont + self.selectedTextColor = selectedTextColor ?? DefaultValues.selectedTextColor self.selectedIconTintColor = selectedIconTintColor + self.accessibilityIdentifier = accessibilityIdentifier } // MARK: BetterSegmentedControlSegment @@ -58,23 +74,31 @@ open class IconWithLabelSegment: BetterSegmentedControlSegment { return createView(withIcon: icon, iconSize: iconSize, backgroundColor: normalBackgroundColor, - iconTintColor: normalIconTintColor) + iconTintColor: normalIconTintColor, + withText: text, + font: normalFont, + textColor: normalTextColor, + accessibilityIdentifier: accessibilityIdentifier) }() public lazy var selectedView: UIView = { return createView(withIcon: icon, iconSize: iconSize, backgroundColor: selectedBackgroundColor, - iconTintColor: selectedIconTintColor) + iconTintColor: selectedIconTintColor, + withText: text, + font: selectedFont, + textColor: selectedTextColor, + accessibilityIdentifier: accessibilityIdentifier) }() #warning("here") private func createView(withIcon icon: UIImage, iconSize: CGSize, backgroundColor: UIColor, iconTintColor: UIColor, - withText text: text, - font: selectedFont, - textColor: selectedTextColor, - accessibilityIdentifier: accessibilityIdentifier) -> UIView { + withText text: String?, + font: UIFont?, + textColor: UIColor?, + accessibilityIdentifier: String?) -> UIView { let view = IconWithLabelView() view.backgroundColor = backgroundColor view.imageViewConfig(icon: icon, @@ -85,6 +109,7 @@ open class IconWithLabelSegment: BetterSegmentedControlSegment { view.labelConfig(text: text, numberOfLines: numberOfLines, font: font, + textColor: textColor, lineBreakMode: .byTruncatingTail, textAlignment: .center, accessibilityIdentifier: accessibilityIdentifier) @@ -93,20 +118,40 @@ open class IconWithLabelSegment: BetterSegmentedControlSegment { #warning("and here") } +public class IconWithLabel { + let icon: UIImage + let title: String + public init(icon: UIImage, title: String) { + self.icon = icon + self.title = title + } +} + public extension IconWithLabelSegment { - class func segments(withIcons icons: [UIImage], + class func segments(withIconsAndLabels iconsAndLabelS: [IconWithLabel], iconSize: CGSize, + numberOfLines: Int = 1, normalBackgroundColor: UIColor? = nil, normalIconTintColor: UIColor, + normalFont: UIFont? = nil, + normalTextColor: UIColor? = nil, selectedBackgroundColor: UIColor? = nil, - selectedIconTintColor: UIColor) -> [BetterSegmentedControlSegment] { - return icons.map { - IconWithLabelSegment(icon: $0, + selectedIconTintColor: UIColor, + selectedFont: UIFont? = nil, + selectedTextColor: UIColor? = nil) -> [BetterSegmentedControlSegment] { + return iconsAndLabelS.map { + IconWithLabelSegment(icon: $0.icon, + text: $0.title, iconSize: iconSize, + numberOfLines: numberOfLines, normalBackgroundColor: normalBackgroundColor, normalIconTintColor: normalIconTintColor, + normalFont: normalFont, + normalTextColor: normalTextColor, selectedBackgroundColor: selectedBackgroundColor, - selectedIconTintColor: selectedIconTintColor) + selectedIconTintColor: selectedIconTintColor, + selectedFont: selectedFont, + selectedTextColor: selectedTextColor) } } } diff --git a/Pod/Classes/Segments/IconWithLabelView.swift b/Pod/Classes/Segments/IconWithLabelView.swift index 6f980d3..a8d4c91 100644 --- a/Pod/Classes/Segments/IconWithLabelView.swift +++ b/Pod/Classes/Segments/IconWithLabelView.swift @@ -48,13 +48,13 @@ class IconWithLabelView: UIView { // MARK: Label //config - func labelConfig(text: String, numberOfLines: Int, font: UIFont, textColor: UIColor, lineBreakMode: NSLineBreakMode, textAlignment: NSTextAlignment, accessibilityIdentifier: String) { + func labelConfig(text: String?, numberOfLines: Int?, font: UIFont?, textColor: UIColor?, lineBreakMode: NSLineBreakMode, textAlignment: NSTextAlignment, accessibilityIdentifier: String?) { self.label = UILabel() self.addSubview(self.label!) self.labelConstraints() self.label?.sizeToFit() self.label?.text = text - self.label?.numberOfLines = numberOfLines + self.label?.numberOfLines = numberOfLines ?? 1 self.label?.font = font self.label?.textColor = textColor self.label?.lineBreakMode = lineBreakMode @@ -64,7 +64,7 @@ class IconWithLabelView: UIView { //constraints fileprivate func labelConstraints() { self.label!.translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint(item: self.label!, attribute: .top, relatedBy: .equal, toItem: self.imageView, attribute: .bottom, multiplier: 1, constant: 7.5).isActive = true + NSLayoutConstraint(item: self.label!, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.65, constant: 0).isActive = true NSLayoutConstraint(item: self.label!, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0).isActive = true NSLayoutConstraint(item: self.label!, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1, constant: 0).isActive = true } From f1aed60bc0c8a2aba678698c5cf5fbd9a53e4ccf Mon Sep 17 00:00:00 2001 From: Arman Zoghi Date: Mon, 15 Feb 2021 11:40:47 +0330 Subject: [PATCH 4/5] Removed custom warnings and moved the IconWithLabel class to it's own file --- .../ViewController.swift | 4 ++-- Pod/Classes/Segments/IconSegment.swift | 2 -- Pod/Classes/Segments/IconWithLabel.swift | 18 ++++++++++++++++++ .../Segments/IconWithLabelSegment.swift | 11 ----------- Pod/Classes/Segments/IconWithLabelView.swift | 8 -------- Pod/Classes/Segments/LabelSegment.swift | 2 -- 6 files changed, 20 insertions(+), 25 deletions(-) create mode 100644 Pod/Classes/Segments/IconWithLabel.swift diff --git a/Example/BetterSegmentedControl/ViewController.swift b/Example/BetterSegmentedControl/ViewController.swift index 19aa509..a02b74c 100644 --- a/Example/BetterSegmentedControl/ViewController.swift +++ b/Example/BetterSegmentedControl/ViewController.swift @@ -88,11 +88,11 @@ class ViewController: UIViewController { normalIconTintColor: .systemTeal, normalFont: .systemFont(ofSize: 10), normalTextColor: .systemTeal, - selectedIconTintColor: .darkGray, + selectedIconTintColor: #colorLiteral(red: 0.2117647059, green: 0.2705882353, blue: 0.3098039216, alpha: 1), selectedFont: .systemFont(ofSize: 10)), index: 1) iconWithLabelSegmentControl.center.x = view.center.x - iconWithLabelSegmentControl.backgroundColor = .darkGray + iconWithLabelSegmentControl.backgroundColor = #colorLiteral(red: 0.2117647059, green: 0.2705882353, blue: 0.3098039216, alpha: 1) iconWithLabelSegmentControl.layer.cornerRadius = 10 iconWithLabelSegmentControl.cornerRadius = 10 iconWithLabelSegmentControl.indicatorView.backgroundColor = .systemTeal diff --git a/Pod/Classes/Segments/IconSegment.swift b/Pod/Classes/Segments/IconSegment.swift index d5cd78e..01286db 100644 --- a/Pod/Classes/Segments/IconSegment.swift +++ b/Pod/Classes/Segments/IconSegment.swift @@ -56,7 +56,6 @@ open class IconSegment: BetterSegmentedControlSegment { backgroundColor: selectedBackgroundColor, iconTintColor: selectedIconTintColor) }() - #warning("here") private func createView(withIcon icon: UIImage, iconSize: CGSize, backgroundColor: UIColor, @@ -77,7 +76,6 @@ open class IconSegment: BetterSegmentedControlSegment { view.addSubview(imageView) return view } - #warning("and here") } public extension IconSegment { diff --git a/Pod/Classes/Segments/IconWithLabel.swift b/Pod/Classes/Segments/IconWithLabel.swift new file mode 100644 index 0000000..b3180c2 --- /dev/null +++ b/Pod/Classes/Segments/IconWithLabel.swift @@ -0,0 +1,18 @@ +// +// IconWithLabel.swift +// BetterSegmentedControl +// +// Created by Arman Zoghi on 2/15/21. +// + +import Foundation +import UIKit + +public class IconWithLabel { + let icon: UIImage + let title: String + public init(icon: UIImage, title: String) { + self.icon = icon + self.title = title + } +} diff --git a/Pod/Classes/Segments/IconWithLabelSegment.swift b/Pod/Classes/Segments/IconWithLabelSegment.swift index 1c987a9..7e580a5 100644 --- a/Pod/Classes/Segments/IconWithLabelSegment.swift +++ b/Pod/Classes/Segments/IconWithLabelSegment.swift @@ -90,7 +90,6 @@ open class IconWithLabelSegment: BetterSegmentedControlSegment { textColor: selectedTextColor, accessibilityIdentifier: accessibilityIdentifier) }() - #warning("here") private func createView(withIcon icon: UIImage, iconSize: CGSize, backgroundColor: UIColor, @@ -115,16 +114,6 @@ open class IconWithLabelSegment: BetterSegmentedControlSegment { accessibilityIdentifier: accessibilityIdentifier) return view } - #warning("and here") -} - -public class IconWithLabel { - let icon: UIImage - let title: String - public init(icon: UIImage, title: String) { - self.icon = icon - self.title = title - } } public extension IconWithLabelSegment { diff --git a/Pod/Classes/Segments/IconWithLabelView.swift b/Pod/Classes/Segments/IconWithLabelView.swift index a8d4c91..93b400f 100644 --- a/Pod/Classes/Segments/IconWithLabelView.swift +++ b/Pod/Classes/Segments/IconWithLabelView.swift @@ -15,18 +15,10 @@ class IconWithLabelView: UIView { // MARK: Lifecycle override init(frame: CGRect) { super.init(frame: frame) -// self.createView() } public required init?(coder: NSCoder) { super.init(coder: coder) -// self.createView() - } - - // MARK: Create View - fileprivate func createView() { -// self.imageViewConfig() -// self.labelConfig() } // MARK: Image View diff --git a/Pod/Classes/Segments/LabelSegment.swift b/Pod/Classes/Segments/LabelSegment.swift index cb5298b..f32f64c 100644 --- a/Pod/Classes/Segments/LabelSegment.swift +++ b/Pod/Classes/Segments/LabelSegment.swift @@ -74,7 +74,6 @@ open class LabelSegment: BetterSegmentedControlSegment { textColor: selectedTextColor, accessibilityIdentifier: accessibilityIdentifier) }() - #warning("here") open func createLabel(withText text: String?, backgroundColor: UIColor, font: UIFont, @@ -91,7 +90,6 @@ open class LabelSegment: BetterSegmentedControlSegment { label.accessibilityIdentifier = accessibilityIdentifier return label } - #warning("here") } public extension LabelSegment { From a83c654d992b80eff40ec9b1c649700993c2604d Mon Sep 17 00:00:00 2001 From: Arman Zoghi Date: Mon, 15 Feb 2021 11:54:02 +0330 Subject: [PATCH 5/5] Relocated the IconWithLabel files to a new folder named IconWithLabel to better organize files. --- Example/BetterSegmentedControl/ViewController.swift | 5 ++--- Pod/Classes/Segments/{ => IconWithLabel}/IconWithLabel.swift | 0 .../Segments/{ => IconWithLabel}/IconWithLabelSegment.swift | 0 .../Segments/{ => IconWithLabel}/IconWithLabelView.swift | 0 4 files changed, 2 insertions(+), 3 deletions(-) rename Pod/Classes/Segments/{ => IconWithLabel}/IconWithLabel.swift (100%) rename Pod/Classes/Segments/{ => IconWithLabel}/IconWithLabelSegment.swift (100%) rename Pod/Classes/Segments/{ => IconWithLabel}/IconWithLabelView.swift (100%) diff --git a/Example/BetterSegmentedControl/ViewController.swift b/Example/BetterSegmentedControl/ViewController.swift index a02b74c..064ffbd 100644 --- a/Example/BetterSegmentedControl/ViewController.swift +++ b/Example/BetterSegmentedControl/ViewController.swift @@ -80,9 +80,9 @@ class ViewController: UIViewController { noSelectedSegmentControl.addTarget(self, action: #selector(segmentedControl1ValueChanged(_:)), for: .valueChanged) view.addSubview(noSelectedSegmentControl) - // Control 8: Icons and labels + // Control 8: Icons with labels let iconWithLabelSegmentControl = BetterSegmentedControl( - frame: CGRect(x: 0, y: 550, width: 200, height: 40), + frame: CGRect(x: 0, y: 560, width: 200, height: 40), segments: IconWithLabelSegment.segments(withIconsAndLabels: [IconWithLabel(icon: UIImage(named: "facebook")!, title: "Facebook"), IconWithLabel(icon: UIImage(named: "twitter")!, title: "Twitter")], iconSize: CGSize(width: 20.0, height: 20.0), normalIconTintColor: .systemTeal, @@ -93,7 +93,6 @@ class ViewController: UIViewController { index: 1) iconWithLabelSegmentControl.center.x = view.center.x iconWithLabelSegmentControl.backgroundColor = #colorLiteral(red: 0.2117647059, green: 0.2705882353, blue: 0.3098039216, alpha: 1) - iconWithLabelSegmentControl.layer.cornerRadius = 10 iconWithLabelSegmentControl.cornerRadius = 10 iconWithLabelSegmentControl.indicatorView.backgroundColor = .systemTeal iconWithLabelSegmentControl.addTarget(self, action: #selector(segmentedControl1ValueChanged(_:)), for: .valueChanged) diff --git a/Pod/Classes/Segments/IconWithLabel.swift b/Pod/Classes/Segments/IconWithLabel/IconWithLabel.swift similarity index 100% rename from Pod/Classes/Segments/IconWithLabel.swift rename to Pod/Classes/Segments/IconWithLabel/IconWithLabel.swift diff --git a/Pod/Classes/Segments/IconWithLabelSegment.swift b/Pod/Classes/Segments/IconWithLabel/IconWithLabelSegment.swift similarity index 100% rename from Pod/Classes/Segments/IconWithLabelSegment.swift rename to Pod/Classes/Segments/IconWithLabel/IconWithLabelSegment.swift diff --git a/Pod/Classes/Segments/IconWithLabelView.swift b/Pod/Classes/Segments/IconWithLabel/IconWithLabelView.swift similarity index 100% rename from Pod/Classes/Segments/IconWithLabelView.swift rename to Pod/Classes/Segments/IconWithLabel/IconWithLabelView.swift