Reputation: 47
I'm (re-)writing a small app to control a USB device. I would like the window of that app to be always entirely visible. So when you move around the window, it stops moving when the bottom hits the bottom of the screen or one of the sides hits the side of the screen.
Is this at all possible?
Upvotes: 1
Views: 1170
Reputation: 558
With the code example provided by Ken Aspeslagh no longer available via the Dropbox link, I thought it would be useful to share my solution. For my project, I only needed to worry about the bottom and right side of the screen, but others should be able to adapt this to account for the top and left side of the screen.
- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
{
if (((self.frame.origin.x + self.frame.size.width) > screen.frame.size.width) && (screen.frame.size.width > 0))
{
frameRect.origin.x = (screen.frame.size.width - self.frame.size.width);
}
if (self.frame.origin.y < 0)
{
frameRect.origin.y = 0;
}
return frameRect;
}
Upvotes: 0
Reputation: 11594
If you want to prevent the user from being able to freely move the window, simply make a custom window with no title bar. Having a title bar indicates to the user that the window can be moved, so the solution here is to not have one. Then, there's no control for moving it. You'll need to provide your own controls for closing the window.
Upvotes: 0
Reputation: 11594
Yes, it's possible.
Normal NSWindows with a title bar constrain their own frames to not allow the top of the window to leave the top of the screen. This is done in -[NSWindow constrainFrameRect:toScreen:]
You can override this method to constrain the frame however you'd like.
Upvotes: 2