I am very new to OSX development.
How do I tint an application to dark gray like this one did?
is it possible to do that from interface builder? If not, how do I do that from code?
To change window title bar color, set the window as textured in IB. Then in corresponding view controller/app delegate update the window color.
NSColor *grayColor = [NSColor colorWithCalibratedRed:64/255.0f
green:64/255.0f
blue:64/255.0f
alpha:1.0];
[self.window setBackgroundColor:grayColor];
Now the window would like this :
With Toolbar:
Note that when window is set as textured entire window will be textured. I have subclassed the view and set background to white color :
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
}
Else the window would like this :
Related
How can I change background color of scroller at NSTableView? For example I've made black background color of my TableView and got white scroller, I want ClearColor or Black color of Scroller's background. How can I make it?
You'll need to subclass NSScroller and override its -(void)drawRect: method
- (void)drawRect:(NSRect)dirtyRect
{
// the only line is required
[self drawKnob];
}
I have NSCollectionView and would like to set its background color, however using some of codes suggested here on stack I came to a problem - none of them fill the window entirely. You might be able to see a white (around 1px) white border around nscollection view except of the corners (collection view constraints are set to 0).
Ideally I would like to get rid of the white border.
Code for the first image:
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor(context, 0.227,0.251,0.337,1);
CGContextFillRect(context, NSRectToCGRect(dirtyRect));
}
Code for the second image:
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
[[NSColor blueColor] setFill];
NSRectFill(dirtyRect);
}
Do you have Autolayout turned on? Any project warning? Go to MainMenu.xib and look in the right info panel. If Yes, then do something with the constraints.
I want to set a background color to a view and as of now I have subclassed that view in Xib and in drawRect method I am setting the color.
- (void)drawRect:(NSRect)dirtyRect {
[[NSColor blackColor] setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
Am using [self.view enterFullScreenMode:screen withOptions:nil]; to show the view(which contains an image) in fullscreen. But when view is in fullscreen it shows a default gray background instead of black color. How can I set the background to black ?
Are you sure your methods calling? Put a NSLog to make sure.
If it is, then delete the line [super drawRect:dirtyRect];
I want to draw on the screen with a transparent background, so that everything (e.g. open applications are still visible).
In windowDidLoad of my custom NSWindowController i have the follwing:
[self.window setOpaque: NO];
[self.window setHasShadow:NO];
[self.window setBackgroundColor:[NSColor clearColor]];
[self.window setStyleMask:NSBorderlessWindowMask];
My custom NSWindow overrides canBecomeKeyWindow
- (BOOL)canBecomeKeyWindow {
return YES;
}
My View overrides drawRect
- (void)drawRect:(NSRect)rect {
[[NSColor clearColor]set];
NSRectFill(rect);
...
}
Problem: Trying to draw by using mouse events inside of my custom view results in the view/application beneath my Window to receive these events.
It justs works when i do not set the NSWindow styleMask to NSBorderlessWindowMask or by setting the background color of the custom view to semitransparent.
[[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.05] set]
How can i draw on screen with full transparency and NSBorderlessWindowMask?
Adding
[self.window setIgnoresMouseEvents:NO];
to windowDidLoad of my custom NSWindowController solved it
Im working on a drag n' drop view and found some handlers for drag and drop actions on the web. I want to make it so it turns blue when the user drags a file over the drag and drop area and gray again when they exit the drag and drop area. The issues is its not updating when you drag your mouse over it or exit it. Heres some of the code:
- (void)drawRect:(NSRect)rect
{
NSRect bounds = [self bounds];
[[NSColor grayColor] set];
[NSBezierPath fillRect:bounds];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
NSRect bounds = [self bounds];
[[NSColor blueColor] set];
[NSBezierPath fillRect:bounds];
return NSDragOperationCopy;
}
- (void)draggingExited:(id <NSDraggingInfo>)sender {
NSRect bounds = [self bounds];
[[NSColor grayColor] set];
[NSBezierPath fillRect:bounds];
}
Thanks for any help.
Are you calling [yourView: setNeedsDisplay] anywhere?
This is how you let the drawing framework know it needs to message your UIView subclass with drawRect:, so you should do it whenever things have changed. In your case, this probably means when the mouse enters or exits the drop area.
Drawing only works when a context (like a canvas for painting) is set up for you to draw into. When the framework calls -drawRect: it has set up a drawing context for you, so drawing commands like -[NSColor set] and -[NSBezierPath fillRect:] work as you expect.
Outside of -drawRect: there is usually no drawing context set up. Using drawing commands outside of -drawRect: is like waving a paintbrush in the air; there's no canvas, so no painting happens.
In 99.99% of cases, all view drawing should be kept within -drawRect: because NSView does a lot of work that you don't want to do to get the drawing context set up correctly and efficiently.
So, how do you change your view's drawing within your -draggingEntered: and -draggingExited: methods? By side effects.
You're doing the same thing in all three cases: 1) Setting a color and 2) Drawing a rectangle. The only difference is the color changes in each method. So, why not control which color you use in -drawRect: with an ivar, like so:
- (void)draggingEntered:(id <NSDraggingInfo>)sender {
drawBlueColorIvar = YES;
// ...
}
Then in -drawRect: you do this:
- (void)drawRect:(NSRect)rect {
NSColor *color = drawBlueColorIvar ? [NSColor blueColor] : [NSColor grayColor];
[color set];
[NSBezierPath fillRect:rect];
}
(Notice I didn't use [self bounds]. It is more efficient to just draw into the "dirty" rect, when possible.)
Finally, you need some way to tell the framework that your view needs to redraw when drawBlueColorIvar changes. The framework won't draw anything unless it's told it needs to. As Chris Cooper said, you do this with [self setNeedsDisplay:YES]. This should go after any place you change drawBlueColorIvar.