Skip to content

Commit

Permalink
Merge pull request #3 from jianghongbing/master
Browse files Browse the repository at this point in the history
use UILayoutGuide replace spacers(UIView)
  • Loading branch information
askopin authored Feb 15, 2019
2 parents dbbad2d + 6b9ff94 commit 2dc6a13
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions BubbleTabBar/Classes/BubbleTabBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit

open class BubbleTabBar: UITabBar {

private var buttons: [CBTabBarButton] = []
public var animationDuration: Double = 0.3

Expand All @@ -19,7 +19,7 @@ open class BubbleTabBar: UITabBar {
guard shouldSelectOnTabBar else {
shouldSelectOnTabBar = true
return

}
guard let newValue = newValue else {
buttons.forEach { $0.setSelected(false) }
Expand All @@ -29,12 +29,12 @@ open class BubbleTabBar: UITabBar {
index != NSNotFound else {
return
}

select(itemAt: index, animated: false)

}
}

open override var tintColor: UIColor! {
didSet {
buttons.forEach { button in
Expand All @@ -44,12 +44,12 @@ open class BubbleTabBar: UITabBar {
}
}
}

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

public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
Expand Down Expand Up @@ -89,24 +89,24 @@ open class BubbleTabBar: UITabBar {
csContainerBottom.constant = -safeAreaInsets.bottom
} else { }
}

open override var items: [UITabBarItem]? {
didSet {
reloadViews()
}
}

open override func setItems(_ items: [UITabBarItem]?, animated: Bool) {
super.setItems(items, animated: animated)
reloadViews()
}

private var spacers: [UIView] = []
private var spaceLayoutGuides:[UILayoutGuide] = []

private func reloadViews() {
subviews.filter { String(describing: type(of: $0)) == "UITabBarButton" }.forEach { $0.removeFromSuperview() }
buttons.forEach { $0.removeFromSuperview()}
spacers.forEach { $0.removeFromSuperview()}
buttons.forEach { $0.removeFromSuperview() }
spaceLayoutGuides.forEach { self.container.removeLayoutGuide($0) }
buttons = items?.map { self.button(forItem: $0) } ?? []
buttons.forEach { (button) in
self.container.addSubview(button)
Expand All @@ -116,22 +116,18 @@ open class BubbleTabBar: UITabBar {
buttons.first?.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10.0).isActive = true
buttons.last?.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10.0).isActive = true
let viewCount = buttons.count - 1
spacers = []
spaceLayoutGuides = [];
for i in 0..<viewCount {
let spacer = UIView()
spacer.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(spacer)
spacer.isHidden = true
spacer.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
spacer.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
let layoutGuide = UILayoutGuide()
container.addLayoutGuide(layoutGuide)
spaceLayoutGuides.append(layoutGuide)
let prevBtn = buttons[i]
let nextBtn = buttons[i + 1]
spacer.leadingAnchor.constraint(equalTo: prevBtn.trailingAnchor).isActive = true
spacer.trailingAnchor.constraint(equalTo: nextBtn.leadingAnchor).isActive = true
spacers.append(spacer)
layoutGuide.leadingAnchor.constraint(equalTo: prevBtn.trailingAnchor).isActive = true
layoutGuide.trailingAnchor.constraint(equalTo: nextBtn.leadingAnchor).isActive = true
}
for spacer in spacers[1...] {
spacer.widthAnchor.constraint(equalTo: spacers[0].widthAnchor, multiplier: 1.0).isActive = true
for layoutGuide in spaceLayoutGuides[1...] {
layoutGuide.widthAnchor.constraint(equalTo: spaceLayoutGuides[0].widthAnchor, multiplier: 1.0).isActive = true;
}
layoutIfNeeded()
}
Expand All @@ -149,12 +145,12 @@ open class BubbleTabBar: UITabBar {
}
return button
}

@objc private func btnPressed(sender: CBTabBarButton) {
guard let index = buttons.index(of: sender),
index != NSNotFound,
let item = items?[index] else {
return
index != NSNotFound,
let item = items?[index] else {
return
}
buttons.forEach { (button) in
guard button != sender else {
Expand All @@ -168,7 +164,7 @@ open class BubbleTabBar: UITabBar {
}
delegate?.tabBar?(self, didSelect: item)
}

func select(itemAt index: Int, animated: Bool = false) {
guard index < buttons.count else {
return
Expand All @@ -193,5 +189,5 @@ open class BubbleTabBar: UITabBar {
self.container.layoutIfNeeded()
}
}

}

0 comments on commit 2dc6a13

Please sign in to comment.