iOS 更改可访问性焦点。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7529464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS change accessibility focus.
提问by John S
is there a way to set the accessibility focus programatically (App Store-safe)? Any help will be greatly appreciated.
有没有办法以编程方式设置可访问性焦点(App Store 安全)?任何帮助将不胜感激。
回答by Vlad
To focus on element you can call.
要专注于元素,您可以调用。
Swift:
迅速:
UIAccessibility.post(notification: .screenChanged, argument: self.myFirstView)
ObjC:
对象:
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, self.myFirstView);
Otherwise for iOS 8+ use the accessibilityElements to set element order and it will focus automatically on first element in the list
否则对于 iOS 8+ 使用 accessibilityElements 来设置元素顺序,它将自动关注列表中的第一个元素
self.accessibilityElements = @[myFirstView, mySecondButton, myThirdLabel]
回答by vberihuete
This is the Swift code:
这是斯威夫特代码:
UIAccessibility.post(notification: .screenChanged, argument: <theView>)
Example usage
示例用法
let titleLabel = UILabel()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIAccessibility.post(notification: .screenChanged, argument: titleLabel)
}
回答by Daniel Hj?rtat Hj?rtstr?m
extension UIAccessibility {
static func setFocusTo(_ object: Any?) {
if UIAccessibility.isVoiceOverRunning {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
UIAccessibility.post(notification: .layoutChanged, argument: object)
}
}
}
}
Add this extension and call it by passing in the view you would like to be focused. If you would like to change focus when navigating using a tabbar, you can call this from viewWillAppear. This code wont work in any init method without a the delay of 0.7 or more.
添加此扩展并通过传入您想要关注的视图来调用它。如果您想在使用标签栏导航时更改焦点,您可以从 viewWillAppear 调用它。如果没有 0.7 或更多的延迟,此代码将无法在任何 init 方法中工作。