NSClipView weird background image in NSScrollView - objective-c

I'm trying to get a transparent background on my NSTableView, however, it seems as though the NSClipView nestled inside the NSScrollView is causing weird background problems, where it seems it's redrawing the background of the main NSView inside the NSClipView.
I've done all this in an attempt to remove it, but it's just not happening:
[[self.scrollView contentView] setCopiesOnScroll:NO];
[[self.scrollView contentView] setDrawsBackground:NO];
[self.scrollView setDrawsBackground:NO];
[self.scrollView setCopiesOnScroll:NO];
Any help would be greatly appreciated.
Regards,
Mike

Found the answer. When creating the background image in the NSView subclass, change the bounds of the drawInRect from dirtyRect to self.bounds. Works a treat.

Related

NSTextView inherits parents view background on update

I have a NSTextView that I am using to display a timer. This timer updates every second counting down to 00:00. When the timer updates the string for the NSTextView, the NSTextView acts as if it redraws but uses its parents views background for its own background.
My NSTextView has a clear background color and has been set not to draw its background. Its parent view draws a 3 part image for its own background. Below is the code for the NSTextView.
_timerLabel = [[NSTextView alloc] init];
[self.timerLabel setSelectable:NO];
[self.timerLabel setEditable:NO];
self.timerLabel.font = fontLoaded ? [NSFont fontWithName:#"DS-Digital" size:26.0f] : [NSFont fontWithName:#"Lucida Grande" size:26.0f];
self.timerLabel.textColor = [NSColor colorWithCalibratedRed:(50.0f/255.0f) green:(50.0f/255.0f) blue:(50.0f/255.0f) alpha:1.0f];
self.timerLabel.backgroundColor = [NSColor clearColor];
[self.timerLabel setDrawsBackground:NO];
[self.timerLabel setAllowsDocumentBackgroundColorChange:NO];
[self.timerLabel setAlignment:NSCenterTextAlignment];
self.timerLabel.string = #"5:11";
[self addSubview:self.timerLabel];
Any ides why this would be happening? I have tried everything I could think of or find in the apple documents and nothing has solved my issue. Any help would be greatly appreciated.
Before updating the text in the NSTextView
https://www.dropbox.com/s/za3twd8hb7r1apr/Screen%20Shot%202013-04-10%20at%205.37.10%20PM.png
After updating the text in the NSTextView
https://www.dropbox.com/s/p0bsk63o8yweyqs/Screen%20Shot%202013-04-10%20at%205.37.28%20PM.png
I figured out what was happening and thought I share in case anyone else gets stuck with the same issue.
When updating the text for the NSTextView (can be an NSTextField too), the drawRect function gets called with the rect for the NSTextView that was updated.
In my case, my draw rect was drawing a 3 part image for the background of my NSView to the supplied rect in the drawRect function. Since the rect was the rect of the NSTextView after and updated to its string, this would redraw my background image for the size of the NSTextView.
Currently the only solution I have been able to come up with is to always draw my 3 part image for the bounds of my NSView.
Hope that makes since.

Transparent NSWindow, but not subviews?

Is it possible to have a transparent NSWindow, but not for it's subviews (NSTextField and NSButton). Right now the text is also showing wat's below, I'd like that to be 0% transparent.
Here's what I do in the NSWindow init:
[self setAlphaValue:0.9];
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
You need to put a dark backgound image having the default colour of window behind each of textfield and button.
For this purpose you can subclass all required objects and add the above.

Padding around NSTableView

I've a following problem. There is a subclassed NSScrollView with a view based NSTableView in it. I've added a custom background to the scrollview in the -drawRect: method of subclass, and now I would like to add some "padding" around the inner tableview like this:
example http://img.skitch.com/20120117-ktd9g5wy8u9cm37jeebjjxx61u.png
How can I implement this?
If you're targeting Mac OS 10.10 or later, you could use
[scrollView setAutomaticallyAdjustsContentInsets:NO];
[scrollView setContentInsets:NSEdgeInsetsMake(top, right, bottom, left)];
Finally, I've solved the problem. I've created an NSView (let's call it documentContentView), added my NSTableView as a subview of this documentContentView, then I've added the documentContentView to the scrollview's documentView:
NSTableView *docView = (NSTableView *)self.scrollView.documentView;
id newClipView = [[CustomClipView alloc] initWithFrame:[self.scrollView.contentView frame]];
[self.scrollView setContentView:(NSClipView *)newClipView];
[newClipView setDrawsBackground:NO];
NSView *documentContentView = [[NSView alloc] initWithFrame:docView.bounds];
docView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[documentContentView addSubview:docView];
[self.scrollView setDocumentView:documentContentView];
[self.scrollView setDrawsBackground:NO];
I've created my custom NSClipView called CustomClipView (based on this article http://www.cocoadev.com/index.pl?CenteringInsideNSScrollView) and this subclass sets the origin of the documentContentView when the window resized. I've subclassed my tableview as well, and in -reloadData method I can resize the documentContentView when the tableview change it's contents.
The left and right padding can be done inside of the row/cell itself. For the top and bottom padding I suggest to add additional rows with no content and not selectable. This is not sexy, but worked for me.
First of all, don't add backgrounds in drawRect:. Add it in your initWithFrame: if you're subclassing, or change it from the invoker.
Adding the padding is easy: Change the frame of the NSTableView so that it is smaller, and has an origin that isn't at 0,0.

Grey border around view when using NSBorderlessWindowMask

I'm having the exact same problem as in this question:
Gray border when using NSBorderlessWindowMask
However, the accepted answer (as in the comments) of removing the window shadow doesn't seem to work, at least on Lion.
I've subclassed NSWindow, and created a borderless window in this manner:
-(id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
self = [super initWithContentRect:contentRect
styleMask:(NSBorderlessWindowMask | NSResizableWindowMask)
backing:bufferingType
defer:flag];
[self setMovableByWindowBackground:YES];
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setHasShadow:YES];
[self setLevel:NSMainMenuWindowLevel];
return self;
}
Please note that this app will only be run on Lion (so NSResizableWindowMask doesn't change the appearance). I tried disabling the shadow, and toggling numerous settings for my window but I can't seem to remove this grey border:
Nowhere in my code do I add a border. I simply have a NSSplitView added in Interface Builder in a window. During runtime I add the colored view as a subview to the left pane of the split view, completely filling the bounds of the left split view.
Edit: This happens even using a simple NSView, not even a split view.
TL;DR: Why does my NSView have a grey border around it?
ok got it. to remove the shadow simply add this to your NSWindow subclass:
- (BOOL)hasShadow {
return NO;
}
and to remove the border you need to know that this border is coming from the view - not the window (just like you said it in your edit). So you have to disable the border for the view with this code:
[myview setBorderType:NSNoBorder];

SetFrameCenterRotation cause movement issues in NSView

I asked the following question one hour ago:
Rotating NSImageView cause the NSImageView to scale
I found the solution to a rotation issue by rotating the NSView:
[self setFrameCenterRotation:-priorResult];
[self setFrameCenterRotation:rot];
[self setNeedsDisplay:YES];
This works just as I want it but when I try to move the NSView by setting the frame:
[self setFrame:CGRectMake(prect.xPos, prect.yPos, prect.w, prect.h)];
The frame won't move correctly. I read about CALayer and Core Animation, but I don't understand it, does anyone have a simple solution for my problem?