OSX Lion: different views in fullscreen and in windowed mode - objective-c

I'm trying to make it so that a certain view contained in a window becomes the main content view when fullscreen mode is toggled and goes back to taking just a portion of the window when the user leaves fullscreen mode.
I've come up with the following:
- (void)windowWillEnterFullScreen:(NSNotification *)notification
{
NSLog(#"entering fullscreen");
oldView = [[[NSApplication sharedApplication] mainWindow] contentView];
[oldView retain];
[[[NSApplication sharedApplication] mainWindow] setContentView:myViewOfInterest];
}
-(void)windowWillExitFullScreen:(NSNotification *)notification
{
[[[NSApplication sharedApplication] mainWindow] setContentView:oldView];
}
However this only works for the first bit: the window maximises and the view of interest becomes the only one, but when fullscreen mode is left, the view that was the only one visible in fullscreen mode is no longer in the window.
I'm very new to Objective-C and Cocoa, so could anyone tell me what am I doing wrong?
Thanks in advance!

A view can only be a sub-view to one other view at a time. Your myViewOfInterest is removed as a sub-view (of the view hierarchy) of oldView when you make it the contentView of the window. When you later restore oldView you need to add myViewOfInterest back where it was (and what size it was etc.).

Related

Cocoa: NSUserNotification appears behind NSPopover

I have no idea where to even start trying to fix this but maybe someone can point me in the right direction. I have a status item application that shows a popover when clicked, some user interactions produce an NSUserNotification to warn/inform about changes or errors. But when the notification appears it is shown behind the popover, so if they overlay the user can't read what it says.
Is there a way to force the notification to appear in front of the popover? Like order the notification to the front.
Edit: here's a picture showing part of the notification hidden behind the popover after a user gave bad input. I don't really want to hide the popover to show the error notification.
I've tried stuff lake makeKeyAndOrderFront, activateIgnoringOtherApps, the type of stuff you'd use on a window, but none of it applies to an NSUserNotification so it doesn't work.
Edit 2: I should note that to get the popover to appear in front of everything when called I use activateIgnoringOtherApps and becomeFirstResponder on the popover, but even getting rid of that still shows the notification behind the popover.
Edit 3: The popover is shown relative to a status item in the status bar using
[[self popover] showRelativeToRect:statusItem.view.bounds ofView:statusItem.view preferredEdge:NSMaxYEdge];
When clicked, the status item calls a method that does this:
- (void)openPopover:(id)sender {
if (![_popover isShown] | (_popoverIsClosing)) {
[_preferencesWindow close];
[NSApp activateIgnoringOtherApps:YES];
[_popover becomeFirstResponder];
[statusItemView setHighlighted:YES];
[[self popover] showRelativeToRect:statusItem.view.bounds ofView:statusItem.view preferredEdge:NSMaxYEdge];
[self performSelector:#selector(defaultTextField)];
NSLog(#"popover opened");
} else {
[_preferencesWindow close];
[NSApp activateIgnoringOtherApps:NO];
NSLog(#"popover closed");
[_popover close];
[_popover resignFirstResponder];
[[NSApplication sharedApplication] hide: self];
[statusItemView setHighlighted:NO];
}
}
Thanks for the help.
You want to adjust the window.level.
A notification is using level 18, so as long as your level is lower, you will not have this issue.
You can use these default NSWindow.Level
.normal = 0
.floating = 3
.submenu = 3
.tornOffMenu = 3
Or a custom level NSWindow.Level(rawValue: 17).
An important thing to note is you should set the windows level during or after the popover did show. Otherwise your setting will be reset.
You can do this from the popover.delegate using the function,
func popoverDidShow(_ notification: Notification)
Or listen to the notification directly with NSPopoverDidShowNotification.
This has been tested on macOS 10.15

Exit fullscreen breaks in Mountain Lion after changing window's delegate

I created my project from the Xcode template for non-document based Cocoa Application.
I have a custom NSWindowController that, after being instantiated on startup, takes possession of the default window (defined in MainMenu.xib).
If I leave the app delegate's window as it is, I can toggle fullscreen mode with command-F (set to -toggleFullscreen: in a menu item), or I can exit from fullscreen by pressing ESC.
Once I set my window controller as the window's delegate (I need this to do some OpenGL adjustments on enter/exit fullscreen, etc.), I can still enter fullscreen by pressing command+F, but I can no longer exit fullscreen (save for command+tab to another app, or command+Q).
Also, the Apple docs mention setting the menu action to -toggleFullscreen: and the target to nil. How is this last part done in Interface Builder? (I connected the action to First Responder's -toggleFullscreen:)
What should I do?
So, I found the problem (posting the question in SO seems to be a condition to finding the solution, always...)
The offending line was not setting the delegate, but what I was doing to the window after entering fullscreen mode. In particular, as soon as I commented out the following line
[window setStyleMask:NSBorderlessWindowMask]; in the code below:
- (void) windowDidEnterFullScreen:(NSNotification*) notification
{
NSWindow* window = [self window];
NSRect mainDisplayRect = [[NSScreen mainScreen] frame];
[window setStyleMask:NSBorderlessWindowMask];
[window setContentSize:mainDisplayRect.size];
[window setLevel:NSMainMenuWindowLevel + 1];
[window makeKeyAndOrderFront:self];
NSRect windowFrame = [window frame];
windowFrame.origin.x = 0;
windowFrame.origin.y = 0;
[window setFrame:windowFrame display:YES];
}
...the expected enter/exit fullscreen mode behaviour was fixed.

Show Window without activating (keep application below it active)

I need to show a window (without title bar) above third party applications without my window taking focus.
I have tried using an NSPanel and setting enabling non-activating, but that didn't help.
I tried orderFront:self, but that didn't help either.
I always needed to add [NSApp activateIgnoringOtherApps:YES]; because the window wouldn't show otherwise.
I have here a sample project for just this functionality:
http://users.telenet.be/prullen/TopW2.zip
UIElement is set to true in the application's plist file, so there is no dock. You can activate the window by pressing ALT + SPACE at the same time. You will see that the app below it looses focus. Any thoughts on how to fix this? I've seen other apps do it so I know it's possible.
Edit: here's the code so far. Remember the window is a non-activating NSPanel.
I still need that last NSApp activateIgnoringOtherApps line or otherwise it doesn't display. But of course that makes the window the active one.
_windowController = [[MyWindowController alloc] initWithWindowNibName:#"MyWindowController"];
[[_windowController window] setLevel:NSNormalWindowLevel+1];
[[_windowController window] orderFrontRegardless];
[_windowController showWindow:self];
[NSApp activateIgnoringOtherApps:YES];
I've also subclassed NSPanel and added two methods:
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (BOOL)canBecomeMainWindow
{
return YES;
}
Edit: OK, unchecking setHidesOnDeactivate fixes this, but now the window will never hide. I need it to hide when the user presses the app below it or switches to another app.
Edit 2: OK, this seems to fix the above issue:
- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(hideWindow) name:NSWindowDidResignKeyNotification object:nil];
}
- (void)hideWindow {
[self setHidesOnDeactivate:YES];
}
Not sure if there's a better way.
And for those that want to know how to display the window:
[[_windowController window] setLevel:NSPopUpMenuWindowLevel];
[[_windowController window] orderFrontRegardless];
[[_windowController window] makeKeyWindow];
[_windowController showWindow:self];
Either one of these should do the trick:
Use -[NSWindow orderFrontRegardless] to get a normal level window to the front without activating the corresponding app, or
Use -[NSWindow setLevel:] to increase the window level to something higher than NSNormalWindowLevel
Not to take away from #puzzle's useful answer, but it sounds like your problem has something to do with using an NSPanel instead of an NSWindow.
The "How Panels Work" docs say:
Onscreen panels, except for alert dialogs, are removed from the screen when the application isn’t active and are restored when the application again becomes active. This reduces screen clutter.
Specifically, the NSWindow implementation of the hidesOnDeactivate method returns NO, but the NSPanel implementation of the same method returns YES.
So perhaps you could override hidesOnDeactivate to return NO, or change to NSWindow

subview from UIWindow and sending it back to UIwindow

I have an UIWebView loaded with div, act as editor to write. Now i am adding UIWebView as sub view on UIWindow to set the frame equal to full screen and to hide the UIKeyboard, but at some button method, i need to get UIWebview from UIWindow and sent it back to UIKeyboard. Here is my code which is not working:
keyboardWindowFrame= nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows])
{
if (![[testWindow class] isEqual:[UIWindow class]])
{
keyboardWindowFrame = testWindow;
[webViewForEditing setFrame:CGRectMake(5, 63, 310, 400)];
[webViewForEditing.scrollView setContentSize:CGSizeMake(310, 400)];
[keyboardWindowFrame addSubview:webViewForEditing];
break;
}
}
- (IBAction)keyboardButtonSelected:(id)sender
{
[keyboardWindowFrame sendSubviewToBack:webViewForEditing]; //need to send UIWebView back to UIWindow so i can write
}
I think I am trying to do the same thing as you and so although your question is not particularly clear hopefully my answer will help.
It seems that you have a view 'webViewForEditing' which you want to add and bring in front of the keyboard. When you click a button you want to put this view behind the keyboard again.
I have also tried using the sendSubViewToBack code with no joy.
In the end though I managed to get it to work using:
[[self view] exchangeSubviewAtIndex: withSubviewAtIndex: ];
(Credit goes to Sunny with it adapted from a question here)
I used the following code below, with to switch between a UIView and the keyboard:
- (IBAction)toggleButtonPressed:(id)sender {
UIWindow * window = [UIApplication sharedApplication].windows.lastObject;
if (window.subviews.count == 1) {
[window addSubview:_menuView];
}
else {
[window exchangeSubviewAtIndex: 0 withSubviewAtIndex: 1];
}
}
I am only working with two views (_menuView and the keyboard) which means I check how many subviews my window has to make sure I only add _menuView once.
Then it is easy to exchange the two views.
Even if you had more subviews in your window I am sure you could use a modified version of this. As long as none of your other views change places then exchanging them will always switch the same two views.
NOTE: As an aside. I am not sure if this code works if called when the keyboard is not first responder. I get the variable for Window using the last object in the window, which is always the keyboard if it has been made first responder. It might need tweaking to get it working at other times.
I formulated another, more eloquent, answer while working on this:
Add the views you want to the window view that holds the keyboard view:
#interface BViewController : UIViewController {
UIWindow * window;
}
// Add the code when the keyboard is showing to ensure it is added while we have a keyboard and to make sure it is only added once
-(void) keyboardDidShow: (NSNotification *) notification {
window = [UIApplication sharedApplication].windows.lastObject;
if (window.subviews.count == 1) {
[window addSubview:_menuView];
[window addSubview:_gameView];
[window addSubview:_stickerView];
[self hideViews];
}
}
- (void)hideViews {
_menuView.hidden = YES;
_gameView.hidden = YES;
_stickerView.hidden = YES;
}
So now we have our UIWindow view which contains our views and also our keyboard. They are all hidden so our keyboard appears at the front and can be typed on.
Now use a simple function to decide which view to bring in front of the keyboard:
- (void)bringViewToFront: (UIView *)view {
[self hideViews];
view.hidden = NO;
}
Hiding the views makes sure we only have our correct view at the front.
I spent a lot of time thinking how we could move different views forward and back, using the exchange function. Actually using hide and reveal means we can get our view immediately and still easily have access to the keyboard by hiding all the views.

UISplitViewController non root, forcing custom rotation methods, makes master view dissappear

I'm trying to add a split view inside of a tab bar, and since the split view isn't the root, it doesn't properly get the rotation notifications, so the delegate's methods are never called to add the button to the toolbar in the detail view.
I've rigged it up so I can generate the popover when rotated, but when this method is called, the view dissappears from the landscape mode, and if you activate it and then rotate back into landscape, it's a black empty box where the master view used to be. How do I get rid of this occuring?
-(void) displayPopover:(id)sender
{
//Toggle the popover: if it's showing, hide it
if (popoverController != nil && [popoverController isPopoverVisible])
{
[popoverController dismissPopoverAnimated:NO];
}
else
{
//Create a Popover displaying the master view
if (popoverController == nil)
{
popoverController=[[UIPopoverController alloc] initWithContentViewController:self->rootController];
popoverController.popoverContentSize=CGSizeMake(300, 500);
}
[popoverController presentPopoverFromBarButtonItem:[detailController.toolbar.items objectAtIndex:0] permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
You will need to remove all the objects from window using:
[appdelegate window ] subviews] objectAtIndex:0] removeFromSuperview];
Then add your splitview to the window, you can get the view callbacks.
I would recommend either finding a way to get your SplitViewController to be root, or creating a custom subclass of the UISplitViewController that allows for non-root placement.
I really like what Matt Gemmell did here: http://mattgemmell.com/2010/07/31/mgsplitviewcontroller-for-ipad
Using a custom subclass like Matt's will allow you to benefit from all the same delegate methods that a SplitView as root would allow. I used it in a project where I wanted my SplitView to appear as a modal - almost impossible with a traditional UISplitViewController.
so your split view has rotation enabled (shouldAutorotateToInterfaceOrientation:) now you have to make sure that the tab controller has also rotation enabled (should be the appDelegate, am I right?) AND you have to make sure that every other view that is in your TabBar has also rotation enabled!
so if your TabBar contains 2 tabs you have to set the rotation in 3 classes.