FROM : Steve Weller
DATE : Sat Mar 01 06:31:56 2008
On Feb 29, 2008, at 4:00 PM, Steve Weller wrote:
>
> I have a custom view into which I can draw a background color and a
> centered rectangle. As the window is resized, the rectangle stays
> centered and is clipped when the window gets small.
>
> I want to define a canvas size slightly bigger than the rectangle
> and have the scrollers appear when the available space is less than
> the canvas needs. I override setFrameSize to do this:
>
> - (void) setFrameSize:(NSSize)newSize
> {
> NSSize cSize;
> cSize = [self canvasSize]; // Provides a size
>
> // Use the larger dimensions of the two rects
> if(newSize.width > cSize.width)
> cSize.width = newSize.width;
> if(newSize.height > cSize.height)
> cSize.height = newSize.height;
>
> [super setFrameSize:cSize];
> }
>
> And this works as long as the window is only made smaller. If the
> window is made larger then the scrollers do not disappear.
>
> Is my approach the right one for what I am trying to achieve?
>
> If it is, how can I fix the scroller problem?
I figured it out. Key to my confusion was that I was not involving the
superview in the calculation.
In my -awakeFromNib I put this code:
// Receive notifications if the frame changes
[self setPostsBoundsChangedNotifications: YES];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter] ;
[center addObserver: self
selector: @selector(frameDidChangeNotification:)
name: NSViewFrameDidChangeNotification
object: self];
NSSize cSize;
cSize = [self canvasSize];
[self setFrame:NSMakeRect(0,0,cSize.width,cSize.height)];
and I added
// The frame has changed
-(void)frameDidChangeNotification:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
NSRect frame = [[self superview] frame];
NSSize cSize;
cSize = [self canvasSize];
// Use the larger dimensions of the canvas and the superview
if(frame.size.width > cSize.width)
cSize.width = frame.size.width;
if(frame.size.height > cSize.height)
cSize.height = frame.size.height;
[self
setFrame:NSMakeRect
(frame.origin.x,frame.origin.y,cSize.width,cSize.height)];
// NSLog(@"%f %f",newSize.width, newSize.height);
NSNotificationCenter *center = [NSNotificationCenter defaultCenter] ;
[center addObserver: self
selector: @selector(frameDidChangeNotification:)
name: NSViewFrameDidChangeNotification
object: self];
}
This figures out the rectangle that encloses both the canvas and the
superview's frame and makes my custom view's frame equal to that. The
only remaining thing to fix is that the lower left point is always
shown in the view, when I actually want the center point to be shown.
So I have to shift the frame origin as part of the calculation.
DATE : Sat Mar 01 06:31:56 2008
On Feb 29, 2008, at 4:00 PM, Steve Weller wrote:
>
> I have a custom view into which I can draw a background color and a
> centered rectangle. As the window is resized, the rectangle stays
> centered and is clipped when the window gets small.
>
> I want to define a canvas size slightly bigger than the rectangle
> and have the scrollers appear when the available space is less than
> the canvas needs. I override setFrameSize to do this:
>
> - (void) setFrameSize:(NSSize)newSize
> {
> NSSize cSize;
> cSize = [self canvasSize]; // Provides a size
>
> // Use the larger dimensions of the two rects
> if(newSize.width > cSize.width)
> cSize.width = newSize.width;
> if(newSize.height > cSize.height)
> cSize.height = newSize.height;
>
> [super setFrameSize:cSize];
> }
>
> And this works as long as the window is only made smaller. If the
> window is made larger then the scrollers do not disappear.
>
> Is my approach the right one for what I am trying to achieve?
>
> If it is, how can I fix the scroller problem?
I figured it out. Key to my confusion was that I was not involving the
superview in the calculation.
In my -awakeFromNib I put this code:
// Receive notifications if the frame changes
[self setPostsBoundsChangedNotifications: YES];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter] ;
[center addObserver: self
selector: @selector(frameDidChangeNotification:)
name: NSViewFrameDidChangeNotification
object: self];
NSSize cSize;
cSize = [self canvasSize];
[self setFrame:NSMakeRect(0,0,cSize.width,cSize.height)];
and I added
// The frame has changed
-(void)frameDidChangeNotification:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
NSRect frame = [[self superview] frame];
NSSize cSize;
cSize = [self canvasSize];
// Use the larger dimensions of the canvas and the superview
if(frame.size.width > cSize.width)
cSize.width = frame.size.width;
if(frame.size.height > cSize.height)
cSize.height = frame.size.height;
[self
setFrame:NSMakeRect
(frame.origin.x,frame.origin.y,cSize.width,cSize.height)];
// NSLog(@"%f %f",newSize.width, newSize.height);
NSNotificationCenter *center = [NSNotificationCenter defaultCenter] ;
[center addObserver: self
selector: @selector(frameDidChangeNotification:)
name: NSViewFrameDidChangeNotification
object: self];
}
This figures out the rectangle that encloses both the canvas and the
superview's frame and makes my custom view's frame equal to that. The
only remaining thing to fix is that the lower left point is always
shown in the view, when I actually want the center point to be shown.
So I have to shift the frame origin as part of the calculation.
| Related mails | Author | Date |
|---|---|---|
| Steve Weller | Mar 1, 01:00 | |
| Quincey Morris | Mar 1, 01:46 | |
| Steve Weller | Mar 1, 03:10 | |
| Quincey Morris | Mar 1, 03:40 | |
| Quincey Morris | Mar 1, 04:31 | |
| Steve Weller | Mar 1, 06:31 | |
| Quincey Morris | Mar 1, 07:33 | |
| Steve Weller | Mar 1, 17:57 | |
| Kyle Sluder | Mar 1, 18:37 |






Cocoa mail archive

