Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Most questions, and answers related to this, are based on older versions of both Xcode and Swift. Additionally, 90 percent of the questions relate to UIKit and drawing custom controls.

I am adding a standard button, that is centered inside a custom control, decorated with 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 the button centered on the canvas, but the canvas is blank. Not sure many people use it this way, but it worked gloriously with Swift 3 in Xcode 8.3.

Does anyone else have this problem?

like image 530
TimTwoToes Avatar asked Oct 28 '22 22:10

TimTwoToes


1 Answers

I was able to get this to work in the latest Xcode by adding the following two lines to the top of the initialControlSetup function:

wantsLayer = true
canDrawSubviewsIntoLayer = true

I think this basically tells the NSView to render in a way that is more similar to how iOS works. If this worked in Xcode 8.3 as you say, it's possible that Apple introduced this regression in Xcode 9 without realizing it.

like image 174
Dave Weston Avatar answered Nov 08 '22 03:11

Dave Weston