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..e495afe 100755 --- a/SJSegmentedScrollView/Classes/SJSegmentTab.swift +++ b/SJSegmentedScrollView/Classes/SJSegmentTab.swift @@ -26,83 +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 + } + } + var titleBackgroundColor: UIColor?{ + didSet{ + if let titleBackgroundColor = titleBackgroundColor{ + button.backgroundColor = titleBackgroundColor + } + } + } + + convenience init(title: String, cornerRadius: CGFloat) { + 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") - } + layer.masksToBounds = true + layer.cornerRadius = cornerRadius + } + + 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..76d743f 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 { @@ -69,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? @@ -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 + segmentViewOffsetWidth : 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, cornerRadius: fixWidth ? 0 : 15) + } else { + segmentTab = SJSegmentTab.init(title: "", cornerRadius: fixWidth ? 0 : 15) + } - 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 a322f1a..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 { @@ -68,6 +69,12 @@ class SJSegmentedScrollView: UIScrollView { } } + var segmentTitleBackgroundColor: UIColor? { + didSet { + segmentView?.segmentTitleBackgroundColor = segmentTitleBackgroundColor + } + } + var segmentShadow: SJShadow? { didSet { segmentView?.shadow = segmentShadow @@ -80,6 +87,14 @@ class SJSegmentedScrollView: UIScrollView { } } + var fixWidth: Bool = true { + didSet { + segmentView?.fixWidth = fixWidth + } + } + + var segmentRightOffset: CGFloat = 0.0 + var topSpacing: CGFloat? var bottomSpacing: CGFloat? @@ -100,19 +115,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 { @@ -240,14 +255,13 @@ class SJSegmentedScrollView: UIScrollView { contentViewHeightConstraint.constant = getContentHeight() contentView?.layoutIfNeeded() - - segmentView?.didChangeParentViewFrame(frame) + segmentView?.didChangeParentViewFrame(CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.width - segmentRightOffset, height: frame.height)) contentView?.updateContentControllersFrame(frame) } //MARK: Private Functions func getContentHeight() -> CGFloat { - + var contentHeight = (superview?.bounds.height)! + headerViewHeight! contentHeight -= (topSpacing! + bottomSpacing! + headerViewOffsetHeight!) return contentHeight @@ -257,19 +271,23 @@ 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 - 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?.fixWidth = fixWidth + segmentView?.bounces = false segmentView!.translatesAutoresizingMaskIntoConstraints = false segmentView!.didSelectSegmentAtIndex = {[unowned self] (segment, index, animated) in @@ -295,9 +313,6 @@ class SJSegmentedScrollView: UIScrollView { addConstraints(verticalConstraints) segmentViewHeightConstraint = verticalConstraints[1] - } else { - - segmentViewHeight = 0.0 } } @@ -316,11 +331,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-|", @@ -344,18 +359,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, @@ -377,36 +392,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 e6a5f03..cc4e334 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. * @@ -184,6 +197,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. * @@ -242,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. @@ -281,11 +305,24 @@ import UIKit } } + open var fixWidth: Bool = true { + didSet { + segmentedScrollView.fixWidth = fixWidth + } + } + open weak var delegate:SJSegmentedViewControllerDelegate? var segmentedScrollView = SJSegmentedScrollView(frame: CGRect.zero) var segmentScrollViewTopConstraint: NSLayoutConstraint? + + open var hideSingleItem: Bool = true { + didSet { + segmentedScrollView.hideSingleItem = hideSingleItem + } + } + /** Custom initializer for SJSegmentedViewController. @@ -306,7 +343,7 @@ import UIKit } required public init?(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) + super.init(coder: aDecoder) } override open func loadView() { @@ -319,11 +356,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 + } } /** @@ -339,21 +376,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. @@ -365,6 +402,7 @@ import UIKit segmentedScrollView.segmentTitleColor = segmentTitleColor segmentedScrollView.segmentSelectedTitleColor = segmentSelectedTitleColor segmentedScrollView.segmentBackgroundColor = segmentBackgroundColor + segmentedScrollView.segmentTitleBackgroundColor = segmentTitleBackgroundColor segmentedScrollView.segmentShadow = segmentShadow segmentedScrollView.segmentTitleFont = segmentTitleFont segmentedScrollView.segmentBounces = segmentBounces @@ -450,9 +488,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 @@ -472,7 +510,7 @@ import UIKit if headerViewController == nil { headerViewController = UIViewController() - headerViewHeight = 0.0 + headerViewHeight = 0.0 } addHeaderViewController(headerViewController!)