Grey border around view when using NSBorderlessWindowMask - objective-c

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];

Related

Invisible Fullscreen View with Cocoa

I'm trying to create an application which change the mouse cursor, but to do that the cursor must be inside a NSView. To do this all the time, I would need to have an intangible and invisible fullscreen view.
I've created a subclass of NSView which implement the following code to became full screen in initWithFrame:
[self enterFullScreenMode:[NSScreen mainScreen] withOptions:nil];
And using that code, I can change the cursor inside the view:
- (void)resetCursorRects{
[self addCursorRect:[self bounds] cursor:appCursor];
}
The cursor changed successfully, but here comes the first problem: the view is in fullscreen, but it's visible, like a gray background. I've tried this method:
[[self window] setOpaque:NO];
And this one:
[[self window] setBackgroundColor:[NSColor colorWithWhite:1.0 alpha:0.0]];
But both of them just change the view color to black instead of gray. How can I fix that?

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.

UIButton inside UIView doesn't respond to touch events

I've put a UIButton inside a custom UIView and the button is not receiving any touch events (it doesn't get into the highlighted state, so my problem is not about being unable to wire up a touch inside up handler). I've tried both putting it into the XIB in Interface Builder, and also tried programatically adding the UIButton into the UIView seperately, both ended with no luck. All my views are inside a UIScrollView, so I first though UIScrollView may be blocking them, so I've also added a button programatically exactly the same way I add my custom view into UIScrollView, and the button worked, elimination the possibility of UIScrollView could be the cause. My View's hiearchy is like this:
The button is over the image view, and the front layer isn't occupying my button completely, so there's no reason for me not be physically interacting with the button. At my custom view's code side, I'm creating my view as such:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIView *sub = [[[NSBundle mainBundle] loadNibNamed:#"ProfileView" owner:self options:nil] objectAtIndex:0];
[self addSubview:sub];
[sub setUserInteractionEnabled:YES];
[self setUserInteractionEnabled:YES];
CALayer *layer = sub.layer;
layer.masksToBounds = YES;
layer.borderWidth = 5.0;
layer.borderColor = [UIColor whiteColor].CGColor;
layer.cornerRadius = 30.0;
/*layer.shadowOffset = CGSizeZero;
layer.shadowRadius = 20.0;
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.shadowOpacity = 0.8;
*/
}
return self;
}
I've tried all combinations of setUserInteractionsEnabled, and had no luck. (Yes, also set them to checked in Interface Builder too). I've also read in another question with a similar problem that I should try overriding 'canBecomeFirstResponder' to return 'YES' and I've also done that too. But the problem persists, I can't click the button. I've not given any special properties, settings to the button, it's just a regular one. My other objects in the view (labels below, image view behind the button etc.) are working properly without problems. What could be possibly wrong here?
Thanks,
Can.
UPDATE: Here is a quick reproduction of the problem: https://dl.dropbox.com/u/79632924/Test.zip
Try to run and click the button.
Looking at the test project, I believe your problem in the way you create TestView, you do not specify the frame for it, so basically the parent view is 0 size, and the subviews you see from XIB extending out of the parent view and thus do not get anything in responder chain.
You should either specify the frame when creating TestView, or adjust the frame after loading XIB file.
I have had this problem as well. The cause for me was that the UIButton superview frame was of height 0, so I believe that even though a touch was happening, it was not being passed down to the button.
After making sure that the button's superview took a larger rectangle as a frame the button actions worked.
The root cause for this problem on my side was a faulty auto layout implementation (I forgot to set the height constraint for the button's superview).
I've found the solution. I was initializing my custom view as:
MyView *view = [[MyView alloc] init];
I've initialized it instead with a frame of my view's size, and it started responding to events:
CGRect rect = CGRectMake(0,0,width,height);
MyView *view = [[MyView alloc] initWithFrame:rect];
Storyboard Solution
Just for anyone wanting a solution to this when using storyboards and constraints.
Add a constraint between the superview (containing the button) and the UIButton with an equal heights constraint.
In my case, I had selected embed UIButton in a UIView with no inset on the storyboard. Adding the additional height constraint between the UIButton and the superview allowed the UIButton to respond to touches.
You can confirm the issue by starting the View Debugger and visually confirm that the superview of the UIButton is not selectable.
(Xcode 11, *- Should also work in earlier versions)

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.

Custom NSView Fill Paints Over Bottom Bar

I have a window which has a custom NSView and has a bottom bar with controls on it, one of which is an NSColorWheel.
For simplicity sake the Window is 332px high, with the custom NSView being 300px high and the bottom bar being 32px high.
The bottom bar is created as part of my awakeFromNib when the app loads the window using the following code:
[[self window] setAutorecalculatesContentBorderThickness:YES forEdge:NSMinYEdge];
[[self window] setContentBorderThickness: 32.0 forEdge: NSMinYEdge];
In my custom NSView class I fill the rectangle with color. Everything works fine when the app loads using the following in my NSView class:
- (void)drawRect:(NSRect)dirtyRect
{
dirtyRect = [self bounds];
NSColor * mNewColor = [NSColor blackColor];
[mNewColor set];
[NSBezierPath fillRect:dirtyRect];
}
However, if I subsequently call a method that changes the color of the custom NSView when a color wheel in the bottom bar is changed, the bottom bar gets overwritten with the color. The following code illustrates this method (this code is in the custom NSView class:
- (void)changeBackgroundColor:(NSNotification *)notification
{
NSLog(#"Changed background color");
NSRect mRect = [self bounds];
NSColor * mNewColor = [theColorWell color];
[mNewColor set];
[NSBezierPath fillRect:mRect];
[self setNeedsDisplay:YES];
}
Resizing the window instantly corrects the problem, but obviously I don't want the user to have to resize the window for an obvious bug!
What I don't understand is why my bounds appear to be mapping to the parent window and not the custom NSView when I call setNeedsDisplay and yet the bound correctly adjust when I resize the window using the mouse (even if just by 1 pixel).
Do I somehow need to account for the bottom bar on the redraw?
Any and all help much appreciated.
You should do all your drawing in the drawRect: method of your custom NSView. Cocoa automatically sets up the graphics context for you when it calls this method - things may not draw correctly if you perform drawing operations in other methods.
Your code in drawRect: could set the colour to the the current background colour as specified by your NSColorWell and fill the dirtyRect rectangle with this.
Then in the other method just call [self setNeedsDisplay:YES]; and then drawRect: will automatically be called to redraw the view.
See here for more information: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaViewsGuide/SubclassingNSView/SubclassingNSView.html#//apple_ref/doc/uid/TP40002978-CH7-SW4 (in particular the Drawing View Content section)