Is IBDesignable broken for AppKit in Xcode 9.2 and Swift 4?

Most of the questions and answers related to this are based on older versions of both Xcode and Swift. In addition, 90 percent of the questions are about UIKit and creating custom controls.

I add a standard button located inside the custom control, decorated IBDesignable.

import Cocoa

@IBDesignable public class ButtonPresetView: NSView {
    public override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialControlSetup()
    }

    public required init?(coder decoder: NSCoder) {
        super.init(coder: decoder)
        initialControlSetup()
    }

    private func initialControlSetup() {
        let button = NSButton(title: "Hello", target: nil, action: nil)
        button.translatesAutoresizingMaskIntoConstraints = false
        addSubview(button)

        // Configure button
        centerXAnchor.constraint(equalTo: button.centerXAnchor).isActive = true
        centerYAnchor.constraint(equalTo: button.centerYAnchor).isActive = true
    }
}

I add a custom view to the application and set the class property in the Identity Inspector to my custom class ( ButtonPresetView).

It should show a button centered on the canvas, but the canvas is empty. Not sure if many use it that way, but it worked great with Swift 3 in Xcode 8.3.

Does anyone else have this problem?

+4
2

Xcode, initialControlSetup:

wantsLayer = true
canDrawSubviewsIntoLayer = true

, NSView , iOS. Xcode 8.3, , , Apple Xcode 9, .

0

, .

canDrawSubviewsIntoLayer true, , wantsLayer , , canDrawSubviewsIntoLayer, true.

, , . , canDrawSubviewsIntoLayer = true prepareForInterfaceBuilder().

, Interface Builder , button.wantsLayer = true, canDrawSubviewsIntoLayer . , , , Interface Builder / .

0

Source: https://habr.com/ru/post/1690286/


All Articles