UISwitch setOn(:, animated:) does not work as document

岁酱吖の 提交于 2019-12-03 07:12:23
DispatchQueue.main.async {
    sender.setOn(flag, animated: animated)
}

it works for me in Xcode 8.

but call UISwitch.setOn(_:animated:) directly on main thread doesn't work.

Update

thanks to @codiction:

UISwitch.setOn(_:animated:) can be called direclty on main thread, but can't be called directly in UISwitch ValueChanged action on iOS 10.

The following solution resolves that issue. You should dispatch_async only if trying to call [UISwitch setOn:] within the action callback of that switch itself.

dispatch_async(dispatch_get_main_queue(), ^{

    [switch setOn:YES animated:YES];
});

I found these solutions to partially work. However, when I used switch.setOn(false, animated: true) the onTint color would appear white in the off position. I'm assuming this is a bug but I worked around this by using this:

switch.setOn(false, animated: true)
switch.onTintColor = .clear

Don't forget to reset the onTint color when you're ready to allow the switch to be toggled on, if applicable.

switch.onTintColor = <your custom color> // Use nil to use the default onTint for UISwitch

Hopefully this helps save someone a little time.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!