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