Reputation: 1430
I just started a brandnew project GrafLaboratory to do some experimenting on drawing in NSView. I added a custom view to the main window and configured it to adopt it´s size when the window changes it´s size.
I modified my InitFrameWith to this:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSRect r = [self bounds];
}
return self;
}
When I start my new app the first time and ask for [self bounds] I get the correct value: {{0, 50}, {600, 450}}. I then change the size of the window by dragging with the mouse. When restarting my app initWithFrame returns the same value: {{0, 50}, {600, 450}}.
But I the new changed size. I already tried several things but without success.
Any clues what I´m doing wrong?
Thanks, Ronald
Upvotes: 1
Views: 702
Reputation: 39296
The initWithFrame is only relevant when you initialize the object within a frame.
If it's a custom UIView with custom drawing, then look at:
- (void)drawRect:(NSRect)frame
That will get called when setNeedsDisplay is called and/or the view is resized.
With some custom views, one thing I've done in initWithFrame is to set the autoresize masks:
// subviews autoresize
[self setAutoresizesSubviews:YES];
// set resize masks (springs)
[self setAutoresizingMask:(NSViewMaxXMargin | NSViewMinXMargin |
NSViewMaxYMargin | NSViewMinYMargin |
NSViewHeightSizable | NSViewWidthSizable)];
Then in both init and drawRect, I call my layoutViews metod if the rect has changed:
- (void)drawRect:(NSRect)frame {
...
// called on resize. on init, delegate isn't hooked up so we'll skip
if (!NSEqualRects(_prevRect, [self bounds]))
{
[self layoutViews];
}
// stash away rect for comparison so layout view code only happens when the rect changes
_prevRect = [self bounds];
My layoutViews has custom layout logic.
Hope that helps.
Upvotes: 2