Cocoa App Enter Full Screen - objective-c

I am developing a simple application which make use of full screen window.
Window contains view that contains button, image etc etc... , but when I enter in full screen with the follow lines:
NSWindow* tmp = [self window];
[tmp setStyleMask:NSBorderlessWindowMask];
[tmp setFrame:[tmp frameRectForContentRect:[[tmp screen] frame]]display:YES animate:NO];
[tmp setBackingType:NSBackingStoreBuffered];
screenRect = [[NSScreen mainScreen] frame];
int windowLevel = CGShieldingWindowLevel();
[self.window setLevel:windowLevel];
The view I put in the window doesn't resized automatically, I could make some operation for resize correctly that work, but there are a way to do that automatically?
I post all my AppController here:
-(id)init {
self = [super initWithWindowNibName:#"MainWindow"];
NSWindow* tmp = [self window];
[tmp setStyleMask:NSBorderlessWindowMask];
[tmp setFrame:[tmp frameRectForContentRect:[[tmp screen] frame]]display:YES animate:NO];
[tmp setBackingType:NSBackingStoreBuffered];
screenRect = [[NSScreen mainScreen] frame];
/**
// [[tmp standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
// [[tmp standardWindowButton:NSWindowZoomButton] setHidden:YES];
self.window = [[NSWindow alloc] initWithContentRect:screenRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO screen:[NSScreen mainScreen]];
**/
int windowLevel = CGShieldingWindowLevel();
[self.window setLevel:windowLevel];
return self;
}
// We need to be layer-backed to have subview transitions.
-(void)awakeFromNib {
[[self window] setContentSize:[topMenu frame].size];
[[[self window] contentView] addSubview:topMenu];
[topMenu enterFullScreenMode:[NSScreen mainScreen] withOptions:nil];
[[[self window] contentView] setWantsLayer:YES];
}
- (void)dealloc
{
[super dealloc];
}
- (void)windowDidLoad
{
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
[topMenu_controller performAnimation];
return;
}

You can use the springs and struts of Interface Builder to set a view's autosizing behavior:
http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/IB_UserGuide/Layout/Layout.html

Related

Cocoa NSAnimationContex Weird behaviour

I have an NSView NSButton and an NSTextField in this view's subviews
all of them are Layer Backed
I ma trying to animate this view changing frame.
- (void)animateTheView:(SPTabView *)view toFrame:(NSRect)endFrame
{
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context)
{
context.allowsImplicitAnimation = YES;
context.duration = ANIMATION_TIME;//is a define 0.6f
[view setFrame:endFrame];
} completionHandler:nil];
}
Edit In my view I am not using AutoLayout I am just overriding the setFrame:
- (void)setFrame:(NSRect)frame
{
[super setFrame:frame];
[_titleLabel setFrame:self.bounds]; //The NSTextField I am talking about
[_closeButton setFrame:NSMakeRect(5, 2, B_WIDTH, B_HEIGHT)];//This one works fine
[self removeTrackingRect:_tracking];
_tracking = [self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:NO];
}
EDIT 2 Posting the subViews Initialisation Code
[super setWantsLayer:YES];
[self setWantsLayer:YES];
[self setCanDrawSubviewsIntoLayer:YES];
[self drawRegular];
_closeButton = [[NSButton alloc]init];
[_closeButton setCanDrawSubviewsIntoLayer:YES];
[_closeButton setTitle:#"X"];
[_closeButton setTarget:self];
[_closeButton setAction:#selector(closeTab)];
[_closeButton setHidden:YES];
[_closeButton setWantsLayer:YES];
_titleLabel = [[SPLabel alloc]init]; //SPLabel is a silly subclass of NSTextField which just set it's properties like editable to as NO
[_titleLabel setText:#"Untitled.txt"];
[_titleLabel setTextColor:[NSColor blackColor]];
[_titleLabel setAlignment:NSCenterTextAlignment];
[_titleLabel setWantsLayer:YES];
[_titleLabel setTag:6];
[self addSubview:_titleLabel];
[self addSubview:_closeButton];
My view and it's button animating FINE! but the textField doesn't animate at all - it just jumps to the EndFrame...
Any Ideas some1 ?

NSToolbar with custom views

Hi so I've got a problem that i can't really figure out what the cause is. I made an app so far that has a NSToolbar that has custom views in it so that you can switch between views on the toolbar (just like safari's preference window toolbar views). I was using CCodings tutorial on youtube but i have the problem of the views moving by themselves, for example i click on view 1 and when i click on view 2 view one changes and the view moves up so that you can only see half of when i put on the view, I'm using Xcode 6 and OS X 10.10 so i don't know if that would be one cause but i thank you in advance if you can help me, heres the MainWindowController.m file:
Also the views should be setup correctly
-(NSRect)newFrameForNewContentView:(NSView*)view {
NSWindow *window = [self window];
NSRect newFrameRect = [window frameRectForContentRect:[view frame]];
NSRect oldFrameRect = [window frame];
NSSize newSize = newFrameRect.size;
NSSize oldSize = oldFrameRect.size;
NSRect frame = [window frame];
frame.size = newSize;
frame.origin.y -= (newSize.height - oldSize.height);
return frame;
}
-(NSView *)viewForTag:(int)tag {
NSView *view = nil;
switch (tag) {
case 0:
view = smallView;
break;
case 1:
view = mediumView;
break;
case 2: default:
view = largeView;
break;
}
return view;
}
- (BOOL)validateToolbarItem:(NSToolbarItem *)item {
if ([item tag] == currentViewTag) return NO;
else return YES;
}
-(void)awakeFromNib {
[[self window] setContentSize:[smallView frame].size];
[[[self window] contentView] addSubview:smallView];
[[[self window] contentView] setWantsLayer:YES];
}
-(IBAction)switchView:(id)sender {
int tag = [sender tag];
NSView *view = [self viewForTag:tag];
NSView *previousView = [self viewForTag:currentViewTag];
currentViewTag = tag;
NSRect newFrame = [self newFrameForNewContentView:view];
[NSAnimationContext beginGrouping];
if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask)
[[NSAnimationContext currentContext] setDuration:1.0];
[[[[self window] contentView] animator] replaceSubview:previousView with:view];
[[[self window] animator] setFrame:newFrame display:YES];
[NSAnimationContext endGrouping];
}
Seems that you also need to change NSWidnow frame apart from contenView.
Also as for me for switching views by click events its better to use NSTabView.

Objective C, rounded corner custom window?

I have a subclass of NSWindow to customize one of my windows for my app.
I have everything set, but I am not sure how to make the corners round.
Currently, my window is a transparent rectangular window with some buttons, labels, and a textfield in it.
The class includes:
#import "TransparentRoundRectWindow.h"
#implementation TransparentRoundRectWindow
-(id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self) {
[self setAlphaValue:0.75];
[self setOpaque:YES];
[self setHasShadow:YES];
[self setBackgroundColor:[NSColor clearColor]];
}
return self;
}
-(BOOL)canBecomeKeyWindow
{
return YES;
}
I just need to make the corners round now. I tried searching for similar situations and saw some of them explaining to override the drawRect method but I couldn't get them to work.
How could I do this?
(I'm using Mac OS X Lion)
Thanks in advance.
You need to set to Your window Opaque to NO. And subclass Your window's view.
Window subclass:
-(id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self) {
[self setOpaque:NO];
[self setHasShadow:YES];
[self setBackgroundColor:[NSColor clearColor]];
}
return self;
}
-(BOOL)canBecomeKeyWindow
{
return YES;
}
Window's view subclass:
- (void)drawRect:(NSRect)rect
{
NSBezierPath * path;
path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:8 yRadius:8];
[[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.75] set];
[path fill];
}
Result:
More explanation how to do this:
Create new NSView class and paste "Window's view subclass" code which I wrote in it. Then go to Your window's view.
Here is window's view click on it:
Go to the Identity inspector and set class to your created class:

View Won't Accept First Responder Status

I'm creating a new window and view programmatically. I need the view to accept First Responder status. For whatever reason it's not.
I'm doing this old school because I want my app to be compatible with older versions of OSX. Here's what I've got:
- (IBAction)startShow:(id)sender {
// Capture the main display
if (CGDisplayCapture( kCGDirectMainDisplay ) != kCGErrorSuccess) {
NSLog( #"Couldn't capture the main display!" );
}
// Get the shielding window level
windowLevel = CGShieldingWindowLevel();
// Get the screen rect of our main display
screenRect = [[NSScreen mainScreen] frame];
// Put up a new window
displayWindow = [[ShowWindow alloc] initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO screen:[NSScreen mainScreen]];
[displayWindow setBackgroundColor:[NSColor blackColor]];
[displayWindow setLevel:windowLevel];
[displayWindow makeKeyAndOrderFront:nil];
// Load show window with options from settings window.
[displayWindow loadOptions:screenRect];
}
Just to clarify, displayWindow is an instance of ShowWindow, which is a subclass of NSWindow. In my ShowWindow class I'm doing the following:
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (void) loadOptions:(NSRect)screenRect {
// Create custom view to hold text fields
view = [[ShowView alloc] initWithFrame: screenRect];
[self setContentView:view];
// Track Title Text Field Initialize
textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, screenRect.size.height / 4, screenRect.size.width, 350)];
[textField setStringValue:#"My Label"];
[textField setBezeled:NO];
[textField setDrawsBackground:NO];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setTextColor:[NSColor whiteColor]];
[textField setFont:[NSFont fontWithName:#"Helvetica" size:92]];
[textField setAlignment:NSCenterTextAlignment];
// Add track title text field to main view.
[view addSubview:textField];
// Make the view first responder
[self makeFirstResponder:view];
}
view is an instance of ShowView, which is a subclass of NSView. Last but not least here's what I've got going on in ShowView:
// Next four methods set main view as first responder to accept keyboard input
- (BOOL)acceptsFirstResponder
{
NSLog(#"I accepted being a first responder! Yea!");
return YES;
}
- (BOOL)resignFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
- (BOOL)becomeFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
- (void)insertText:(NSString *)input
{
}

How to Animate a custom NSWindow

I'm having some troubles animating a custom NSWindow.
Here's my init method:
- (CustomWindow *)initWithView:(NSView *)view
{
if ((self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO])) {
[[self contentView] addSubview:view];
[self setBackgroundColor:[NSColor darkGrayColor]];
[self setMovableByWindowBackground:YES];
[self setExcludedFromWindowsMenu:YES];
[self setOpaque:NO];
[self setHasShadow:YES];
[self setDelegate:self];
}
return self;
}
When I call [self setFrame:originalFrame display:NO animate:YES]; there is a delay that corresponds to the animation duration, but the animation itself doesn't occur.
I think this is because I'm using a borderless window?
Simply use this: [self setFrame:originalFrame display:YES animate:YES];
Guess your window is resized, but view you add to content view, don't. If you have only one view, then instead of:
[[self contentView] addSubview:view];
use:
[self setContentView:view];