AZTabBarController
AZTabBarController copied to clipboard
tabBar reset my segment buttonbar when hide
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)
}
}
`
I don’t think this has anything to do with the tab bar. What library are you using for that segment control?
I use the apple original segment control and write an extension function for moving the bottombar. I found a workaround for this issue. I setBar hidden again in viewDidAppear and the bottombar position is correct. If I put setBar() before moveButtonBar() the button bar still reset to first segment selection. I also print the bottombar positin X in the viewDidDisappear, X position changes after go into mainViewController which tabar set hidden = true in viewDidAppear(please see the demo video in first comment). I supposedly the tabbar hidden change will draw the layout and reset buttonbar position(I know this situation is weird...). I report this issue just want to learn new thing for coding. AZTabBarController is really a nice third party kit. demo video: https://youtu.be/FCrVNhnXCEE
works fine
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setBackgroundImage()
currentTabBar?.setBar(hidden: false, animated: false)
productSegment.moveButtonBar(buttonBar: buttonBar)
}
or
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
currentTabBar?.setBar(hidden: false, animated: false)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setBackgroundImage()
productSegment.moveButtonBar(buttonBar: buttonBar)
}
can't works
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setBackgroundImage()
productSegment.moveButtonBar(buttonBar: buttonBar)
currentTabBar?.setBar(hidden: false, animated: false)
}
or
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
currentTabBar?.setBar(hidden: false, animated: false)
productSegment.moveButtonBar(buttonBar: buttonBar)
}