diff --git a/Segmentio.xcodeproj/project.pbxproj b/Segmentio.xcodeproj/project.pbxproj index 749dced..1035680 100644 --- a/Segmentio.xcodeproj/project.pbxproj +++ b/Segmentio.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ 324B605E1D33E5200050B083 /* UIView+Appearance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 324B60541D33E5200050B083 /* UIView+Appearance.swift */; }; 324B605F1D33E5200050B083 /* Segmentio.swift in Sources */ = {isa = PBXBuildFile; fileRef = 324B60551D33E5200050B083 /* Segmentio.swift */; }; 324B60601D33E5200050B083 /* SegmentioOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 324B60561D33E5200050B083 /* SegmentioOptions.swift */; }; + B6D194A6234F1D2600314C03 /* SegmentioCellWithImageOrLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D194A5234F1D2600314C03 /* SegmentioCellWithImageOrLabel.swift */; }; C5514DD81DAB5B82008F14AA /* BadgeViewPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5514DD31DAB5B82008F14AA /* BadgeViewPresenter.swift */; }; C5514DD91DAB5B82008F14AA /* BadgeWithCounterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5514DD51DAB5B82008F14AA /* BadgeWithCounterView.swift */; }; C5514DDA1DAB5B82008F14AA /* BadgeWithCounterViewBigSized.xib in Resources */ = {isa = PBXBuildFile; fileRef = C5514DD61DAB5B82008F14AA /* BadgeWithCounterViewBigSized.xib */; }; @@ -38,6 +39,7 @@ 324B60541D33E5200050B083 /* UIView+Appearance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Appearance.swift"; sourceTree = ""; }; 324B60551D33E5200050B083 /* Segmentio.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Segmentio.swift; sourceTree = ""; }; 324B60561D33E5200050B083 /* SegmentioOptions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SegmentioOptions.swift; sourceTree = ""; }; + B6D194A5234F1D2600314C03 /* SegmentioCellWithImageOrLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentioCellWithImageOrLabel.swift; sourceTree = ""; }; C5514DD31DAB5B82008F14AA /* BadgeViewPresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BadgeViewPresenter.swift; sourceTree = ""; }; C5514DD51DAB5B82008F14AA /* BadgeWithCounterView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BadgeWithCounterView.swift; sourceTree = ""; }; C5514DD61DAB5B82008F14AA /* BadgeWithCounterViewBigSized.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = BadgeWithCounterViewBigSized.xib; sourceTree = ""; }; @@ -103,6 +105,7 @@ 324B60501D33E5200050B083 /* SegmentioCellWithImageOverLabel.swift */, 324B60511D33E5200050B083 /* SegmentioCellWithImageUnderLabel.swift */, 324B60521D33E5200050B083 /* SegmentioCellWithLabel.swift */, + B6D194A5234F1D2600314C03 /* SegmentioCellWithImageOrLabel.swift */, ); path = Cells; sourceTree = ""; @@ -227,6 +230,7 @@ 324B605F1D33E5200050B083 /* Segmentio.swift in Sources */, 324B60581D33E5200050B083 /* SegmentioCellWithImage.swift in Sources */, 324B605A1D33E5200050B083 /* SegmentioCellWithImageBeforeLabel.swift in Sources */, + B6D194A6234F1D2600314C03 /* SegmentioCellWithImageOrLabel.swift in Sources */, 324B605C1D33E5200050B083 /* SegmentioCellWithImageUnderLabel.swift in Sources */, 324B605E1D33E5200050B083 /* UIView+Appearance.swift in Sources */, 324B605D1D33E5200050B083 /* SegmentioCellWithLabel.swift in Sources */, diff --git a/Segmentio/Source/Cells/SegmentioCellWithImageOrLabel.swift b/Segmentio/Source/Cells/SegmentioCellWithImageOrLabel.swift new file mode 100644 index 0000000..501da11 --- /dev/null +++ b/Segmentio/Source/Cells/SegmentioCellWithImageOrLabel.swift @@ -0,0 +1,83 @@ +// +// SegmentioCellWithImageOrLabel.swift +// Segmentio +// +// Created by Ahsan Ali on 10/10/2019. +// + +import UIKit + +class SegmentioCellWithImageOrLabel: SegmentioCell { + + override func setupConstraintsForSubviews() { + super.setupConstraintsForSubviews() + guard let imageContainerView = imageContainerView else { + return + } + guard let containerView = containerView else { + return + } + + let views = [ + "imageContainerView": imageContainerView, + "containerView": containerView, + "contentView": contentView + ] + + // main constraints + let segmentImageViewHorizontConstraint = NSLayoutConstraint.constraints( + withVisualFormat: "V:[contentView]-(<=1)-[imageContainerView]", + options: [.alignAllCenterX], + metrics: nil, + views: views) + NSLayoutConstraint.activate(segmentImageViewHorizontConstraint) + + let segmentImageViewVerticleConstraint = NSLayoutConstraint.constraints( + withVisualFormat: "H:[contentView]-(<=1)-[imageContainerView]", + options: [.alignAllCenterY], + metrics: nil, + views: views) + NSLayoutConstraint.activate(segmentImageViewVerticleConstraint) + + + let segmentTitleLabelHorizontConstraint = NSLayoutConstraint.constraints( + withVisualFormat: "V:[contentView]-(<=1)-[containerView]", + options: [.alignAllCenterX], + metrics: nil, + views: views + ) + NSLayoutConstraint.activate(segmentTitleLabelHorizontConstraint) + + let segmentTitleLabelVerticleConstraint = NSLayoutConstraint.constraints( + withVisualFormat: "H:[contentView]-(<=1)-[containerView]", + options: [.alignAllCenterY], + metrics: nil, + views: views + ) + NSLayoutConstraint.activate(segmentTitleLabelVerticleConstraint) + + // custom constraints + topConstraint = NSLayoutConstraint( + item: containerView, + attribute: .top, + relatedBy: .equal, + toItem: contentView, + attribute: .top, + multiplier: 1, + constant: padding + ) + topConstraint?.isActive = true + + bottomConstraint = NSLayoutConstraint( + item: contentView, + attribute: .bottom, + relatedBy: .equal, + toItem: imageContainerView, + attribute: .bottom, + multiplier: 1, + constant: padding + ) + bottomConstraint?.isActive = true + } + +} diff --git a/Segmentio/Source/Segmentio.swift b/Segmentio/Source/Segmentio.swift index f1f2de8..88cce5f 100644 --- a/Segmentio/Source/Segmentio.swift +++ b/Segmentio/Source/Segmentio.swift @@ -215,6 +215,8 @@ open class Segmentio: UIView { return SegmentioCellWithImageBeforeLabel.self case .imageAfterLabel: return SegmentioCellWithImageAfterLabel.self + case .imageOrLabel: + return SegmentioCellWithImageOrLabel.self } } diff --git a/Segmentio/Source/SegmentioOptions.swift b/Segmentio/Source/SegmentioOptions.swift index c711ede..082b7a4 100644 --- a/Segmentio/Source/SegmentioOptions.swift +++ b/Segmentio/Source/SegmentioOptions.swift @@ -141,7 +141,7 @@ public enum SegmentioPosition { public enum SegmentioStyle: String { - case onlyLabel, onlyImage, imageOverLabel, imageUnderLabel, imageBeforeLabel, imageAfterLabel + case onlyLabel, onlyImage, imageOverLabel, imageUnderLabel, imageBeforeLabel, imageAfterLabel, imageOrLabel public static let allStyles = [ onlyLabel, @@ -149,12 +149,13 @@ public enum SegmentioStyle: String { imageOverLabel, imageUnderLabel, imageBeforeLabel, - imageAfterLabel + imageAfterLabel, + imageOrLabel ] public func isWithText() -> Bool { switch self { - case .onlyLabel, .imageOverLabel, .imageUnderLabel, .imageBeforeLabel, .imageAfterLabel: + case .onlyLabel, .imageOverLabel, .imageUnderLabel, .imageBeforeLabel, .imageAfterLabel,.imageOrLabel: return true default: return false @@ -163,7 +164,7 @@ public enum SegmentioStyle: String { public func isWithImage() -> Bool { switch self { - case .imageOverLabel, .imageUnderLabel, .imageBeforeLabel, .imageAfterLabel, .onlyImage: + case .imageOverLabel, .imageUnderLabel, .imageBeforeLabel, .imageAfterLabel, .onlyImage,.imageOrLabel: return true default: return false @@ -173,7 +174,7 @@ public enum SegmentioStyle: String { public var layoutMargins: CGFloat { let defaultLayoutMargins: CGFloat = 8.0 switch self { - case .onlyLabel, .imageAfterLabel, .imageBeforeLabel, .imageOverLabel, .imageUnderLabel: + case .onlyLabel, .imageAfterLabel, .imageBeforeLabel, .imageOverLabel, .imageUnderLabel,.imageOrLabel: return 4 * defaultLayoutMargins case .onlyImage: return 2 * defaultLayoutMargins