我有RPSystemBroadcastPickerView视图的视图。在文档中,apple展示了为此视图分配框架的示例,如下所示:
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我必须为这个视图设置初始框架吗?因为通常,如果使用约束创建和定位视图,则不必分配初始帧。
有人能解释一下这种行为吗?
谢谢。
发布于 2020-07-16 16:50:48
是的,你必须设置初始帧。在没有初始帧的情况下实例化时,可以检查宽度和高度。
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.0x0.0,因此它不可见且无法工作。
对于初始帧,宽度和高度打印50.0x50.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文档中,他们建议使用初始框架。
https://stackoverflow.com/questions/62571088
复制相似问题