如何将文件模板XIB从NS更改为UI?

时间:2019-05-27 07:50:22

标签: swift xcode

我正在尝试创建一个文件模板,该文件模板生成一个View.swift文件(UIView的子类)和一个连接到该View.swift文件的XIB。

我可以很好地将两者连接起来,但是由于某种原因,我的XIB不是...对... UIKit友好?我不确定如何准确地描述它。我希望我的XIB文件像这样加载:

Expected XIB in the interface builder

但是,它却像这样加载:

Current XIB in the interface builder

这是相关的View.swift文件,XIB文件的配置和TemplateInfo.plist

View.swift

import UIKit

class ___VARIABLE_sceneName___View: ___VARIABLE_cocoaSubclass___ {
    //MARK: XIB Outlets
    @IBOutlet var contentView: ___VARIABLE_cocoaSubclass___!

 // MARK: Object lifecycle
override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setup()
}

// MARK: Setup

private func setup() {
Bundle.main.loadNibNamed("___VARIABLE_sceneName___", owner: self, options: nil)
        addSubview(contentView)
        contentView.frame = self.bounds
        contentView.autoresizingMask = [.flexibleHeight, .flexibleHeight]    
}

XIB

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11134" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11134"/>
    </dependencies>
    <objects>
        <customObject id="-2" userLabel="File's Owner" customClass="___VARIABLE_sceneName___View" customModuleProvider="target">
            <connections>
                <outlet property="contentView" destination="Hz6-mo-xeY" id="0bl-1N-x8E"/>
            </connections>
        </customObject>
        <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
        <customObject id="-3" userLabel="Application" customClass="NSObject"/>
        <customView id="Hz6-mo-xeY">
            <rect key="frame" x="0.0" y="0.0" width="480" height="272"/>
            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
        </customView>
    </objects>
</document>

TemplateInfo.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>DefaultCompletionName</key>
    <string>MyScene</string>
    <key>Description</key>
    <string>This generates a new View for a View Controller.</string>
    <key>Kind</key>
    <string>Xcode.IDEKit.TextSubstitutionFileTemplateKind</string>
    <key>Options</key>
    <array>
        <dict>
            <key>Name</key>
            <string>Subclass Of</string>
            <key>Values</key>
            <array>
                <string>NSView</string>
            </array>
            <key>FallbackHeader</key>
            <string>#import &lt;Cocoa/Cocoa.h&gt;</string>
            <key>Default</key>
            <string>NSView</string>
            <key>Type</key>
            <string>class</string>
            <key>Identifier</key>
            <string>cocoaSubclass</string>
        </dict>
        <dict>
            <key>Description</key>
            <string>The name of the scene to create</string>
            <key>Identifier</key>
            <string>sceneName</string>
            <key>Name</key>
            <string>New Scene Name:</string>
            <key>NotPersisted</key>
            <true/>
            <key>Required</key>
            <true/>
            <key>Type</key>
            <string>text</string>
        </dict>
        <dict>
            <key>Default</key>
            <string>___VARIABLE_sceneName:identifier___</string>
            <key>Identifier</key>
            <string>productName</string>
            <key>Type</key>
            <string>static</string>
        </dict>
        <dict>
            <key>Default</key>
            <string>___VARIABLE_sceneName:identifier___View</string>
            <key>Description</key>
            <string>The view controller name</string>
            <key>Identifier</key>
            <string>viewControllerName</string>
            <key>Name</key>
            <string>View Controller Name:</string>
            <key>Required</key>
            <true/>
            <key>Type</key>
            <string>static</string>
        </dict>
        <dict>
            <key>Default</key>
            <string>___VARIABLE_sceneName:identifier___Components</string>
            <key>Description</key>
            <string>The components name</string>
            <key>Identifier</key>
            <string>componentsName</string>
            <key>Name</key>
            <string>Components Name:</string>
            <key>Required</key>
            <true/>
            <key>Type</key>
            <string>static</string>
        </dict>
        <dict>
            <key>Identifier</key>
            <string>XIB</string>
            <key>Name</key>
            <string>Also create a XIB file</string>
            <key>Description</key>
            <string>Use a Xib?</string>
            <key>Type</key>
            <string>checkbox</string>
            <key>Required</key>
            <true/>
            <key>Default</key>
            <false/>
            <key>NotPersisted</key>
            <true/>
        </dict>
        <dict>
            <key>Identifier</key>
            <string>languageChoice</string>
            <key>Required</key>
            <true/>
            <key>Name</key>
            <string>Language:</string>
            <key>Description</key>
            <string>The implementation language</string>
            <key>Type</key>
            <string>popup</string>
            <key>Default</key>
            <string>Swift</string>
            <key>Values</key>
            <array>
                <string>Swift</string>
            </array>
            <key>MainTemplateFiles</key>
            <dict>
                <key>Swift</key>
                <string>___FILEBASENAME___View.swift</string>
            </dict>
            <key>AllowedTypes</key>
            <dict>
                <key>Swift</key>
                <array>
                    <string>public.swift-source</string>
                </array>
            </dict>
        </dict>
    </array>
    <key>Platforms</key>
    <array>
        <string>com.apple.platform.iphoneos</string>
    </array>
    <key>SortOrder</key>
    <string>1</string>
    <key>Summary</key>
    <string>This generates a new View for the View Controller of the same name.</string>
</dict>
</plist>

模板容器文件夹的名称为NSViewXIBSwift

1 个答案:

答案 0 :(得分:2)

创建xib时,请区分iOS xib:

enter image description here

从macOS xib:

enter image description here