how to change window color on mac os - macos

mac os: 10.10
xcode:6.1
I got a "default cocoa program" by Xcode. and I want to change the window color...after researching, I did the following code in AppDelegate.m:
NSWindow *myWindow;
NSColor *semiTransparentBlue =
[NSColor colorWithDeviceRed:0.0 green:0.0 blue:1.0 alpha:0.5];
[myWindow setBackgroundColor:semiTransparentBlue];
but window color not changed...
1: how to change the window color on mac os?
2: no NSWindow in "Default cocoa program" by Xcode, why will get a window?
thank your very much.
i tried some functions...but failed..following is the detail:
(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
//compile success but the window's color is not changed.
[[NSApplication sharedApplication] keyWindow].backgroundColor = [NSColor redColor];
//or the following two got compiled error
self.view.backgroundColor = [UIColor redColor];
self.window.backgroundColor = [UIColor redColor];
}

in your app delegate juste type this :
self.window.backgroundColor = [NSColor whiteColor];

Related

Xcode 10 - NSVisualEffectView added to NSWindow when displayed modally (Mac OS)

I have a NSWindow that I display modally on some other window to display alerts.
The code is:
// This code is in MyAlert .m file. MyAlert inherits from NSWindow
- (void)showInWindow:(NSWindow *) {
[window beginSheet:self completionHandler:NULL];
}
Thing is that when compiled with Xcode 10.1 in a Mac running Mojave I see a gray "blurring" view behind the alert which I don't want there: I want the background window where it's shown to be visible.
Same code compiled with Xcode 9.4.1 does not show this blurring view.
Furthermore, I debugged the UI and there's indeed a NSVisualEffectView inserted in the Xcode 10.1 compiled version which is not there when compiling on 9.4.1, but I can't seem to find a way to get rid of it.
Below are screenshots of the UI debugged in both versions.
Has someone faced and figured this one out?
Update (Minimal Project reproducing issue of inserted nsvisualeffectview): http://s000.tinyupload.com/?file_id=43114618701497778758
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
__weak typeof(self)weakSelf = self;
NSView *contentView = self.window.contentView;
contentView.wantsLayer = YES;
[self.window setOpaque:NO];
[self.window setHasShadow:NO];
contentView.layer.backgroundColor = [NSColor redColor].CGColor;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf showAlert];
});
}
- (void)showAlert {
DummyAlertWindowController *alertController = [[DummyAlertWindowController alloc] initWithWindowNibName:#"DummyAlertWindowController"];
[self.window beginSheet:alertController.window completionHandler:^(NSModalResponse returnCode) {
;
}];
}
#implementation DummyAlertWindowController
- (void)windowDidLoad {
[super windowDidLoad];
self.properContentView.wantsLayer = YES;
self.properContentView.layer.backgroundColor = [NSColor blueColor].CGColor;
[self.window setOpaque:NO];
[self.window setHasShadow:NO];
self.window.contentView.wantsLayer = YES;
self.window.contentView.layer.backgroundColor = [NSColor clearColor].CGColor;
}
#end
You can recover your non-frosted appearance by making your window borderless.
Add this class to your project.
#interface BorderlessWindow : NSWindow
#end
#implementation BorderlessWindow
- (instancetype)initWithContentRect:(NSRect)contentRect styleMask:(NSWindowStyleMask)style backing:(NSBackingStoreType)backingStoreType defer:(BOOL)flag {
self = [super initWithContentRect:contentRect
styleMask:NSWindowStyleMaskBorderless
backing:backingStoreType
defer:flag];
return self;
}
#end
And set the window class in your XIB to BorderlessWindow
Lastly set the backgroundColor on the window to get the transparency.
#implementation DummyAlertWindowController
- (void)windowDidLoad {
[super windowDidLoad];
//stuff
[self.window setOpaque:NO];
[self.window setHasShadow:NO];
[self.window setBackgroundColor:[NSColor clearColor]];
}
#end
As a side note using wantsLayer to get backgroundColors is better served now by using a custom style NSBox with a custom color or using the backgroundColor property on the window.

Main storyboard setting affect rotation function in iOS8

In my app, the rotation works fine in iOS7 but not working in iOS8.
As you can see, the status bar rotates, but the view controller does not.
I found the issue is caused by main storyboard setting, but I have no idea why it cased the issue. I just removed main storyboard setting in project file and everything works fine in iOS8.
Following is my codes to load storyboard.
[self setMainStoryboard:[UIStoryboard storyboardWithName:#"MainStoryboard-ipad" bundle:nil]];
UIViewController *vc = [mainStoryboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[self.window setRootViewController:vc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
Maybe I used the wrong way to load storyboard?
I fix the problem, but I think this might not be a good solution. It's just a workaround. Anyway, here is what I have done. If you run into the same problem, you can try removing main storyboard setting in your target.
That's it, no further steps. Everything is fine after I remove main storyboard setting. Maybe the way I load main storyboard is incorrect or there is something wrong with my setting of storyboard.
Following is the codes I used to load storyboard:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
UIViewController *vc = [storyboard instantiateInitialViewController];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
// Override point for customization after application launch.
[self.window setRootViewController:nav];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
If anyone know the correct way to use main storyboard, please give me some feedback. Thanks!

Full Screen Cocoa Application - Force Quit

I am facing a weird issue when trying to make a full screen application. I am currently using a a NSWindow with a NSBorderlessWindowMask to make the screen show at top, and I subclassed NSWindow to accept keys:
- (BOOL) canBecomeKeyWindow
{
return YES;
}
This works fine, and I can use keys in my window, and quit out of the application with Command + Q. However, when I try to force quit the application on my Mac, the screen freezes, and I must restart my computer. I have a simple WebView inside my Window:
mainWindow = [[MyBorderWindow alloc] initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO screen:[NSScreen mainScreen]];
[mainWindow setLevel:windowLevel];
[mainWindow setBackgroundColor:[NSColor blackColor]];
[mainWindow makeKeyAndOrderFront:nil];
NSView *contentView = [mainWindow contentView];
WebView *customView = [[WebView alloc] initWithFrame:[contentView bounds]];
[customView setTranslatesAutoresizingMaskIntoConstraints:NO];
[contentView addSubview:customView];
I have tried the application on another computer, and force quit does not freeze the screen. Both computers are running OS X Mavericks.
Please let me know if there is anything else I should add.
Thanks in advance,
Bucco
Edit:
I think that my applicationWillTerminate method in AppDelegate might be part of the issue, but am not sure. Why would this method work fine on another computer, but not mine?
- (void)applicationWillTerminate:(NSNotification *)notification
{
[mainWindow orderOut:self];
// Release the display(s)
if (CGDisplayRelease( kCGDirectMainDisplay ) != kCGErrorSuccess) {
NSLog( #"Couldn't release the display(s)!" );
}
}

cocoa : use of calayer is crashing my application in cocoa?

I am using the following code to set the background color of my nsview using calayer.
CALayer *viewLayer = [CALayer layer];
[self.view setLayer:viewLayer];
[self.view setWantsLayer:YES];
[viewLayer setBackgroundColor:CGColorCreateGenericRGB(0.74, 0.99, 0.79, 1.0)];
This code is crashing my application.
Please help me..
Your code works for me. The only thing that you must check is your view has loaded by that time you set the layer for it.
If you just want to draw the view with specified background color you don't have to set the CALayer for it. The easiest way is make your own NSView subclass:
#interface MyColoredView: NSView
#property (copy) NSColor* backgroundColor;
#end
#implementation MyColoredView
#synthetize backgroundColor;
- (void)drawRect:(NSRect)dirtyRect
{
[self.backgroundColor set];
NSRectFill(dirtyRect)
}
#end
After that you must set the class for your view in IB to MyColoredView and set it's background color during initialization:
self.view.backgroundColor = [NSColor greenColor];

NILauncherView Background Colour

I'm trying to set the background image of a NILauncherViewController included with iOS NimbubKit (update of the three20 kit).
I've tried
self.launcherView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];
And self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];
self.launcherView.backgroundColor = [UIColor clearColor];
But neither work.
Nor does adding a UIImageView as a subview to the viewController and making the launcher view clear.
Yes the images has the correct name.
Edit
If anybody needs to know how to fix this I simply made a UIViewController and added a NILauncherView as a subview and then copied the delegate/datasource methods from the NILauncerViewController
It looks like the view property for the NILauncherViewController is not a NILauncherView. To access the NILauncherView instance just do something like:
NILauncherView *view = [controller.view.subviews objectAtIndex:0];
And you can then set the background color with:
view.backgroundColor = ...

Resources