Cocoa Programming L54 - Custom NSButton

AppleProgramming · 14K views · 2013 · 16:47

Open on YouTube

Software (other)UX & UI designtutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it as a worked example when they need to subclass NSButton and give it a custom, layer-backed appearance.

Recommended by 1 people across 1 different months, 2013 to 2014.

What it is

A screencast from Lucas Derraugh's Cocoa Programming series showing how to build a custom NSButton on macOS. It uses Core Animation layers and a subclass of NSButton to draw a custom background, including stretchable images via contentsCenter.

What people said

Recommends beipawel · 2014-07 · Stack Overflow · on “Problems with Background: NSButton on NSView”

One way to solve this is using Core Animation Layers. I found out watching this tutorial [this video] . For that you got to your MainMenu.xib file, select the Button object and click on the layers tab in the Utilities (see screenshot (1)). Then activate Core Animation Layer for the top most View. With that all subelements also have Core Animation Layer activated (There might be performance issues with that if the project is bigger, so take care). Next I subclassed the NSButton (calling it BPButtonView in my Github project which I updated now). Select the Button in the xib-file and change the Custom Class to BPButtonView , or whatever you called it. The code for the new NSButton subclass looks lik

Cites Matt Cooper · 2013-12 · Stack Overflow · on “NSButton layer.contentscenter Not Stretching Properly”

I'm trying to create an NSButton with a custom background using the technique found here: [this video] . Everything worked fine for me except the part at the end where he explains how to use self.layer.contentscenter to make sure the edges of the image don't stretch in the wrong way. I tried to implement it in the same way, but it doesn't do anything for me, and nothing changes when I comment it out. Am I implementing something wrong or just misunderstanding what's going on here? Here's my code and images of the problem. How the button looks when square (as designed): How it looks when stretched: (notice how the top and bottom edges compress vertically, when they should only stretch horizontally)

Cites 02fentym · 2014-01 · Stack Overflow · on “Access UIButtons from the interface”

I have a weird problem; maybe you can help. I've created a class called WhitePianoKey which I use to subclass NSButton. I followed another tutorial [this video] and got everything to work. The only change I made was that I created my buttons programmatically using the tutorial you suggested. The problem I have is that I can only get the button to "press" when I click directly in the middle of it...any thoughts?