UISwitch setOn(:,animated :)不能用作文档

时间:2016-09-19 06:07:41

标签: ios10 uiswitch

正如Apple的文档所写,UISwitch的函数setOn(on: Bool, animated: Bool)不会发送动作。它在iOS 10之前工作正常,但它会在我在iOS 10中调用它之后发送动作。我在“ValueChanged”事件中调用它来强制切换回来,所以我把这个事件动作两次。这是iOS 10中的一个错误吗?

3 个答案:

答案 0 :(得分:9)

DispatchQueue.main.async {
    sender.setOn(flag, animated: animated)
}

它适用于Xcode 8。

但直接在主线程上调用UISwitch.setOn(_:animated:)不起作用。

更新

感谢@codiction:

UISwitch.setOn(_:animated:)可以在主线程上调用direclty,但不能在iOS 10上的UISwitch ValueChanged操作中直接调用。

答案 1 :(得分:8)

以下解决方案可解决该问题。只有在尝试在该交换机本身的动作回调中调用[UISwitch setOn:]时,才应调度dispas_async。

dispatch_async(dispatch_get_main_queue(), ^{

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

答案 2 :(得分:1)

我发现这些解决方案部分起作用。但是,当我使用switch.setOn(false, animated: true)时,onTint颜色在关闭位置会显示为白色。我假设这是一个错误,但是我通过使用以下方法解决了这个问题:

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

在准备好允许打开开关时,请不要忘记重置onTint颜色。

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

希望这可以帮助您节省一些时间。