标签: uihostingcontroller

Swift UI:UIHostingController.view 不适合 iOS 13 的内容视图大小

iOS 13 iOS 14

我想根据通信结果更新 Swift UI View。但 UIHostingController.view 不适合 iOS 13 的 rootView 大小。当我尝试使用下面的示例代码时,也会发生同样的情况。我想将自调整大小的 SwiftUI View 添加到 UIStackView,但是 SwiftUI View 与上一个和下一个视图重叠,因为这个问题。我怎样才能避免这个问题?

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let object = SampleObject()
        let sampleView = SampleView(object: object)
        let hosting = UIHostingController(rootView: sampleView)
        hosting.view.backgroundColor = UIColor.green
        addChild(hosting)
        view.addSubview(hosting.view)
        hosting.view.translatesAutoresizingMaskIntoConstraints = false
        hosting.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        hosting.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        hosting.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        hosting.didMove(toParent: self)
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            object.test()
        }
    }
}


struct SampleView: View {
    @ObservedObject var object: SampleObject …
Run Code Online (Sandbox Code Playgroud)

ios swift ios13 swiftui uihostingcontroller

6
推荐指数
1
解决办法
5082
查看次数

将触摸从具有透明背景的 SwiftUI 视图转发到 UIKit 视图

我有一个大型 SwiftUI 视图,其中包含底部菜单和其上方的一些小型 UI 组件。我想将此视图覆盖在现有实例之上UIViewController,因此使用UIHostingController.

我遇到的问题是,尽管 SwiftUI 视图的背景和透明视图的背景都不同,但 SwiftUI 视图的透明区域不会转发触摸UIHostingController

由于我无法替换 所使用的视图子类UIHostingController,因此我无法扰乱其命中测试机制以使其手动转发触摸。

有办法做到这一点吗?现在看来是不可能的。

uikit ios swift swiftui uihostingcontroller

6
推荐指数
0
解决办法
275
查看次数

嵌入 UIHostingController 中的 NavigationView 具有额外的安全区域插图

有人知道如何处理这个问题吗?似乎当您有一个带有 NavigationView 的 UIHostingController 时,会发生以下情况:

在此输入图像描述

注意大的灰色标签栏安全区域。

这主要是一个 UIKit 应用程序。我们正在用 swiftUI 视图替换选项卡栏中的一些选项卡。

这是我的代码:

var body: some View {
    NavigationView {
        ZStack {
            MapKitMapView(
                mapView: mapView,
                annotations: $annotations,
                polylines: $polylines,
                centerCoordinate: $centerCoordinate,
                newMapRegion: $newMapRegion,
                userTrackingMode: $userTrackingMode
            )
            .edgesIgnoringSafeArea(.all)
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
            
            ButtonLayer(mapView: mapView, userTrackingMode: $userTrackingMode, showSheet: $showSheet)
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .padding(.margin)
        }
        .edgesIgnoringSafeArea(.all)
        .navigationBarHidden(true)
        .sheet(isPresented: $showSheet) {
            SettingsView()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请原谅审查制度。我希望该应用程序保持匿名。

但本质上我的 UITabBar 是按如下方式构建的:

  • 有一个全局的 UINavigationViewController
  • UINavigationController 中的第一个也是唯一的项目是这个 UITabBar
  • UITabBar 选项卡都是以编程方式创建的。
  • 有问题的选项卡只是一个 UIHostingController,其中包含您在上面看到的 rootView 代码。

我尝试手动为 UIHostingController …

swift swiftui uihostingcontroller uiviewrepresentable

5
推荐指数
1
解决办法
3502
查看次数

UIHostingController 中的 SwiftUI:显示 ActionSheet 会发出有关分离视图控制器的警告

我创建了一个 SwiftUI 视图,它显示在 MKMapView 的注释标注中。我使用 UIHostingController 来包装 SwiftUI 视图,并使用 annotationView 的detailCalloutAccessoryView属性来设置视图并显示它。这在大多数情况下工作正常。然后我在 SwiftUI 视图中添加了一些按钮,当点击这些按钮时需要显示警报或操作表。

这是代码:

struct ContactProfileButton: View {
    @State var showActionSheet: Bool = false
    var body: some View {
        Button(action: {
            print("Profile button tapped!")
            self.showActionSheet.toggle()
        }){
            Image("profile").resizable()
        }.frame(width: 26.0, height: 26.0)
        .actionSheet(isPresented: $showActionSheet, content: {
            print("Profile button - show action sheet")
            return ActionSheet(title: Text("Profile title"),
                               message: Text("Profile message"),
                               buttons: [
                                .default(Text("Do something")),
                                .destructive(Text("Cancel"))
                               ])
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

当我点击按钮时,它工作正常并显示一个操作表,但在控制台中生成此警告:

[演示]不鼓励从分离的视图控制器 < TtGC7SwiftUI19UIHostingControllerVS_7AnyView : 0x7fac1fac8280>呈现视图控制器 <SwiftUI.PlatformAlertController: 0x7fac118b3a00> 。

解决此警告的最佳方法是什么?如果在 UIHostingController …

uiview uiactionsheet swiftui uihostingcontroller

5
推荐指数
0
解决办法
124
查看次数

iOS 16 NavigationBar 设置大标题并隐藏内联标题

在检查 iOS 16 上的应用程序错误时,我们发现导航栏上的行为发生了变化,即使在非 14 Pro 型号上也是如此。有多个问题,我们想知道如何解决它们。所有问题都没有更改代码。只需在 iOS 16 上运行该应用程序即可。

  1. 在某些视图上,iOS 将标题样式更改为“大”。它曾经是并且应该是内联的。
  2. 在某些视图上,标题会在推送动画期间内联显示,但一旦动画完成,标题样式就会更改为大。这会导致 UI 跳转。在其他视图中,标题完全消失。
  3. 在某些视图中,标题是正确内联的,但是当显示工作表并关闭时,它会导致标题从导航栏中完全消失。

我们使用 UIHostingControllers 来包装所有 SwiftUI 视图。也许这里有一些问题。

swift swiftui uihostingcontroller ios16

5
推荐指数
1
解决办法
1577
查看次数

混合 UIKit 和 SwiftUI 项目中导航栏按钮出现延迟

我有一个应用程序,其主要框架是使用uikit开发的,其一些页面是由SwiftUI编写的,并嵌入到uikit中通过UIHostingController显示。现在有一个Swiftui页面,我想通过UIHostingController推送到这个页面。

窗口初始化代码如下:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        
        let vc = UINavigationController(rootViewController: ViewController())
        
        window?.rootViewController = vc
        window?.makeKeyAndVisible()
        return true
    }
Run Code Online (Sandbox Code Playgroud)

然后是 SwiftUI 视图:

struct ContentView: View {
    var body: some View {
        Text("hello world")
    }
}
Run Code Online (Sandbox Code Playgroud)

然后是UIKit vc:

import UIKit
import SwiftUI

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(runBtn)
        runBtn.frame = CGRect(x: 100, y: 200, width: 200, height: 50)
    }
    
    lazy var runBtn: UIButton  = …
Run Code Online (Sandbox Code Playgroud)

uikit swiftui uihostingcontroller

5
推荐指数
1
解决办法
419
查看次数

将可变高度的 UIHostingController 插入 UIStackView

我有一个问题,我试图将 SwiftUI 视图插入到现有的 UIKit 视图中。SwiftUI 视图可以动态更改高度,但 UIStackView 不会调整到新的大小。我创建了一个(可怕的)测试项目来强调这一点。

按钮:

class TestButton: UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupButton()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupButton()
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupButton() {
        setTitleColor(.white, for: .normal)
        backgroundColor = .red
        titleLabel?.font = .boldSystemFont(ofSize: 25)
        layer.cornerRadius = 10
    }
}
Run Code Online (Sandbox Code Playgroud)

SwiftUI 视图:

struct TestSwiftUIView: View {
    @State var text: [String] = ["This is a line of text"]
    var body: some View {
        VStack {
            ForEach(text, id: \.self) { …
Run Code Online (Sandbox Code Playgroud)

uikit swift uistackview swiftui uihostingcontroller

4
推荐指数
1
解决办法
1458
查看次数

UITabBar containing SwiftUI View

I have the below series of controllers and views. However, when I use the navigation link on the MoreView it changes the tabBarItem.title value. For instance it will say more, but when the privacy policy button is clicked and the user is navigated to policy view, the title in the tab bar changes to whatever is in .navigationBarTitle() or if non is provided, an empty string! How do I avoid this? I want the tab bar title to be static …

uitabbarcontroller swift swiftui uihostingcontroller

3
推荐指数
1
解决办法
535
查看次数

如何将 UIHostingController 的背景颜色设置为清除?

我需要能够查看我的 SwiftUI 视图以及呈现它们的 UIHosting 控制器,以便看到它们下面的背景图像。下面的代码仍然在我的 SwiftUI 视图下显示一个白色矩形。Paul Hudson要使用 edgeIgnoringSafeArea 但我不能使用它,因为我的 SwiftUIView 嵌入在更大的 UI 方案中。有什么办法可以使我看到的这个白色矩形清晰?

    var body: some View {
          ZStack {
            VStack {
                 //omitted
              }
        }.background(Color.clear)
   }
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui uihostingcontroller zstack

2
推荐指数
1
解决办法
908
查看次数