Skip to content

tabBar reset my segment buttonbar when hide #68

@frank61003

Description

@frank61003

Hi, I use the tab bar and hide it in my main view(first tab), and use self.currentTabBar?.setBar(hidden: false, animated: false) in main view's viewDidDissapear(). I also set some segment with button bar in other tab view. The issue is that my button bar reset to the first segment position. My button bar works fine when I change tab with tapping tab bar. I try many times and found that issue supposedly happen when tabBar hidden is changing. Is some way to correct this issue?
Demo video: https://youtu.be/V6lD-40gNpw

extension UISegmentedControl {
    func setButtonBar(buttonBar: UIView){
        buttonBar.translatesAutoresizingMaskIntoConstraints = false
        buttonBar.backgroundColor = UIColor.orange
        self.superview!.addSubview(buttonBar)
        buttonBar.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        buttonBar.heightAnchor.constraint(equalToConstant: 1).isActive = true
        buttonBar.leftAnchor.constraint(equalTo: self.leftAnchor, constant: (self.frame.width /         CGFloat(self.numberOfSegments)) * CGFloat(self.selectedSegmentIndex)).isActive = true
        buttonBar.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1 / CGFloat(self.numberOfSegments)).isActive = true
    }


    func moveButtonBar(buttonBar: UIView){
        print("movebuttonBar")
        UIView.animate(withDuration: 0.4) {
            buttonBar.frame.origin.x = (self.frame.width / CGFloat(self.numberOfSegments)) * CGFloat(self.selectedSegmentIndex) + self.frame.origin.x
        }
    }
}

 class ProductViewController: UIViewController {
 let buttonBar = UIView()
@IBOutlet weak var productSegment: UISegmentedControl!
@IBOutlet weak var productCollectionView: UICollectionView!
@IBOutlet weak var searchProductView: UIView!
@IBOutlet weak var caculateProductView: UIView!
@IBOutlet weak var companyListView: UIView!

@IBAction func changeContainerView(_ sender: UISegmentedControl) {
    self.view.endEditing(true)
    sender.moveButtonBar(buttonBar: buttonBar)
    switch sender.selectedSegmentIndex{
        case 0:
            searchProductView.isHidden = false
            caculateProductView.isHidden = true
            companyListView.isHidden = true
            break
        case 1:
            searchProductView.isHidden = true
            caculateProductView.isHidden = false
            companyListView.isHidden = true
            break
        case 2:
            searchProductView.isHidden = true
            caculateProductView.isHidden = true
            companyListView.isHidden = false
            break
        default:
            return
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.backgroundView.setBackgroundCornerRadius()
    self.productSegment.setStyle()
    self.productSegment.setButtonBar(buttonBar: buttonBar)        
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    setBackgroundImage()
    productSegment.moveButtonBar(buttonBar: buttonBar)
}
}

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions