gpt4 book ai didi

ios - UIKit 中的可扩展 SwiftUI View 垂直居中

转载 作者:行者123 更新时间:2023-11-29 13:51:27 29 4
gpt4 key购买 nike

我在 UIKit 应用程序中集成了一些新的 SwiftUI View ,但遇到了问题。我已经研究了以下问题很长一段时间,但尚未找到原因和解决方案。该问题具体发生在将View集成到UIKit中时。我正在尝试创建一个简单的可点击 View ,在点击时垂直展开/折叠。

这就是 SwiftUI View 预览的样子(以及它应该如何表现):
Screenshot
Video

这是我在 UIKit 中实现 SwiftUI View 时得到的结果:
Screenshot
Video

似乎即使我将 UIHostingController View 的顶部操作限制为父 View ,UIHostingController View 也会垂直居中。

如评论中所述,我可以将 HostController View 的底部限制在其父 View 的底部,但这会使下面的内容无法交互。

我正在寻找的是 HostController View 约束(特别是高度)与 SwiftUI View 框架匹配的解决方案。

SwiftUI View 的代码:

import SwiftUI

struct ColorView: View {
@State var isCollapsed = true

var body: some View {
VStack {
VStack(spacing: 5) {
HStack {
Spacer()
Text("Title")
Spacer()
}
.frame(height: 100)

if !isCollapsed {
HStack {
Spacer()
Text("description")
Spacer()
}
.padding(40)
}
}
.background(Color(isCollapsed ? UIColor.red : UIColor.blue))
.onTapGesture {
withAnimation {
self.isCollapsed.toggle()
}
}

Spacer()
}
}
}

struct ColorView_Previews: PreviewProvider {
static var previews: some View {
return ColorView()
}
}

以及上述SwiftUI View的ViewController中的UIKit实现:

struct ViewControllerRepresentable: UIViewControllerRepresentable {
typealias UIViewControllerType = ViewController

func makeUIViewController(context: UIViewControllerRepresentableContext<ViewControllerRepresentable>) -> ViewControllerRepresentable.UIViewControllerType {
return ViewController()
}

func updateUIViewController(_ uiViewController: ViewControllerRepresentable.UIViewControllerType, context: UIViewControllerRepresentableContext<ViewControllerRepresentable>) { }
}

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

let colorView = ColorView()
let colorController = UIHostingController(rootView: colorView)

addChild(colorController)
view.addSubview(colorController.view)
colorController.didMove(toParent: self)

colorController.view.translatesAutoresizingMaskIntoConstraints = false
colorController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
colorController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
colorController.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
}
}

struct ViewControllerRepresentable_Previews: PreviewProvider {
static var previews: some View {
Group {
ViewControllerRepresentable()
}
}
}

非常感谢任何帮助。

最佳答案

这是在 iOS Single View Playground (Xcode 11.2) 中测试过的代码快照。原始的 ColorView 没有改变,所以下面没有重复。

UIKit UIViewController 中集成 SwiftUI View 的行为与纯 SwiftUI 环境中的行为相同。

import UIKit
import PlaygroundSupport
import SwiftUI

class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
self.view = view
}

override func viewDidLoad() {
super.viewDidLoad()

let child = UIHostingController(rootView: ColorView())
addChild(child)
self.view.addSubview(child.view)
child.didMove(toParent: self)

child.view.translatesAutoresizingMaskIntoConstraints = false
child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
child.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

关于ios - UIKit 中的可扩展 SwiftUI View 垂直居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59532721/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com