Skip to content
This repository was archived by the owner on Dec 16, 2024. It is now read-only.
Merged
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
4 changes: 4 additions & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ final class ViewController: SwipeMenuViewController {
override func swipeMenuView(_ swipeMenuView: SwipeMenuView, titleForPageAt index: Int) -> String {
return children[index].title ?? ""
}

override func swipeMenuView(_ swipeMenuView: SwipeMenuView, hasNotificationPageAt index: Int) -> Bool {
return index % 2 == 1 ? true : false
}

override func swipeMenuView(_ swipeMenuView: SwipeMenuView, viewControllerForPageAt index: Int) -> UIViewController {
let vc = children[index]
Expand Down
10 changes: 10 additions & 0 deletions Sources/Classes/SwipeMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public struct SwipeMenuViewOptions {

/// ItemView selected textColor. Defaults to `.black`.
public var selectedTextColor: UIColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)

/// ItemView notification badge color. Defaults to `.red`.
public var notificationBadgeColor: UIColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
}

public struct AdditionView {
Expand Down Expand Up @@ -178,6 +181,9 @@ public protocol SwipeMenuViewDataSource: class {

/// Return strings to be displayed at the tab in `SwipeMenuView`.
func swipeMenuView(_ swipeMenuView: SwipeMenuView, titleForPageAt index: Int) -> String

/// Returns whether or not the tab in `SwipeMenuView` has notifications.
func swipeMenuView(_ swipeMenuView: SwipeMenuView, hasNotificationPageAt index: Int) -> Bool

/// Return a ViewController to be displayed at the page in `SwipeMenuView`.
func swipeMenuView(_ swipeMenuView: SwipeMenuView, viewControllerForPageAt index: Int) -> UIViewController
Expand Down Expand Up @@ -399,6 +405,10 @@ extension SwipeMenuView: TabViewDelegate, TabViewDataSource {
public func tabView(_ tabView: TabView, titleForItemAt index: Int) -> String? {
return dataSource?.swipeMenuView(self, titleForPageAt: index)
}

public func tabView(_ tabView: TabView, hasNotificationForItemAt index: Int) -> Bool? {
return dataSource?.swipeMenuView(self, hasNotificationPageAt: index)
}
}

// MARK: - UIScrollViewDelegate
Expand Down
6 changes: 5 additions & 1 deletion Sources/Classes/SwipeMenuViewController.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit

open class SwipeMenuViewController: UIViewController, SwipeMenuViewDelegate, SwipeMenuViewDataSource {

open var swipeMenuView: SwipeMenuView!

open override func viewDidLoad() {
Expand Down Expand Up @@ -61,6 +61,10 @@ open class SwipeMenuViewController: UIViewController, SwipeMenuViewDelegate, Swi
open func swipeMenuView(_ swipeMenuView: SwipeMenuView, titleForPageAt index: Int) -> String {
return children[index].title ?? ""
}

open func swipeMenuView(_ swipeMenuView: SwipeMenuView, hasNotificationPageAt index: Int) -> Bool {
return false
}

open func swipeMenuView(_ swipeMenuView: SwipeMenuView, viewControllerForPageAt index: Int) -> UIViewController {
let vc = children[index]
Expand Down
31 changes: 29 additions & 2 deletions Sources/Classes/TabItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import UIKit
final class TabItemView: UIView {

private(set) var titleLabel: UILabel = UILabel()
private var notificationBadgeView: UIView = UIView()
let notificationBadgeViewSize: CGSize = CGSize(width: 6, height: 6)

public var textColor: UIColor = UIColor(red: 140/255, green: 140/255, blue: 140/255, alpha: 1.0)
public var selectedTextColor: UIColor = .white
public var notificationBadgeColor: UIColor = .red

public var isSelected: Bool = false {
didSet {
Expand All @@ -16,11 +19,27 @@ final class TabItemView: UIView {
}
}
}
public var notificationBadgeViewFrame: CGRect = .zero {
didSet {
notificationBadgeView.frame = notificationBadgeViewFrame
}
}
public var hasNotification: Bool = false {
didSet {
if hasNotification {
notificationBadgeView.backgroundColor = notificationBadgeColor
notificationBadgeView.isHidden = false
} else {
notificationBadgeView.isHidden = true
}
}
}

public override init(frame: CGRect) {
super.init(frame: frame)

setupLabel()
setupNotificationBadgeView()
}

required public init?(coder aDecoder: NSCoder) {
Expand All @@ -40,9 +59,17 @@ final class TabItemView: UIView {
addSubview(titleLabel)
layoutLabel()
}


private func setupNotificationBadgeView() {
notificationBadgeView = UIView()
notificationBadgeView.isHidden = true
notificationBadgeView.layer.cornerRadius = notificationBadgeViewSize.height / 2
notificationBadgeView.clipsToBounds = true
addSubview(notificationBadgeView)
}

private func layoutLabel() {

titleLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
titleLabel.topAnchor.constraint(equalTo: self.topAnchor),
Expand Down
18 changes: 18 additions & 0 deletions Sources/Classes/TabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public protocol TabViewDataSource: class {

/// Return strings to be displayed at the tab in `TabView`.
func tabView(_ tabView: TabView, titleForItemAt index: Int) -> String?

/// Returns whether or not the tab in `TabView` has notifications.
func tabView(_ tabView: TabView, hasNotificationForItemAt index: Int) -> Bool?
}

open class TabView: UIScrollView {
Expand Down Expand Up @@ -220,6 +223,10 @@ open class TabView: UIScrollView {
tabItemView.textColor = options.itemView.textColor
tabItemView.selectedTextColor = options.itemView.selectedTextColor
}
if let hasNotification = dataSource.tabView(self, hasNotificationForItemAt: index) {
tabItemView.notificationBadgeColor = options.itemView.notificationBadgeColor
tabItemView.hasNotification = hasNotification
}

tabItemView.isSelected = index == currentIndex

Expand Down Expand Up @@ -250,6 +257,17 @@ open class TabView: UIScrollView {
adjustCellSize = CGSize(width: (frame.width - options.margin * 2) / CGFloat(itemCount), height: tabItemView.frame.size.height)
}
tabItemView.frame.size = adjustCellSize

if let title = dataSource.tabView(self, titleForItemAt: index) {
let textFrame = (title as NSString).boundingRect(with: tabItemView.titleLabel.frame.size,
options: .usesLineFragmentOrigin,
attributes: [NSAttributedString.Key.font: options.itemView.font],
context: nil)
tabItemView.notificationBadgeViewFrame = .init(x: textFrame.width + (adjustCellSize.width - textFrame.width) / 2 + 6,
y: adjustCellSize.height / 2 - tabItemView.notificationBadgeViewSize.height,
width: tabItemView.notificationBadgeViewSize.width,
height: tabItemView.notificationBadgeViewSize.height)
}

containerView.addArrangedSubview(tabItemView)
}
Expand Down