jump to navigation

NSProgressIndicator over NSImageView programmatically 4 May 2011

Posted by David Wilson in Mac.
Tags: , ,
trackback

I have been battling to get NSProgressIndicator overlaid on my NSImageView programmatically. I didn’t have a xib (nib) file for this so the challenge was all mine. The thing is that the NSImageView is resized along the way and I wanted the NSProgressIndicator to remain centered in the image… and I didn’t want to be the one responsible for shifting it around. When I managed to get the NSProgressIndicator visible eventually and almost working I found there to be a strange rectangle around the outside of the spinning progress animation… it turned out that when I resized the image this tended to shift around a bit… this became my final clue to solving the puzzle the issue was to do with my configuration of the autoresizingmask.

So… in case you ever have a similar need… I would like to share this snippet of code with you. Hopefully this will save you some considerable time.

float default_width = 30.0; 
float default_height = 30.0; 
NSRect initialFrame = NSMakeRect(0.0, 0.0, default_width, default_height); 
itemImageView = [[NSButton alloc] init]; [itemImageView setFrame:initialFrame]; 
[itemImageView setToolTip:@"Click the image for full screen, click again to return to normal size."]; 
[itemImageView setTarget:self]; 
[itemImageView setAction:@selector(imageClicked:)]; 
[itemImageView setBordered:NO]; 
[itemImageView setFrame:self.frame]; 
[itemImageView setAutoresizesSubviews:TRUE]; 
[itemImageView.cell setImageScaling:NSImageScaleAxesIndependently];

float new_width = 500.0; 
float new_height = 500.0; 
NSRect newFrame = NSMakeRect(0.0, 0.0, new_width, new_height); 
[itemImageView setFrame:newFrame]; 
[itemImageView setImage:itemImageViewOriginal.image]; 
[self addSubview:itemImageView];
refreshInProgressActivityIndicator = [[NSProgressIndicator alloc] init]; 
NSRect newProgressFrame = NSMakeRect(itemImageView.bounds.size.width / 2.0, itemImageView.bounds.size.height / 2.0, 0.0, 0.0); 
[refreshInProgressActivityIndicator setFrame:newProgressFrame]; 
[refreshInProgressActivityIndicator setStyle:NSProgressIndicatorSpinningStyle]; 
[refreshInProgressActivityIndicator setIndeterminate:YES]; 
[refreshInProgressActivityIndicator setDisplayedWhenStopped:FALSE]; 
[refreshInProgressActivityIndicator setHidden:NO]; [refreshInProgressActivityIndicator setControlSize:NSRegularControlSize]; 
[refreshInProgressActivityIndicator sizeToFit]; 
[refreshInProgressActivityIndicator setAutoresizingMask:(NSViewMaxXMargin | NSViewMinXMargin | NSViewMaxYMargin | NSViewMinYMargin)]; 
// [refreshInProgressActivityIndicator setUsesThreadedAnimation:YES]; 
[refreshInProgressActivityIndicator startAnimation:self]; 
[refreshInProgressActivityIndicator stopAnimation:self];
[itemImageView addSubview:refreshInProgressActivityIndicator];

The image above shows the NSProgressIndicator in action. You will have to click on the image to enhance your viewing experience.

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: