How to restore window position in an OSX application?
This is a bug in Xcode 6 and I don't know if it is fixed in Xcode 7.
Setting autosave in InterfaceBuilder has no effect. To get it to work just set its name in windowDidLoad()
of your windowController:
class MyWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
self.windowFrameAutosaveName = "position"
}
}
Save and restore applications and layout
This is a feature of Lion (Mac OS X 10.7) ... I wouldn't kill yourself over a feature Apple has seen the need for and has implemented rather seamlessly...
Remember window sizes and placement when unplugging and replugging second monitor
Have a look at Stay by Cordless Dog. I believe it does exactly what you're looking for.
Getting window restored bounds
There is a method prepared for this task in NSWindow
.
You could ask your ViewController for its NSWindow and set an AutosaveName for its Frame like..
[self.view.window setFrameAutosaveName:@"VerySpecialWindowAutoSaveName"];
which will end up in NSUserDefaults
of your App as entry like...
"NSWindow Frame VerySpecialWindowAutoSaveName" = "300 100 1200 1005 0 0 2560 1289"
But the best place for this code is ... there is no best place because it depends on your apps approach.
The whole process can be challenging when you have multiple windows in a document based application but as you can set the AutosaveName per documents window you are able to recover the frame if needed, at least for the last document. Should be mentioned that you can set the AutosaveName in InterfaceBuilder and in Code as well - so keep an eye on it they follow the same name if you use both IB & code for one and the same window.
and an example in swift you can find in this gist
Save NSWindow Size on Resize & Close For User
Apple makes it easy. In interface builder, for your window, just type in a unique name in the Autosave
field and it will save it under that name in the global user defaults. E.G.
How do you get the current Mac window position/origin programatically in swift
You need to get the view.window.frame
, rather than view.frame
.
if let window = view.window {
print(window.frame.origin)
} else {
print("The view is not in a window yet!")
}
Remember to do this only after view
is added to the window, such as in viewDidAppear
.
The view
's frame is relative to view
's superview's origin, not the screen's origin. and it just so happens that in this case, view
is positioned exactly at the origin of its superview (whatever view that is), hence (0, 0).
Reset Xcode 4 window positions
You need to delete the xcuserdata/username.xcuserdatad/UserInterfaceState.xcuserstate file inside the workspace bundle. You can get to it via Finder "Show Package Contents" or via the Terminal.
If you're not using a workspace, your xcodeproj will have a workspace file inside it that you can open up.
Delete the file and re-open the workspace and your window settings will be back to default.
BTW, this happens to me all the time when I accidentally leave a double-clicked file open and then close the main window.. it always comes back helpfully showing the single file with nothing else.
Related Topics
Drag and Drop Image from Uicollectionview to Uiview
Swift Generic Constraints in Init
Ios8 Swift Uisearchcontroller Hides Navigationbar
Using Decodable with Inheritance Raises an Exception
How to Get Part of a String in Swift
Swift 3: Converting Data to String Returns a Nil Value
Ambiguous Use of Operator '-' in Swift with 'Abs()'
Is .Playground a Swift File? Who Can 'See' It
Delegate Property with Different Type in Swift
Using @Discardableresult for Closures in Swift
How to Make a Function Complete Before Calling Others in an Ibaction