简体   繁体   English

使 SwiftUI 视图嵌入在 Storyboard 视图控制器中使用全屏宽度

[英]Make SwiftUI View embedded in Storyboard ViewController use full Screen Width

Problem问题

Hello, I need help with a SwiftUI View that is presented by a View Controller that is configured in a storyboard.您好,我需要 SwiftUI 视图的帮助,该视图由在 storyboard 中配置的视图 Controller 呈现。

I present a SwiftUI View including its own model using a UIHostingController as described in this post: addSubView SwiftUI View to UIKit UIView in Swift . I present a SwiftUI View including its own model using a UIHostingController as described in this post: addSubView SwiftUI View to UIKit UIView in Swift .

Now my view is shrunken inside its Hosting View Controller's view property to its content size.现在,我的视图在其托管视图控制器的view属性中缩小到其内容大小。

What I have tried我试过的

  • Wrapping a ContainerView inside the Parent View Controller into two StackViews to force the view to use the whole screen (vertical and horizontal)在Parent View Controller 中将一个ContainerView包装成两个StackViews来强制view 使用整个屏幕(垂直和水平)
  • Configuring the initial VStack inside my SwiftUI View with .frame(minWidth: 0, maxWidth: .infinity)使用 .frame .frame(minWidth: 0, maxWidth: .infinity)在我的 SwiftUI 视图中配置初始VStack
  • Using an initial HStack with a Spacer() beneath and below all elements within that stack as suggested in Make a VStack fill the width of the screen in SwiftUI按照使 VStack 填充 SwiftUI 中的屏幕宽度中的建议,在该堆栈内的所有元素下方和下方使用带有Spacer()的初始HStack

Some Code一些代码

View Controller:查看 Controller:

class SomeParentViewController: UIViewController {

    var detailsView: DetailsOverlayView?                   //  --> SwiftUI View
    var detailsViewModel: DetailsOverlayViewModel?         //  --> Its model
    var childVC: UIHostingController<DetailsOverlayView>?  //  --> Child VC to present View
                                                           //      inside UIView
    var detailData: DetailData?
    

    override func viewDidLoad() {

        super.viewDidLoad()

        if let data = detailData {
            model = DetailsOverlayViewModel(data: data)
        }

        if let m = model {
            detailsView = DetailsOverlayView(viewModel: m)
        }

        if let v = paymentDetailsView {
            child = UIHostingController(rootView: v)
            child?.view.translatesAutoresizingMaskIntoConstraints = false
            child?.view.frame = self.view.bounds
        }
    }

    override func viewWillAppear(_ animated: Bool) {

        super.viewWillAppear(animated)

        if let cvc = childVC {
            self.addSubview(cvc.view)
            self.addChild(cvc)
        }
    }
    ...
}

The SwiftUI View: SwiftUI 查看:

...
    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            // Title
            HStack {
                Text("My great and awesome title")
            }
            VStack(alignment: .leading, spacing: 0) {
                Text("My even more awesome subtitle")
                HStack(alignment: .top) {
                    Text("on the left 1")
                    Spacer()
                    Text("on the right 1")
                }
                HStack(alignment: .top) {
                    Text("on the left 2")
                    Spacer()
                    Text("on the right 2")
                }
                Divider()
            }
        }
        .frame(minWidth: 0, maxWidth: .infinity)
        .padding(16)
    }
...

A Picture of the Result结果图片

这张图片显示,我的 View 被 Hosting View Controller 限制为它的内容大小,尽管 Hosting View Controller 应该使用它的 Parent View 的大小,它是 Parent View Controller 的大小,又等于全屏大小。

Well considering a SwiftUI view wrapped in a UIHostingController acts the same way a UIViewController does, you would define autolayout constraints the same way.考虑到包装在UIHostingController中的SwiftUI视图的行为方式与UIViewController的行为方式相同,您将以相同的方式定义自动布局约束。

Having declared translatesAutoresizingMaskIntoConstraints = false , I'm going to assume that's the plan.声明translatesAutoresizingMaskIntoConstraints = false后,我将假设这是计划。 In that case, what I'd suggest is the following:在这种情况下,我建议如下:

NSLayoutConstraint.activate([
  child.view.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
  child.view.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
  child.view.widthAnchor.constraint(equalTo: self.view.widthAnchor),
  child.view.heightAnchor.constraint(equalTo: self.view.heighAnchor)
])

The above makes the UIHostingController the same size as the parent controller and centered to it.以上使 UIHostingController 与父 controller 大小相同并以它为中心。 The height and width anchors can be constants such that you'd do .constraint(equalToConstant: 150) , or whatever other variation you'd prefer.高度和宽度锚点可以是常数,这样您就可以.constraint(equalToConstant: 150)或您喜欢的任何其他变体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM