Xcode:Interface Builder使用错误的目标渲染IBDesignable

3

环境

  • macOS: 10.15.3
  • Xcode: 11.3.1
    • Swift: 5.1
    • 应用程序目标:macOS

背景

我有一个基于NSView(或UIView,取决于所需的目标)的IBDesignable控件。我已经使用软件包管理器对其进行打包,如下所示:

// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MyControl",
    platforms: [
        .macOS(.v10_13),
        .iOS(.v10)
    ],
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "MyControl",
            targets: ["MyControl"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "MyControl",
            dependencies: []),
        .testTarget(
            name: "MyControl Tests",
            dependencies: ["MyControl"]),
    ]
)

作为“MyControl”的一部分,我有以下基于所需目标(Target)的类型别名。
#if os(macOS)
    import AppKit
    public typealias QJViewController = NSViewController
    public typealias QJColor = NSColor
    public typealias QJFont = NSFont
    public typealias QJView = NSView
#elseif os(iOS) || os(tvOS)
    import UIKit
    public typealias QJViewController = UIViewController
    public typealias QJColor = UIColor
    public typealias QJFont = UIFont
    public typealias QJView = UIView
#endif

包编译成功;控件正确导入到我的目标项目中;在目标项目中一切都能正常编译和运行。 问题 当我在InterfaceBuilder中设置控件时,它无法呈现,出现以下错误:

enter image description here

重申一下:我的目标不是appleTV,而是macOS。无论我做什么,IB都想将这个包和我的控件渲染成AppleTV作为目的地。鉴于我在上面使用的类型别名,控件无法正确呈现,因为(显然)macOS不知道UIColor、UIFont、UIView、UIViewController的任何内容。
问题:
有没有办法强制IB在渲染控件时使用特定的目的地?如果没有,我的项目设置中是否缺少某些东西?像我说的,控件按预期工作;我只是希望它在IB中呈现。

我遇到了同样的问题... 我的包只能在iOS下运行(在清单中指定),而IBDesignable在普通项目中可以工作,但在Swift Package中,IB尝试为tvOS渲染... 你找到解决方案了吗? - DungeonDev
我也遇到了同样的问题... 我在我的 Package.swift 文件中定义了 iOS 作为唯一可用的平台,但 IB 仍然试图在 tvOS 上呈现它。 真是疯狂。 - Ayoze Roberto Bernardo
1个回答

1
由于提供的材料不足以测试问题,建议尝试以下条件映射。
#if canImport(AppKit)
    import AppKit
    public typealias QJViewController = NSViewController
    public typealias QJColor = NSColor
    public typealias QJFont = NSFont
    public typealias QJView = NSView
#elseif canImport(UIKit)
    import UIKit
    public typealias QJViewController = UIViewController
    public typealias QJColor = UIColor
    public typealias QJFont = UIFont
    public typealias QJView = UIView
#endif

谢谢,Asperi!这个建议“修复”了IB中的故障。然而,我的问题仍然存在。我的目标应用程序和目的地是macOS;然而,IB尝试使用“AppleTV 4K”作为其目的地来渲染控件。遗憾的是,我仍然得到以下提示:“Designables Build Failed”。 - Mike Manzo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接