How to bring NSWindow to front and to the current Space? - swift

I'm using this code to bring up my window:
[self.window makeKeyAndOrderFront:self];
[self.window setOrderedIndex:0];
But often it will be beneath other windows or displayed in other Space desktop that I last open it in. How do I make it always appear "in front" to the user?
Update
I found the answer from previously asked question; make the window show up to the top of other windows:
[NSApp activateIgnoringOtherApps:YES];
But how to move the app to the user's current Space?

To bring your app to the front:
[NSApp activateIgnoringOtherApps:YES];
Swift:
NSApp.activateIgnoringOtherApps(true)
Swift 3:
NSApp.activate(ignoringOtherApps: true)

Perhaps you want:
[self.window setCollectionBehavior: NSWindowCollectionBehaviorCanJoinAllSpaces];
Experiment with the other collection behaviors... I found NSWindowCollectionBehaviorMoveToActiveSpace was a bit buggy in 10.5 but it might be better now.

Swift 5.0 +
NSWindow.orderFrontRegardless()

The syntax seems to be a little different with Swift 2:
NSApplication.sharedApplication().activateIgnoringOtherApps(true)

Swift 3.0
NSApp.activate(ignoringOtherApps: true)

But how to move the app to the user's current Space?
You don't. Spaces hold windows, not applications. (That's why the collectionBehavior property is on NSWindow, not NSApplication.)

If you want to your app to come to the front, even if its minimised and have it activated (given active focus), then use both of these:
NSApp.activate(ignoringOtherApps: true)
NSApp.windows.first?.orderFrontRegardless()
If you only want to bring it to the front, without giving it focus, just use:
NSApp.windows.first?.orderFrontRegardless()

Swift 2.0
NSApp.activateIgnoringOtherApps(true)
Helpful Programming Tips and Hacks

Related

Mac OS - Swift - Get screen containing the dock

I would like to get the screen containing the dock and the menubar.
Is there a way to do this?
I've check Apple documentation about NSScreen but couldn't find any information about that.
Thanks!
Actually, the answer was, as #Willeke suggested, in the documentation of NSScreen.screens (see:
https://developer.apple.com/documentation/appkit/nsscreen/1388393-screens).
The screen considered as main (with the menubar) is the one with index 0.
It is not the same as NSScreen.main which is the screen with the current active window.

Caret cursor color and size

How do I change the size and colour of the flashing cursor in my textfield using my storyboard for my Mac app.
I assume I have to connect it into the view controller script (control drag) and edit some uitextfield color parameter for the cursor?
But I don't seem to be getting anywhere fast. I am working in swift 2.2 with Mac app Storyboards. Like Ulysses and Taskpaper.
In Swift 3:
Change YOURTEXTFIELD to your textfield and you are set:
(YOURTEXTFIELD.value(forKey: "textInputTraits") as AnyObject).setValue(UIColor.black, forKey: "insertionPointColor")
Documentation: https://developer.apple.com/documentation/appkit/nstextview/1449309-insertionpointcolor
Search for _drawInsertionPointInRect that's the private method that you need. Though you also need some other stuff I think... but it's all mixed into my code so I can't say for sure what it all is. Anyway search for _drawInsertionPointInRect and you'll get to some explanations.

xcode 4.2+ Dismiss numberpad

I am looking for a way to dismiss the number pad in xcode 4.2. I have seen a few suggestions for xcode 3 but none of them seem to work for me. It seems silly that there is no clear/non work around way to dismiss the numberpad of a uitextview. Maybe I am just missing something but I have tried about 10 diff ways with no success. If someone could point me towards a current tutorial or share some super secret tricks that would be great thanks!
Dismissing keyboard on any text input control is simply sending resignFirstResponder message to the control. You don't control the keyboard directly.
And this is completely unrelated to XCode version.

SplitView like Facebook app on iPhone

I want to create an iPhone app that uses a navigation scene similar to the one pictured in the link
Please note I do not want this to only work for iPad, I want it to work for iPhone exactly as pictured, when you click on a tableview item it hides the tableview and makes that view full screen. I want ideas on how to do this because I cannot figure it out myself.
Thanks
Facebook guys have done brilliant job in the new version of the app. The similar open source code can be found from here - JTRevealSidebarDemo. Please note that as of June 2014, this project has been discontinued, so you'll probably have better luck with a project from the list below.
It reveals technique behind doing split view for iPhone.
Some other open source code:
JWSlideMenu
DDMenuController
PKRevealController
ViewDeck
ECSlidingViewController
MWFSlideNavigationViewController
MFSideMenu
SASlideMenu
HHTabListController
MTSlideViewController
MTStackViewController
MMDrawerController
DMSideMenuController
JVFloatingDrawer
How about projects with storyboard compatibility?
I found 1 more slide menu which is compatible with storyboards:
SASlideMenu
Another storyboard-compatible menu is
ECSlidingViewController
and
ViewDeck
from Sagar's answer. They both have storyboard examples (for the last one link is ViewDeckStoryboardExample)
For anyone else looking for an Android version, take a look at:
android fb like slideout navigation
emerging ui pattern side navigation
I realize you asked about facebook, but now that ios7 is out, and this is the defacto thread i thought id post here.
For an effect similar to the kindle app on ios7 you can use:
https://github.com/romaonthego/REFrostedViewController
If anyone else is looking for a way to implement this in MonoTouch now known as Xamarin.ios, take a look at this article I just found. monotouch slide out navigation
EDIT
I just found that they have a free component for this!
flyoutnavigation
I noticed no one listed this wonderful class... SWRevealController.
I use it with my project apps all the time. It's Easy to use and heavily documented... There are also a few examples John gives to the user to understand how it works or if you'd like to derive your project from... Hope this helps
SHSidebarControllerwith filder effect.
This might be helpful to you.. try this :)
This looks to be the best match for me.
PKRevealController
It has nice scroll effect with finger and moves back on partial drag.
https://github.com/pkluz/PKRevealController
MMDrawerController is very good option.
You can configure many things. try it once
https://github.com/mutualmobile/MMDrawerController
I've been working on a floating-style navigation drawer that I hope people will like. It's on GitHub, take a look.

iPhone keyboard tweak

I would reduce the time to get special characters when keeping a touch on the keyboard. Anyone has a clue?
Thanks.
(Undocumented) Override -[UIKeyboardImpl touchLongPressTimer] to call
-(void)touchLongPressTimer {
[self touchLongPressTimerWithDelay:0.5f]; // default = 1.8125f
}
I strongly doubt this is possible using the public API. You'd have a file bug with Apple to ask them to make this possible. It does however seem unlikely, as it would result in differing behavior between apps in a common control.
You could write your own keyboard control for your own app :)