How to initialize an image button?

帅比萌擦擦* 提交于 2019-12-12 05:37:53

问题


Hoping again for someone to explain me a probably basic question. I´ve setup an imagabutton like this:

class overviewImageButton: NSButton {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        self.image = NSImage(named: "buttonImage.png")

    }
}

Now, if I start the application, the image is not shown from start. Pressing the (empty) button, the image shows up. Someone knows how to initialize the button to show the image from start?

Thanks a lot!


回答1:


The problem is that you are putting this code in the function draw for the button so it is not occurring until that time. If you want something to be true from its inception, you should put it in the constructor of the object, so the image code should be in a function with the name override init().

It should probably be something like:

override init() {
    self.image = NSImage(named: "buttonImage.png")
}


来源:https://stackoverflow.com/questions/44728599/how-to-initialize-an-image-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!