In Cocoa, is there a way to programmatically resize a view (in this case a window) to fit all of it's subviews?
Think tic-tac-toe. You are creating the buttons in rows and columns and you want to set the width and height of the parent window (actually, the size of the window's contentView, right?). But maybe you want to sometimes use a 4x4 grid or 5x5, so you can't use a default window setting.
I iterate and do math to determine the positioning of the buttons. (There is the space padding the inside of the window and a separate value for padding between buttons, so it's not completely straight-forward.) I think it'd be cleaner not to have to do that math in开发者_如何学Python two places to set the window size, if I could avoid it.
I suppose I could set the window size correctly each time I add a button, but that's kind of silly. Or maybe it's not, since it's not going to draw it until I'm done?
edit: I just don't want to have two pieces of different math and have to make sure they're drawing the same conclusion, you know?
In the UIKit, UIView has a -sizeToFit method. In the AppKit, several view subclasses such as NSBox and NSText also implement -sizeToFit. You could just use an NSBox with no border.
精彩评论