ios - 对于 alertController:UISegmentedControl 中选定段的 sourceView 是什么
问题描述
我有一个UISegmentedControl
4 段。这些段中的一些UIAlertController
在用户选择时实例化。
在 上UIAlertController
,您可以设置popoverPresentationController?.sourceView
让警报控制器指向原点视图,这将用于 iPad 等较大的设备。
我尝试通过UISegmentedControl
,它有效,但锚始终是分段控件的左上角 - 而不是被选中的段。
我想使用的实际段UISegmentedControl
作为 sourceView,但没有UISegmentedControl
包含这些段的数组。
有一个包含视图数组的子视图属性。该数字对应于段数,所以我虽然找到了解决方案。但如果我分配:
alertController.popoverPresentationController?.sourceView = segmentedControl.subviews[2]
...对于第三段,它有时有效,有时无效。看起来这个数组并不总是按屏幕上的段顺序排列。
如何找到要使用的正确子视图?
解决方案
我找到了一个解决方案:
alertController.popoverPresentationController?.sourceView = (segmentedControl.subviews.sorted { $0.frame.origin.x < $1.frame.origin.x })[segmentedControl.selectedSegmentIndex]
这将首先按照 x 坐标的顺序对视图进行排序。然后我可以使用segmentedControl.selectedSegmentIndex
作为索引。
推荐阅读
- blazor - 适用于 3.2.0-preview2.20160.5 的 Blazor Wasm 组件库
- java - 尝试搜索数组列表时类型不匹配
- python - 通过比较 Python 中其他数组的数据来创建新数组
- java - JPA 中的 Bean 验证 API
- javascript - 在特定索引处添加数组项 - React Redux
- python - 预期 LP_LP_c_longlong 实例而不是指向 LP_c_longlong 的指针
- vue.js - 如何动态更新bootstrap-vue.js js表数据
- apache-beam-io - 使用 apache 2.19 的 bigquery 数据流上的 IllegalAccessError
- outlook-web-addins - Outlook web-addin 使用已注册的方案打开外部应用程序
- firebase - 为什么调度引发错误时我没有收到警报?