NSCollectionView setBackgroundColors?

Discussion in 'Mac Programming' started by Darkroom, Oct 15, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i'm attempting to programatically change the background color of my NSCollectionView.

    first i subcleassed it = CollectionBackground : NSCollectionView, and added the IBAction methods to the header:

    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface CollectionBackground : NSCollectionView
    	{
    
    	}
    
    - (IBAction)backgroundColorBlack:(id)sender;
    - (IBAction)backgroundColorGray:(id)sender;
    - (IBAction)backgroundColorWhite:(id)sender;
    
    @end
    
    then i added the actions to the implementation file:

    Code:
    #import "CollectionBackground.h"
    
    @implementation CollectionBackground
    
    - (IBAction)backgroundColorBlack:(id)sender
    	{
    	NSLog (@"Background Tunred Black");
    	[super setBackgroundColors:[NSArray arrayWithObjects:[NSColor blackColor], nil]];
    	}
    	
    - (IBAction)backgroundColorGray:(id)sender
    	{
    	NSLog (@"Background Tunred Gray");
    	[super setBackgroundColors:[NSArray arrayWithObjects:[NSColor grayColor], nil]];
    	}
    	
    - (IBAction)backgroundColorWhite:(id)sender
    	{
    	NSLog (@"Background Tunred White");
    	[super setBackgroundColors:[NSArray arrayWithObjects:[NSColor whiteColor], nil]];
    	}
    
    @end
    
    except for the NSLogs, nothing is happening... is this the proper way to do this?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Just guessing here since I've never used NSCollectionView, but do you actually have views in the collection? I'm thinking the background colors kind of work like the alternative color on the table view - you actually need to provide the data with view for the colors to show.
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    yeah there's a collectionViewItem object and a view that represents each of the views for the collection...

    so do i just add these actions to the collectionViewItem object?

    i really don't get this...
     
  4. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #4
    Could your views be completely opaque and filling the entire collection view so that background doesn't show? If not, what does the background look like?
     
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    ive attached a small working example... i think what i'm doing is the wrong way of doing this, clearly, since it doesn't work... but maybe i'm just forgetting something?

    in IB, you'll notice that the NSCollectionView is nested inside an NSScrollView (it's like that by default)... the background of the collectionView is Blue on launch, so it's not opaque...
     

    Attached Files:

  6. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #6
    Take a closer look at your nib. It's set up totally wrong.

    Your NSCollectionView subclass "CollectionBackground" is a top-level object in the nib, not in the view like it should be. Instead of dragging out a new collection view, you should have opened the inspector, selected the existing collection view, and changed its class to CollectionBackground.

    The button actions are tied to the CollectionBackground instance, so it's really doing the work when you click the buttons, but since it's not in a view, you're not seeing anything. Connect the buttons to the collection view in the window after you changed its class. This makes it work for me.

    Is this your first time using IB, or was this just a mental lapse? You may need some brushing up...
     
  7. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #7
    ha... a mental lapse for sure... sorry, i knew i was doing something stupid :eek:

    thanks kupa
     

Share This Page