From 9b0c68218ef6f1019db9e47e4eb6eba388c79839 Mon Sep 17 00:00:00 2001 From: Zack <> Date: Wed, 2 Oct 2019 11:23:15 +0800 Subject: [PATCH 1/8] [Update] cliptobound when hide segment tab --- Example/SJSegmentedScrollViewDemo.xcodeproj/project.pbxproj | 2 ++ SJSegmentedScrollView/Classes/SJSegmentTab.swift | 1 + 2 files changed, 3 insertions(+) diff --git a/Example/SJSegmentedScrollViewDemo.xcodeproj/project.pbxproj b/Example/SJSegmentedScrollViewDemo.xcodeproj/project.pbxproj index cee8cb6..20c8170 100644 --- a/Example/SJSegmentedScrollViewDemo.xcodeproj/project.pbxproj +++ b/Example/SJSegmentedScrollViewDemo.xcodeproj/project.pbxproj @@ -189,8 +189,10 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, Base, + ar, ); mainGroup = 607FACC71AFB9204008FA782; productRefGroup = 607FACD11AFB9204008FA782 /* Products */; diff --git a/SJSegmentedScrollView/Classes/SJSegmentTab.swift b/SJSegmentedScrollView/Classes/SJSegmentTab.swift index 63ca1d3..33a5dc9 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentTab.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentTab.swift @@ -38,6 +38,7 @@ open class SJSegmentTab: UIView { convenience init(title: String) { self.init(frame: CGRect.zero) + clipsToBounds = true setTitle(title) } From b676367b5b62bdad7d7a09e6f2d5492dd5687cc0 Mon Sep 17 00:00:00 2001 From: Zack Date: Tue, 18 Aug 2020 12:37:02 +0800 Subject: [PATCH 2/8] update --- .../Classes/SJSegmentedScrollView.swift | 3 +++ .../Classes/SJSegmentedViewController.swift | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift index a322f1a..237f0dc 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift @@ -80,6 +80,8 @@ class SJSegmentedScrollView: UIScrollView { } } + var segmentRightOffset: CGFloat = 0.0 + var topSpacing: CGFloat? var bottomSpacing: CGFloat? @@ -240,6 +242,7 @@ class SJSegmentedScrollView: UIScrollView { contentViewHeightConstraint.constant = getContentHeight() contentView?.layoutIfNeeded() + segmentView?.didChangeParentViewFrame(CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.width - segmentRightOffset, height: frame.height)) segmentView?.didChangeParentViewFrame(frame) contentView?.updateContentControllersFrame(frame) diff --git a/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift b/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift index e6a5f03..ae376dc 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift @@ -184,6 +184,17 @@ import UIKit } } + /** + * Set right offset for segment. + * + * segmentedScrollView.segmentRightOffset = 0.0 + */ + open var segmentRightOffset: CGFloat = 0.0 { + didSet { + segmentedScrollView.segmentRightOffset = segmentRightOffset + } + } + /** * Set bounce for segment. * From 16992f2fa2939556fda9f99d11351365418d11b3 Mon Sep 17 00:00:00 2001 From: Zack Date: Tue, 18 Aug 2020 12:46:43 +0800 Subject: [PATCH 3/8] update --- SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift index 237f0dc..6b98735 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift @@ -243,8 +243,6 @@ class SJSegmentedScrollView: UIScrollView { contentViewHeightConstraint.constant = getContentHeight() contentView?.layoutIfNeeded() segmentView?.didChangeParentViewFrame(CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.width - segmentRightOffset, height: frame.height)) - - segmentView?.didChangeParentViewFrame(frame) contentView?.updateContentControllersFrame(frame) } From 66bd40317ae374d2c470967bc5253ca78b87808b Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 24 Oct 2021 15:59:45 +0800 Subject: [PATCH 4/8] [Update] segment tab flexi and title background color --- .../Classes/SJSegmentTab.swift | 133 ++++++++++-------- .../Classes/SJSegmentView.swift | 118 +++++++++------- .../Classes/SJSegmentedScrollView.swift | 131 +++++++++-------- .../Classes/SJSegmentedViewController.swift | 116 ++++++++------- 4 files changed, 275 insertions(+), 223 deletions(-) diff --git a/SJSegmentedScrollView/Classes/SJSegmentTab.swift b/SJSegmentedScrollView/Classes/SJSegmentTab.swift index 33a5dc9..6f62295 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentTab.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentTab.swift @@ -26,84 +26,93 @@ typealias DidSelectSegmentAtIndex = (_ segment: SJSegmentTab?,_ index: Int,_ ani open class SJSegmentTab: UIView { - let kSegmentViewTagOffset = 100 - let button = UIButton(type: .custom) - - var didSelectSegmentAtIndex: DidSelectSegmentAtIndex? - var isSelected = false { - didSet { - button.isSelected = isSelected - } - } - - convenience init(title: String) { - self.init(frame: CGRect.zero) + let kSegmentViewTagOffset = 100 + let button = UIButton(type: .custom) + + var didSelectSegmentAtIndex: DidSelectSegmentAtIndex? + var isSelected = false { + didSet { + button.isSelected = isSelected + layer.masksToBounds = true + layer.cornerRadius = 15 + } + } + var titleBackgroundColor: UIColor?{ + didSet{ + if let titleBackgroundColor = titleBackgroundColor{ + button.backgroundColor = titleBackgroundColor + } + } + } + + convenience init(title: String) { + self.init(frame: CGRect.zero) clipsToBounds = true setTitle(title) - } - - convenience init(view: UIView) { - self.init(frame: CGRect.zero) - - insertSubview(view, at: 0) - view.removeConstraints(view.constraints) - addConstraintsToView(view) - } - - required override public init(frame: CGRect) { - super.init(frame: frame) - - translatesAutoresizingMaskIntoConstraints = false - button.frame = bounds - button.addTarget(self, action: #selector(SJSegmentTab.onSegmentButtonPress(_:)), - for: .touchUpInside) - addSubview(button) - addConstraintsToView(button) - } - - func addConstraintsToView(_ view: UIView) { - - view.translatesAutoresizingMaskIntoConstraints = false - let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", - options: [], - metrics: nil, - views: ["view": view]) - let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", - options: [], - metrics: nil, - views: ["view": view]) - addConstraints(verticalConstraints) - addConstraints(horizontalConstraints) - } - - required public init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } + } + + convenience init(view: UIView) { + self.init(frame: CGRect.zero) + + insertSubview(view, at: 0) + view.removeConstraints(view.constraints) + addConstraintsToView(view) + } + + required override public init(frame: CGRect) { + super.init(frame: frame) + + translatesAutoresizingMaskIntoConstraints = false + button.frame = bounds + button.addTarget(self, action: #selector(SJSegmentTab.onSegmentButtonPress(_:)), + for: .touchUpInside) + addSubview(button) + addConstraintsToView(button) + } + + func addConstraintsToView(_ view: UIView) { + + view.translatesAutoresizingMaskIntoConstraints = false + let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", + options: [], + metrics: nil, + views: ["view": view]) + let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", + options: [], + metrics: nil, + views: ["view": view]) + addConstraints(verticalConstraints) + addConstraints(horizontalConstraints) + } + + required public init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } open func setTitle(_ title: String) { button.setTitle(title, for: .normal) } - open func titleColor(_ color: UIColor) { + open func titleColor(_ color: UIColor) { - button.setTitleColor(color, for: .normal) - } + button.setTitleColor(color, for: .normal) + } open func selectedTitleColor(_ color: UIColor?) { button.setTitleColor(color, for: .selected) } - open func titleFont(_ font: UIFont) { + open func titleFont(_ font: UIFont) { - button.titleLabel?.font = font - } + button.titleLabel?.font = font + } - @objc func onSegmentButtonPress(_ sender: AnyObject) { - let index = tag - kSegmentViewTagOffset - NotificationCenter.default.post(name: Notification.Name(rawValue: "DidChangeSegmentIndex"), - object: index) + @objc func onSegmentButtonPress(_ sender: AnyObject) { + let index = tag - kSegmentViewTagOffset + NotificationCenter.default.post(name: Notification.Name(rawValue: "DidChangeSegmentIndex"), + object: index) didSelectSegmentAtIndex?(self, index, true) - } + } } diff --git a/SJSegmentedScrollView/Classes/SJSegmentView.swift b/SJSegmentedScrollView/Classes/SJSegmentView.swift index 90c2543..3b1944c 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentView.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentView.swift @@ -54,6 +54,14 @@ class SJSegmentView: UIScrollView { } } + var segmentTitleBackgroundColor: UIColor? { + didSet { + for segment in segments { + segment.titleBackgroundColor = segmentTitleBackgroundColor + } + } + } + var shadow: SJShadow? { didSet { if let shadow = shadow { @@ -78,7 +86,8 @@ class SJSegmentView: UIScrollView { var contentViewWidthConstraint: NSLayoutConstraint? var selectedSegmentViewWidthConstraint: NSLayoutConstraint? var contentSubViewWidthConstraints = [NSLayoutConstraint]() - var controllers: [UIViewController]? + var controllers: [UIViewController]? + var fixWidth: Bool = true var contentView: SJContentView? { didSet { @@ -92,15 +101,15 @@ class SJSegmentView: UIScrollView { required override init(frame: CGRect) { super.init(frame: frame) - showsHorizontalScrollIndicator = false - showsVerticalScrollIndicator = false - bounces = false + showsHorizontalScrollIndicator = false + showsVerticalScrollIndicator = false + bounces = false - NotificationCenter.default.addObserver(self, - selector: #selector(SJSegmentView.didChangeSegmentIndex(_:)), - name: NSNotification.Name("DidChangeSegmentIndex"), - object: nil) + NotificationCenter.default.addObserver(self, + selector: #selector(SJSegmentView.didChangeSegmentIndex(_:)), + name: NSNotification.Name("DidChangeSegmentIndex"), + object: nil) } required init?(coder aDecoder: NSCoder) { @@ -127,10 +136,10 @@ class SJSegmentView: UIScrollView { // select current button let index = notification.object as? Int - if index! < segments.count { - let button = segments[index!] - button.isSelected = true - } + if index! < segments.count { + let button = segments[index!] + button.isSelected = true + } } func setSegmentsView(_ frame: CGRect) { @@ -216,7 +225,7 @@ class SJSegmentView: UIScrollView { toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, - constant: width) + constant: fixWidth ? width : segmentView.frame.size.width) segmentContentView!.addConstraint(widthConstraint) contentSubViewWidthConstraints.append(widthConstraint) @@ -262,63 +271,64 @@ class SJSegmentView: UIScrollView { func getSegmentTabForController(_ controller: UIViewController) -> SJSegmentTab { - var segmentTab: SJSegmentTab? + var segmentTab: SJSegmentTab? - if controller.navigationItem.titleView != nil { - segmentTab = SJSegmentTab.init(view: controller.navigationItem.titleView!) - } else { + if controller.navigationItem.titleView != nil { + segmentTab = SJSegmentTab.init(view: controller.navigationItem.titleView!) + } else { - if let title = controller.title { - segmentTab = SJSegmentTab.init(title: title) - } else { - segmentTab = SJSegmentTab.init(title: "") - } + if let title = controller.title { + segmentTab = SJSegmentTab.init(title: title) + } else { + segmentTab = SJSegmentTab.init(title: "") + } - segmentTab?.backgroundColor = segmentBackgroundColor - segmentTab?.titleColor(titleColor!) + segmentTab?.backgroundColor = segmentBackgroundColor + segmentTab?.titleBackgroundColor = segmentTitleBackgroundColor + segmentTab?.titleColor(titleColor!) segmentTab?.selectedTitleColor(selectedTitleColor!) - segmentTab?.titleFont(font!) - } + segmentTab?.titleFont(font!) + } - segmentTab?.didSelectSegmentAtIndex = didSelectSegmentAtIndex + segmentTab?.didSelectSegmentAtIndex = didSelectSegmentAtIndex return segmentTab! } - func widthForSegment(_ frame: CGRect) -> CGFloat { + func widthForSegment(_ frame: CGRect) -> CGFloat { - var maxWidth: CGFloat = 0 - for controller in controllers! { + var maxWidth: CGFloat = 0 + for controller in controllers! { - var width: CGFloat = 0.0 - if let view = controller.navigationItem.titleView { - width = view.bounds.width - } else if let title = controller.title { + var width: CGFloat = 0.0 + if let view = controller.navigationItem.titleView { + width = view.bounds.width + } else if let title = controller.title { - width = title.widthWithConstrainedWidth(.greatestFiniteMagnitude, - font: font!) - } + width = title.widthWithConstrainedWidth(.greatestFiniteMagnitude, + font: font!) + } - if width > maxWidth { - maxWidth = width - } - } + if width > maxWidth { + maxWidth = width + } + } - let width = Int(maxWidth + segmentViewOffsetWidth) - let totalWidth = width * (controllers?.count)! - if totalWidth < Int(frame.size.width) { - maxWidth = frame.size.width / CGFloat((controllers?.count)!) - } else { - maxWidth = CGFloat(width) - } + let width = Int(maxWidth + segmentViewOffsetWidth) + let totalWidth = width * (controllers?.count)! + if totalWidth < Int(frame.size.width) { + maxWidth = frame.size.width / CGFloat((controllers?.count)!) + } else { + maxWidth = CGFloat(width) + } - return maxWidth - } + return maxWidth + } - override func observeValue(forKeyPath keyPath: String?, - of object: Any?, - change: [NSKeyValueChangeKey : Any]?, - context: UnsafeMutableRawPointer?) { + override func observeValue(forKeyPath keyPath: String?, + of object: Any?, + change: [NSKeyValueChangeKey : Any]?, + context: UnsafeMutableRawPointer?) { if let change = change as [NSKeyValueChangeKey : AnyObject]? { if let old = change[NSKeyValueChangeKey.oldKey], let new = change[NSKeyValueChangeKey.newKey] { diff --git a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift index 6b98735..b585dfb 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift @@ -68,6 +68,12 @@ class SJSegmentedScrollView: UIScrollView { } } + var segmentTitleBackgroundColor: UIColor? { + didSet { + segmentView?.segmentTitleBackgroundColor = segmentTitleBackgroundColor + } + } + var segmentShadow: SJShadow? { didSet { segmentView?.shadow = segmentShadow @@ -80,6 +86,12 @@ class SJSegmentedScrollView: UIScrollView { } } + var fixWidth: Bool { + didSet { + segmentView?.fixWidth = fixWidth + } + } + var segmentRightOffset: CGFloat = 0.0 var topSpacing: CGFloat? @@ -102,19 +114,19 @@ class SJSegmentedScrollView: UIScrollView { var didSelectSegmentAtIndex: DidSelectSegmentAtIndex? - var sjShowsVerticalScrollIndicator: Bool = false { - didSet { - showsVerticalScrollIndicator = sjShowsVerticalScrollIndicator - contentView?.showsVerticalScrollIndicator = sjShowsVerticalScrollIndicator - } - } + var sjShowsVerticalScrollIndicator: Bool = false { + didSet { + showsVerticalScrollIndicator = sjShowsVerticalScrollIndicator + contentView?.showsVerticalScrollIndicator = sjShowsVerticalScrollIndicator + } + } - var sjShowsHorizontalScrollIndicator: Bool = false { - didSet { - showsHorizontalScrollIndicator = sjShowsHorizontalScrollIndicator - contentView?.showsHorizontalScrollIndicator = sjShowsHorizontalScrollIndicator - } - } + var sjShowsHorizontalScrollIndicator: Bool = false { + didSet { + showsHorizontalScrollIndicator = sjShowsHorizontalScrollIndicator + contentView?.showsHorizontalScrollIndicator = sjShowsHorizontalScrollIndicator + } + } private var viewObservers = [UIView]() var sjDisableScrollOnContentView: Bool = false { @@ -248,7 +260,7 @@ class SJSegmentedScrollView: UIScrollView { //MARK: Private Functions func getContentHeight() -> CGFloat { - + var contentHeight = (superview?.bounds.height)! + headerViewHeight! contentHeight -= (topSpacing! + bottomSpacing! + headerViewOffsetHeight!) return contentHeight @@ -261,16 +273,17 @@ class SJSegmentedScrollView: UIScrollView { if controllers.count > 1 { segmentView = SJSegmentView(frame: CGRect.zero) - segmentView?.controllers = controllers - segmentView?.selectedSegmentViewColor = selectedSegmentViewColor - segmentView?.selectedSegmentViewHeight = selectedSegmentViewHeight! - segmentView?.titleColor = segmentTitleColor + segmentView?.controllers = controllers + segmentView?.selectedSegmentViewColor = selectedSegmentViewColor + segmentView?.selectedSegmentViewHeight = selectedSegmentViewHeight! + segmentView?.titleColor = segmentTitleColor segmentView?.selectedTitleColor = segmentSelectedTitleColor - segmentView?.segmentBackgroundColor = segmentBackgroundColor - segmentView?.font = segmentTitleFont! - segmentView?.shadow = segmentShadow - segmentView?.font = segmentTitleFont! - segmentView?.bounces = false + segmentView?.segmentBackgroundColor = segmentBackgroundColor + segmentView?.segmentTitleBackgroundColor = segmentTitleBackgroundColor + segmentView?.font = segmentTitleFont! + segmentView?.shadow = segmentShadow + segmentView?.font = segmentTitleFont! + segmentView?.bounces = false segmentView!.translatesAutoresizingMaskIntoConstraints = false segmentView!.didSelectSegmentAtIndex = {[unowned self] (segment, index, animated) in @@ -317,11 +330,11 @@ class SJSegmentedScrollView: UIScrollView { func createContentView() -> SJContentView { let contentView = SJContentView(frame: CGRect.zero) - contentView.showsVerticalScrollIndicator = sjShowsVerticalScrollIndicator - contentView.showsHorizontalScrollIndicator = sjShowsHorizontalScrollIndicator + contentView.showsVerticalScrollIndicator = sjShowsVerticalScrollIndicator + contentView.showsHorizontalScrollIndicator = sjShowsHorizontalScrollIndicator contentView.isScrollEnabled = !sjDisableScrollOnContentView contentView.translatesAutoresizingMaskIntoConstraints = false - contentView.bounces = segmentBounces + contentView.bounces = segmentBounces scrollContentView.addSubview(contentView) let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[contentView]-0-|", @@ -345,18 +358,18 @@ class SJSegmentedScrollView: UIScrollView { change: CGFloat, oldPosition: CGPoint) { - if headerViewHeight != 0.0 && contentOffset.y != 0.0 { - if scrollView.contentOffset.y < 0.0 { - if contentOffset.y >= 0.0 { + if headerViewHeight != 0.0 && contentOffset.y != 0.0 { + if scrollView.contentOffset.y < 0.0 { + if contentOffset.y >= 0.0 { - var yPos = contentOffset.y - change - yPos = yPos < 0 ? 0 : yPos - let updatedPos = CGPoint(x: contentOffset.x, y: yPos) - setContentOffset(self, point: updatedPos) - setContentOffset(scrollView, point: oldPosition) - } - } - } + var yPos = contentOffset.y - change + yPos = yPos < 0 ? 0 : yPos + let updatedPos = CGPoint(x: contentOffset.x, y: yPos) + setContentOffset(self, point: updatedPos) + setContentOffset(scrollView, point: oldPosition) + } + } + } } func handleScrollDown(_ scrollView: UIScrollView, @@ -378,36 +391,36 @@ class SJSegmentedScrollView: UIScrollView { } } - override func observeValue(forKeyPath keyPath: String?, - of object: Any?, - change: [NSKeyValueChangeKey : Any]?, - context: UnsafeMutableRawPointer?) { - if !observing { return } + override func observeValue(forKeyPath keyPath: String?, + of object: Any?, + change: [NSKeyValueChangeKey : Any]?, + context: UnsafeMutableRawPointer?) { + if !observing { return } - let scrollView = object as? UIScrollView - if scrollView == nil { return } - if scrollView == self { return } + let scrollView = object as? UIScrollView + if scrollView == nil { return } + if scrollView == self { return } - let changeValues = change! as [NSKeyValueChangeKey: AnyObject] + let changeValues = change! as [NSKeyValueChangeKey: AnyObject] - if let new = changeValues[NSKeyValueChangeKey.newKey]?.cgPointValue, - let old = changeValues[NSKeyValueChangeKey.oldKey]?.cgPointValue { + if let new = changeValues[NSKeyValueChangeKey.newKey]?.cgPointValue, + let old = changeValues[NSKeyValueChangeKey.oldKey]?.cgPointValue { - let diff = old.y - new.y + let diff = old.y - new.y - if diff > 0.0 { + if diff > 0.0 { - handleScrollUp(scrollView!, - change: diff, - oldPosition: old) - } else { + handleScrollUp(scrollView!, + change: diff, + oldPosition: old) + } else { - handleScrollDown(scrollView!, - change: diff, - oldPosition: old) - } - } - } + handleScrollDown(scrollView!, + change: diff, + oldPosition: old) + } + } + } func setContentOffset(_ scrollView: UIScrollView, point: CGPoint) { observing = false diff --git a/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift b/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift index ae376dc..a1121ec 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift @@ -160,6 +160,19 @@ import UIKit } } + /** + * Set color for segment background. + * + * By default the color is white. + * + * segmentedViewController.segmentBackgroundColor = UIColor.whiteColor() + */ + open var segmentTitleBackgroundColor = UIColor.clear { + didSet { + segmentedScrollView.segmentTitleBackgroundColor = segmentTitleBackgroundColor + } + } + /** * Set shadow for segment. * @@ -253,31 +266,31 @@ import UIKit } } - /** - * Set vertical scroll indicator. - * - * By default true. - * - * segmentedScrollView.showsVerticalScrollIndicator = false - */ - open var showsVerticalScrollIndicator = true { - didSet { - segmentedScrollView.sjShowsVerticalScrollIndicator = showsVerticalScrollIndicator - } - } + /** + * Set vertical scroll indicator. + * + * By default true. + * + * segmentedScrollView.showsVerticalScrollIndicator = false + */ + open var showsVerticalScrollIndicator = true { + didSet { + segmentedScrollView.sjShowsVerticalScrollIndicator = showsVerticalScrollIndicator + } + } - /** - * Set horizontal scroll indicator. - * - * By default true. - * - * segmentedScrollView.showsHorizontalScrollIndicator = false - */ - open var showsHorizontalScrollIndicator = true { - didSet { - segmentedScrollView.sjShowsHorizontalScrollIndicator = showsHorizontalScrollIndicator - } - } + /** + * Set horizontal scroll indicator. + * + * By default true. + * + * segmentedScrollView.showsHorizontalScrollIndicator = false + */ + open var showsHorizontalScrollIndicator = true { + didSet { + segmentedScrollView.sjShowsHorizontalScrollIndicator = showsHorizontalScrollIndicator + } + } /** * Disable scroll on contentView. @@ -292,6 +305,12 @@ import UIKit } } + open var fixWidth: Bool { + didSet { + segmentedScrollView.fixWidth = fixWidth + } + } + open weak var delegate:SJSegmentedViewControllerDelegate? var segmentedScrollView = SJSegmentedScrollView(frame: CGRect.zero) var segmentScrollViewTopConstraint: NSLayoutConstraint? @@ -317,7 +336,7 @@ import UIKit } required public init?(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) + super.init(coder: aDecoder) } override open func loadView() { @@ -330,11 +349,11 @@ import UIKit view.backgroundColor = UIColor.white loadControllers() - if #available(iOS 11, *) { - segmentedScrollView.contentInsetAdjustmentBehavior = .never - } else { - automaticallyAdjustsScrollViewInsets = false - } + if #available(iOS 11, *) { + segmentedScrollView.contentInsetAdjustmentBehavior = .never + } else { + automaticallyAdjustsScrollViewInsets = false + } } /** @@ -350,21 +369,21 @@ import UIKit segmentedScrollView.updateSubviewsFrame(view.bounds) } - /** - * To select segment programmatically - * - parameter index Int Segment index - * - parameter animated Bool Move with an animation or not. - */ - open func setSelectedSegmentAt(_ index: Int, animated: Bool) { + /** + * To select segment programmatically + * - parameter index Int Segment index + * - parameter animated Bool Move with an animation or not. + */ + open func setSelectedSegmentAt(_ index: Int, animated: Bool) { - if index >= 0 && index < segmentControllers.count { - segmentedScrollView.segmentView?.didSelectSegmentAtIndex!(segments[index], - index, - animated) - NotificationCenter.default.post(name: Notification.Name(rawValue: "DidChangeSegmentIndex"), - object: index) - } - } + if index >= 0 && index < segmentControllers.count { + segmentedScrollView.segmentView?.didSelectSegmentAtIndex!(segments[index], + index, + animated) + NotificationCenter.default.post(name: Notification.Name(rawValue: "DidChangeSegmentIndex"), + object: index) + } + } /** * Set the default values for the segmented scroll view. @@ -376,6 +395,7 @@ import UIKit segmentedScrollView.segmentTitleColor = segmentTitleColor segmentedScrollView.segmentSelectedTitleColor = segmentSelectedTitleColor segmentedScrollView.segmentBackgroundColor = segmentBackgroundColor + segmentedScrollView.segmentTitleBackgroundColor = segmentTitleBackgroundColor segmentedScrollView.segmentShadow = segmentShadow segmentedScrollView.segmentTitleFont = segmentTitleFont segmentedScrollView.segmentBounces = segmentBounces @@ -461,9 +481,9 @@ import UIKit let delegate = controller as? SJSegmentedViewControllerViewSource var observeView = controller.view - if let collectionController = controller as? UICollectionViewController { - observeView = collectionController.collectionView - } + if let collectionController = controller as? UICollectionViewController { + observeView = collectionController.collectionView + } if let view = delegate?.viewForSegmentControllerToObserveContentOffsetChange?() { observeView = view @@ -483,7 +503,7 @@ import UIKit if headerViewController == nil { headerViewController = UIViewController() - headerViewHeight = 0.0 + headerViewHeight = 0.0 } addHeaderViewController(headerViewController!) From 18b6812e1bafe549381002b635316bbdf619a989 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 24 Oct 2021 16:04:54 +0800 Subject: [PATCH 5/8] udpate --- SJSegmentedScrollView/Classes/SJSegmentTab.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SJSegmentedScrollView/Classes/SJSegmentTab.swift b/SJSegmentedScrollView/Classes/SJSegmentTab.swift index 6f62295..81b8033 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentTab.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentTab.swift @@ -41,6 +41,8 @@ open class SJSegmentTab: UIView { didSet{ if let titleBackgroundColor = titleBackgroundColor{ button.backgroundColor = titleBackgroundColor + layer.masksToBounds = true + layer.cornerRadius = 15 } } } From 8ad01da2cb9c9726cde66497e46ca32386d57962 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 24 Oct 2021 16:06:29 +0800 Subject: [PATCH 6/8] update --- SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift | 2 +- SJSegmentedScrollView/Classes/SJSegmentedViewController.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift index b585dfb..298149f 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift @@ -86,7 +86,7 @@ class SJSegmentedScrollView: UIScrollView { } } - var fixWidth: Bool { + var fixWidth: Bool = true { didSet { segmentView?.fixWidth = fixWidth } diff --git a/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift b/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift index a1121ec..7c788e5 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift @@ -305,7 +305,7 @@ import UIKit } } - open var fixWidth: Bool { + open var fixWidth: Bool = true { didSet { segmentedScrollView.fixWidth = fixWidth } From 2a6f56fc14e9ae1b7194017cf2be477348151d88 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 24 Oct 2021 19:10:45 +0800 Subject: [PATCH 7/8] update --- SJSegmentedScrollView/Classes/SJSegmentTab.swift | 8 +++----- SJSegmentedScrollView/Classes/SJSegmentView.swift | 8 ++++---- SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift | 1 + 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/SJSegmentedScrollView/Classes/SJSegmentTab.swift b/SJSegmentedScrollView/Classes/SJSegmentTab.swift index 81b8033..e495afe 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentTab.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentTab.swift @@ -33,24 +33,22 @@ open class SJSegmentTab: UIView { var isSelected = false { didSet { button.isSelected = isSelected - layer.masksToBounds = true - layer.cornerRadius = 15 } } var titleBackgroundColor: UIColor?{ didSet{ if let titleBackgroundColor = titleBackgroundColor{ button.backgroundColor = titleBackgroundColor - layer.masksToBounds = true - layer.cornerRadius = 15 } } } - convenience init(title: String) { + convenience init(title: String, cornerRadius: CGFloat) { self.init(frame: CGRect.zero) clipsToBounds = true setTitle(title) + layer.masksToBounds = true + layer.cornerRadius = cornerRadius } convenience init(view: UIView) { diff --git a/SJSegmentedScrollView/Classes/SJSegmentView.swift b/SJSegmentedScrollView/Classes/SJSegmentView.swift index 3b1944c..76d743f 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentView.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentView.swift @@ -77,7 +77,7 @@ class SJSegmentView: UIScrollView { var font: UIFont? var selectedSegmentViewHeight: CGFloat? let kSegmentViewTagOffset = 100 - var segmentViewOffsetWidth: CGFloat = 10.0 + var segmentViewOffsetWidth: CGFloat = 20.0 var segments = [SJSegmentTab]() var segmentContentView: UIView? var didSelectSegmentAtIndex: DidSelectSegmentAtIndex? @@ -225,7 +225,7 @@ class SJSegmentView: UIScrollView { toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, - constant: fixWidth ? width : segmentView.frame.size.width) + constant: fixWidth ? width + segmentViewOffsetWidth : segmentView.frame.size.width) segmentContentView!.addConstraint(widthConstraint) contentSubViewWidthConstraints.append(widthConstraint) @@ -278,9 +278,9 @@ class SJSegmentView: UIScrollView { } else { if let title = controller.title { - segmentTab = SJSegmentTab.init(title: title) + segmentTab = SJSegmentTab.init(title: title, cornerRadius: fixWidth ? 0 : 15) } else { - segmentTab = SJSegmentTab.init(title: "") + segmentTab = SJSegmentTab.init(title: "", cornerRadius: fixWidth ? 0 : 15) } segmentTab?.backgroundColor = segmentBackgroundColor diff --git a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift index 298149f..3536c02 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift @@ -283,6 +283,7 @@ class SJSegmentedScrollView: UIScrollView { segmentView?.font = segmentTitleFont! segmentView?.shadow = segmentShadow segmentView?.font = segmentTitleFont! + segmentView?.fixWidth = fixWidth segmentView?.bounces = false segmentView!.translatesAutoresizingMaskIntoConstraints = false segmentView!.didSelectSegmentAtIndex = {[unowned self] From 236d712ef8dd272509fefaeb9a00dc1e9323ae53 Mon Sep 17 00:00:00 2001 From: Zack Date: Mon, 8 May 2023 18:45:13 +0800 Subject: [PATCH 8/8] update code --- SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift | 8 ++++---- .../Classes/SJSegmentedViewController.swift | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift index 3536c02..386d0de 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentedScrollView.swift @@ -49,6 +49,7 @@ class SJSegmentedScrollView: UIScrollView { var selectedSegmentViewHeight: CGFloat! = 0 var segmentBounces = false + var hideSingleItem = true var segmentTitleColor: UIColor! = UIColor.red { didSet { @@ -270,7 +271,9 @@ class SJSegmentedScrollView: UIScrollView { func addSegmentView(_ controllers: [UIViewController], frame: CGRect) { - if controllers.count > 1 { + if(hideSingleItem && controllers.count == 1){ + segmentViewHeight = 0.0 + } else { segmentView = SJSegmentView(frame: CGRect.zero) segmentView?.controllers = controllers @@ -310,9 +313,6 @@ class SJSegmentedScrollView: UIScrollView { addConstraints(verticalConstraints) segmentViewHeightConstraint = verticalConstraints[1] - } else { - - segmentViewHeight = 0.0 } } diff --git a/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift b/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift index 7c788e5..cc4e334 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentedViewController.swift @@ -316,6 +316,13 @@ import UIKit var segmentScrollViewTopConstraint: NSLayoutConstraint? + + open var hideSingleItem: Bool = true { + didSet { + segmentedScrollView.hideSingleItem = hideSingleItem + } + } + /** Custom initializer for SJSegmentedViewController.