【问题标题】:RPSystemBroadcastPickerView subviews odd behaviour when initial size not set未设置初始大小时,RPSystemBroadcastPickerView 子视图的奇怪行为
【发布时间】:2020-06-25 08:29:10
【问题描述】:

我有 RPSystemBroadcastPickerView 视图的视图。在文档中,苹果显示了将框架分配给此视图的示例,如下所示:

https://developer.apple.com/documentation/replaykit/rpsystembroadcastpickerview?language=objc

当设置框架+约束时,按预期工作:

picker = RPSystemBroadcastPickerView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))

但如果我像这样初始化RPSystemBroadcastPickerView

picker = RPSystemBroadcastPickerView()

子视图无法正确显示。

两种情况的约束:

picker.translatesAutoresizingMaskIntoConstraints = false

pickerViewContainerView.addSubview(picker)
picker.widthAnchor.constraint(equalTo: pickerViewContainerView.widthAnchor).isActive = true
picker.heightAnchor.constraint(equalTo: pickerViewContainerView.heightAnchor).isActive = true
picker.leadingAnchor.constraint(equalTo: pickerViewContainerView.leadingAnchor).isActive = true
picker.topAnchor.constraint(equalTo: pickerViewContainerView.topAnchor).isActive = true

我必须为此视图设置初始框架吗?因为通常如果您使用约束创建和定位视图,则不必分配初始帧。

有人可以解释一下这种行为吗?

谢谢。

【问题讨论】:

    标签: swift uikit replaykit


    【解决方案1】:

    是的,您必须设置初始帧。在没有初始帧的情况下实例化时可以检查宽度和高度。

    picker = RPSystemBroadcastPickerView()
    print("Height : \(picker.frame.height)")   // this will print as 0.0
    print("Width : \(picker.frame.width)")     // this will print as 0.0
    

    由于选择器视图的高度和宽度为 0.0 x 0.0,因此它不可见且无法正常工作

    初始帧的宽度和高度打印为 50.0 x 50.0 并且可见。

    picker = RPSystemBroadcastPickerView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    print("Height : \(picker.frame.height)")   // this will print as 50.0
    print("Width : \(picker.frame.width)")     // this will print as 50.0
    

    这就是为什么他们在 Apple Developer Documentation 中建议使用初始框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多