I'm trying to create the effect of a bar underneath the selected segment control. I'm creating an UIView that is attached to the bottom and moves depending of the selected element. However there's a bug that causes the bar to go back to the first element whenever you tap it for the first time.

I've tried both using auto layout and frames

@IBOutlet weak var segmentControl: UISegmentedControl!
fileprivate var buttonBar = UIView()

buttonBar = UIView()
self.view.insertSubview(buttonBar, aboveSubview: segmentControl)
// This needs to be false since we are using auto layout constraints
buttonBar.translatesAutoresizingMaskIntoConstraints = false
buttonBar.backgroundColor = UIColor.white
buttonBar.topAnchor.constraint(equalTo: segmentControl.bottomAnchor).isActive = true
buttonBar.heightAnchor.constraint(equalToConstant: 2).isActive = true
// Constrain the button bar to the left side of the segmented control
buttonBar.leftAnchor.constraint(equalTo: segmentControl.leftAnchor).isActive = true
// Constrain the button bar to the width of the segmented control divided by the number of segments
buttonBar.widthAnchor.constraint(equalTo: segmentControl.widthAnchor, multiplier: 1 / CGFloat(segmentControl.numberOfSegments)).isActive = true

@objc func segmentedControlValueChanged(_ sender: UISegmentedControl) {
  DispatchQueue.main.async {
    UIView.animate(withDuration: 0.3) {
      let segmentSize = (self.segmentControl.frame.width / CGFloat(self.segmentControl.numberOfSegments))
      let segmentOrigin = self.segmentControl.frame.origin.x
      let index = CGFloat(self.segmentControl.selectedSegmentIndex)
      self.buttonBar.frame.origin.x = segmentOrigin + (segmentSize * index)

Related posts

Recent Viewed