UIViewRepresentable 保持白色
UIViewRepresentable remains white
我有这个非常基本的设置:
struct ConversationChatMessagesWrapper: UIViewRepresentable {
private let view = ConversationChatViewWithSendMessage()
func makeUIView(context _: Context) -> ConversationChatViewWithSendMessage {
view
}
func updateUIView(
_: ConversationChatViewWithSendMessage,
context _: Context
) {}
}
class ConversationChatViewWithSendMessage: UIView {
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("Not used")
}
init() {
super.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
backgroundColor = .red
let otherView = UIView()
otherView.backgroundColor = .blue
otherView.translatesAutoresizingMaskIntoConstraints = false
addSubview(otherView)
otherView.topAnchor.constraint(equalTo: topAnchor).isActive = true
otherView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
otherView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
otherView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
}
}
我有一段时间没有使用 UIKit,但我认为它应该看到蓝屏,或者至少是红屏,但我的屏幕仍然是白色的。当我删除将 otherView
添加到 ConversationChatViewWithSendMessage
的调用时,我看到蓝屏,所以在添加 otherView
时出现问题,但我看不到什么。我什至在情节提要中复制了这个设置,而且效果很好。
有什么建议吗?
这是来自 Xcode 的屏幕截图,警告指出:
Position and size are ambigious
删除super.init
后的这一行。
translatesAutoresizingMaskIntoConstraints = false
我有这个非常基本的设置:
struct ConversationChatMessagesWrapper: UIViewRepresentable {
private let view = ConversationChatViewWithSendMessage()
func makeUIView(context _: Context) -> ConversationChatViewWithSendMessage {
view
}
func updateUIView(
_: ConversationChatViewWithSendMessage,
context _: Context
) {}
}
class ConversationChatViewWithSendMessage: UIView {
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("Not used")
}
init() {
super.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
backgroundColor = .red
let otherView = UIView()
otherView.backgroundColor = .blue
otherView.translatesAutoresizingMaskIntoConstraints = false
addSubview(otherView)
otherView.topAnchor.constraint(equalTo: topAnchor).isActive = true
otherView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
otherView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
otherView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
}
}
我有一段时间没有使用 UIKit,但我认为它应该看到蓝屏,或者至少是红屏,但我的屏幕仍然是白色的。当我删除将 otherView
添加到 ConversationChatViewWithSendMessage
的调用时,我看到蓝屏,所以在添加 otherView
时出现问题,但我看不到什么。我什至在情节提要中复制了这个设置,而且效果很好。
有什么建议吗?
这是来自 Xcode 的屏幕截图,警告指出:
Position and size are ambigious
删除super.init
后的这一行。
translatesAutoresizingMaskIntoConstraints = false