UIViewRepresentable automatic size - Passing UIKit UIView size to SwiftUI

后端 未结 1 1462
悲&欢浪女
悲&欢浪女 2020-12-11 20:15

Assume you have a UIKit view that wants to decide about its own size (via intrinsicContentSize or layout constraints, the size might change). For example:



        
相关标签:
1条回答
  • 2020-12-11 20:46

    The solution is to set explicitly compression/hugging priority for represented UIView

    Tested with Xcode 11.4 / iOS 13.4

    struct YellowBoxView : UIViewRepresentable {
    
        func makeUIView(context: Context) -> YellowBoxUIKitView {
            let view = YellowBoxUIKitView()
    
            view.setContentHuggingPriority(.required, for: .horizontal) // << here !!
            view.setContentHuggingPriority(.required, for: .vertical)
    
            // the same for compression if needed
    
            return view
        }
    
        func updateUIView(_ uiView: YellowBoxUIKitView, context: Context) {
        }
    }
    
    0 讨论(0)
提交回复
热议问题