Skip to content

Instantly share code, notes, and snippets.

@1024jp
Created December 19, 2013 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1024jp/8032238 to your computer and use it in GitHub Desktop.
Save 1024jp/8032238 to your computer and use it in GitHub Desktop.
NSScrollViewの内容センタリング ref: http://qiita.com/1024jp/items/f3cd7bc7765cf090fa31
#import <Cocoa/Cocoa.h>
@interface MyCenteringClipView : NSClipView
@end
#import "MyCenteringClipView.h"
@implementation MyCenteringClipView
- (NSRect)constrainBoundsRect:(NSRect)proposedBounds
{
NSRect documentBounds = [[self documentView] bounds];
NSPoint delta = NSMakePoint(NSWidth(documentBounds) - NSWidth(proposedBounds),
NSHeight(documentBounds) - NSHeight(proposedBounds));
CGFloat x = (delta.x < 0) ? (delta.x / 2) : MAX(0, MIN(proposedBounds.origin.x, delta.x));
CGFloat y = (delta.y < 0) ? (delta.y / 2) : MAX(0, MIN(proposedBounds.origin.y, delta.y));
proposedBounds.origin = NSMakePoint(floor(x), floor(y));
return proposedBounds;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment