hussc / lightCardTabBar

Different Styles of Custom Tab Bar
36 stars 3 forks source link

Where's the third ViewController in SpecialTabBar ? #1

Open Bejil opened 3 years ago

Bejil commented 3 years ago

Hi, thanks for these great tabBars styles. I don't understand why the third viewController in SpecialTabBarController is hidden ?

I think this could be more flexible...

One pre version could be to update func set(items: [UITabBarItem]) and func select(at index: Int, animated: Bool, notifyDelegate: Bool) like this:

override func set(items: [UITabBarItem]) {

    if items.count < 2 || items.count > 5 {

        fatalError("A Special CardTabBar can't be initialized with less than 2 items and more than 5 items")
    }

    var mutableItems = items
    let specialItem = mutableItems.removeLast()
    let iconOnlyItem = mutableItems.removeLast()

    for i in 0...mutableItems.count-1 {

        let firstTextButton = TextOnlyButton(title: items[i].title)
        firstTextButton.tag = i

        buttons.append(firstTextButton)
        self.textCotainerStackView.addingArrangedSubviews {
            firstTextButton
        }
    }

    let withImageButton = IconOnlyButton(image: iconOnlyItem.image, selectedImage: iconOnlyItem.selectedImage)
    withImageButton.tag = buttons.count

    buttons.append(withImageButton)
    self.rightButtonsContainerStackView.addingArrangedSubviews {
        withImageButton
    }

    let specialButton = SpecialButton(image: specialItem.image, selectedImage: specialItem.selectedImage)
    specialButton.tag = buttons.count
    buttons.append(specialButton)
    self.rightButtonsContainerStackView.addingArrangedSubviews {
        specialButton
    }

    buttons.forEach {
        $0.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside)
    }
}
override func select(at index: Int, animated: Bool, notifyDelegate: Bool) {
    guard textCotainerStackView.arrangedSubviews.count > 0,
          rightButtonsContainerStackView.arrangedSubviews.count > 0 else {
        return
    }

    self.selectedIndex = index

    buttons.forEach {
        $0._isSelected = false
    }

    indicatorView.isHidden = buttons[index] is SpecialButton

    indicatorCenterXConstraint?.isActive = false
    indicatorCenterXConstraint = indicatorView.centerXAnchor.constraint(equalTo: buttons[index].centerXAnchor)
    indicatorCenterXConstraint.isActive = true

    buttons[index]._isSelected = true

    if animated {
        UIView.animate(withDuration: 0.3) {
            self.layoutIfNeeded()
        }
    }

    if notifyDelegate {
        delegate?.cardTabBar(self, didSelectItemAt: selectedIndex)
    }
}

In this way, select method is more dynamic and setItems method too (even if it still forces to have at least the two buttons on the right)