简体   繁体   中英

How to stop UISegmentedControl segments animation

Is there any way to stop the animation of the method insertSegment(withTitle:at:animated) ? Even if I set animated:false the segment animates, look this:

错误的GIF动画

Please ignore the red ball.

The code that I am using to create the component is:

let segmentedControl = UISegmentedControl()
segmentedControl.sendActions(for: UIControlEvents.valueChanged)
segmentedControl.tintColor = UIColor.white
segmentedControl.selectedSegmentIndex = 0
view.addSubview(segmentedControl)

The constraint setup is ok, the frame width (100% - 16 pixels both sides) is the same all the time. The problem is related of the segments, not the parent view.

To add segments (this happens after a settings check):

segmentedControl.insertSegment(withTitle: "Bolinha 1", at: 0, animated: false)

Setting the frame doesn't work, since the problem is related to the inner views.

I tried to remove it using setAnimationsEnabled(false) but doesn't work.

Edit 1: Using segmentedControl.setWidth(segmentedControl.frame.width/3, forSegmentAt: 0) doesn't work.

Edit 2: Starting the view with UISegmentedControl(items: ["", "", ""]) does it replacing the whole component, this is a very heavy solution for this problem btw...

The project uses Swift 3.1.

Thank you.

I discovered that a animation has modifying the behaviour of the inner components.

Removing this animation fixed the problem:

view.setNeedsUpdateConstraints()
view.updateConstraintsIfNeeded()

UIView.animate(
  withDuration: 0.5,
  delay: 0.5,
  usingSpringWithDamping: 1.0,
  initialSpringVelocity: 2,
  animations: { [weak self] in
    self?.view.layoutIfNeeded()
  }, completion: nil)

The correct solution for this is to avoid any animation during the build of constraints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM