• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newbie queries re window sizing etc.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie queries re window sizing etc.


  • Subject: Re: Newbie queries re window sizing etc.
  • From: "Dennis C. De Mars" <email@hidden>
  • Date: Mon, 21 May 2001 11:13:28 -0700

on 5/21/01 10:04 AM, Tim Monroe at email@hidden wrote:

> (2) How can I most easily adjust a superview's size based on the size of
> a subview? In particular, how can I adjust the size of the document
> window based on the size of the image or movie that I load into it? I
> know the size of the image once I've opened it, and I know the relation
> of the image view to the document window (e.g. the number of pixels the
> image view is inset from the document window). But I'm not sure I want to
> hard-code that info into the .m file. Any suggestions? (FWIW, the
> NSImageView is inside of an NSBox, but I don't think that matters here.)

I had to deal with this recently. In particular, I wanted to set the maximum
size of a window dynamically to accommodate the size of a view whose size
could only be known at runtime.

The NSWindow class has a method called setMaxSize which does this, but it
actually sets the maximum frame size, where the frame includes the title
bar, etc. What I wanted was to set the maximum size of the content. Of
course, they are related, but I didn't want to have to calculate the frame
size myself.

There is another method, frameRectForContentRect:styleMask:, that does this
calculation for you. It is a class method, not an instance method.

Putting these things together, I defined a new method for NSWindow which I
added via a cateogory:

@implementation NSWindow (LayoutCategory)

- (void) setMaxContentSize: (NSSize) contentSize
{
NSRect contentRect = { {0.0, 0.0}, {0.0, 0.0} }, frameRect;

contentRect.size = contentSize;
frameRect = [NSWindow frameRectForContentRect:contentRect
styleMask:[self styleMask]];

[self setMaxSize: frameRect.size];
}

@end

In your case, you can do the calculation to see how big your window contents
need to be and call this "setMaxContentSize" method. Since I would think
this would be a very common thing to want to do, I am surprised there isn't
a simpler way to do it (or maybe there is and I missed it).

The only drawback I can think of to the above is that it might not be
guaranteed to to work with any possible subclass of NSWindow because a
subclass might override "frameRectForContentRect:styleMask:" to provide a
different answer than NSWindow would. In order to make this bulletproof, I
guess you should extract the actual class from "self" and send the message
to that.

This only sets the maximum size; it doesn't necessarily change the current
size. To do this you can use the NSWindow method "setContentSize:" directly.
If you are going to do that anyway, you can set the maximum size by calling
setContentSize:, getting the frame size, and then setting calling setMaxSize
with that. In that case, you don't have to go through the rigmarole in code
listed above. Also, I guess if your window isn't resizable you don't have to
worry about setting the max size either...

I'm a "newbie" at Cocoa myself, so I'd be interested in comments on the
issue from the more experienced members of the list

- Dennis D.


  • Prev by Date: Wrapping text in an NSTextView
  • Next by Date: NSTabView question
  • Previous by thread: Re: NSRunLoop processing order of NSPort delegates
  • Next by thread: NSTabView question
  • Index(es):
    • Date
    • Thread