Interface Builder Basics

I have a few basic questions developing an app to run on macOS using Xcode 11 interface builder.


  1. How do you change the background color of the window, toolbar color etc. It seems like this should be easy but I don't see any option in the attributes inspector about changing background color? Do you need code to modify this? If so, an example would be helpful.
  2. How do you set the window to remember size and position after quitting the app?
  3. How do you add a custom image to a toolbar item?


Thanks

Answered by Claude31 in 424476022

You should have defined a class and set it to the view in interface builder.


In interface builder, you should see:

- a NSWindowController

- and below a NSViewController

- in the code you should have a ViewController

- in the identity inspector set the class to ViewController

- in viewDidLoad for this ViewController class, add the 2 lines for layer


class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.wantsLayer = true 
        self.view.layer?.backgroundColor = NSColor.blue.cgColor

1. You have to use layers in code:

        self.view.wantsLayer = true
        self.view.layer?.backgroundColor = NSColor.blue.cgColor


2. The way I do it is to save positions in a JSON file

func savePositionWindows(windID: Int? = nil) {
     // I save in an Array
}

To save the position and size of a specific window (I give an ID from 0 to N to each) or all

On opening I read the settings


3. To add an image in toolbar item, I did it this way


    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        //initialize menu bar icon
        statusItem = NSStatusBar.system.statusItem(withLength: CGFloat(48))
        let image = NSImage(named:"MyImage")!     // Select image in xcAssets
        image.isTemplate = true
        statusItem.button?.image = image
        statusItem.button?.appearsDisabled = false
        statusItem.button?.action = #selector(hideAllFromStatusMenu)
        statusItem.button?.target = self
    }



Note: in the forum, it is better to ask one question per thread.


Doing so, the title can be much more explicit and this helps other take profit of the thread in the future.

But for this first time, that will do.


Don't forget to close the thread if OK by marking the correct answer.

Thanks for the quick answer, sorry to be such a newbie but I am just getting started and there is not a lot of tutorials for macOS development.


So, I have a follow up question on the colors? Where does that code go? Do I have to create a new class for it?


Also, in the future in creating threads I will keep it to one question..


Thanks again.

Accepted Answer

You should have defined a class and set it to the view in interface builder.


In interface builder, you should see:

- a NSWindowController

- and below a NSViewController

- in the code you should have a ViewController

- in the identity inspector set the class to ViewController

- in viewDidLoad for this ViewController class, add the 2 lines for layer


class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.wantsLayer = true 
        self.view.layer?.backgroundColor = NSColor.blue.cgColor

I put the code in to add an image for the toolbar and I get the unresolved identifier 'statusitem"? Also, I don't see how this code identifies the toolbar item to change.


func applicationDidFinishLaunching(_ aNotification: Notification) {  
        // Insert code here to initialize your application  
        //initialize menu bar icon  
        statusItem = NSStatusBar.system.statusItem(withLength: CGFloat(48))  
        let image = NSImage(named:"MyImage")!     // Select image in xcAssets  
        image.isTemplate = true  
        statusItem.button?.image = image  
        statusItem.button?.appearsDisabled = false  
        statusItem.button?.action = #selector(hideAllFromStatusMenu)  
        statusItem.button?.target = self  
    } 
Interface Builder Basics
 
 
Q