Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Update] cliptobound when hide segment tab #137

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Example/SJSegmentedScrollViewDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
ar,
);
mainGroup = 607FACC71AFB9204008FA782;
productRefGroup = 607FACD11AFB9204008FA782 /* Products */;
Expand Down
134 changes: 72 additions & 62 deletions SJSegmentedScrollView/Classes/SJSegmentTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
120 changes: 65 additions & 55 deletions SJSegmentedScrollView/Classes/SJSegmentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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?
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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] {
Expand Down
Loading