0

Sounds easy enough but I can't seem to figure this one out or find any help for it.

This is the hierarchy I have at the moment:

Window
    Custom View (I want this to resize, with its children, to fit Window)
        Custom View (Contains buttons)
        NSBox (gets content set to the WebView below)
WebView

On applicationLoad, I invoke setContentView on the 'NSBox' to the WebView.

So I end up with an NSBox with a WebView inside it, within a custom view.

If I have an IBAction linked to a button, what do I need to do to get the outer Custom View to resize to fit the Window? Hopefully this would also resize the contents, including the NSBox and the WebView.

Is this possible?

Any help greatly appreciated.

Thanks in advance everyone!

EDIT: ATM i have the following code which makes one WebView larger and makes the other hidden, but it positions the view too high up in the window?

oldView.hidden = YES;
[newView setFrameSize:_window.frame.size];
Cristian
  • 6,765
  • 7
  • 43
  • 64
  • Are you using Cocoa Auto-Layout or old-style autoresize masks? In either case it's certainly possible. You either have to set up constraints to keep the edges of box tied to the edges of the containing view or set the autoresize mask to do that (on the Size inspector). Setting constraints should just entail dragging things to the snap-to guides. – Ken Thomases Apr 28 '12 at 13:44
  • @Ken Thomases I understand what you're saying, maybe I didn't explain myself well. I have 2 WebViews in my application and I need to make one or the other fullscreen on click from their original smaller size. Can this be done? Thanks for your answer – Cristian Apr 28 '12 at 13:56
  • @KenThomases I've updated my original post with code that nearly works, any ideas? – Cristian Apr 28 '12 at 14:13
  • Well, the window's frame encompasses more than just the box, so its size is bigger than the box's content view. Why not just set the new view's size to the old view's size? – Ken Thomases Apr 28 '12 at 14:15
  • @KenThomases I have 2 WebViews which are different sizes, on click I want to take 1 WebView and fill the Window with it, and visa versa with the other, I must be missing something – Cristian Apr 28 '12 at 14:20
  • First, the window's frame includes its title bar, so that's bigger than you want. Second, you don't want to fill the window with it because you have a view with buttons and an `NSBox` (possibly with a title and border). You want the web view to fill the box, right? And the old view is already filling the box, so it has the desired size. So set the new view's size to match the old view's. – Ken Thomases Apr 28 '12 at 15:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10644/discussion-between-cristian-and-ken-thomases) – Cristian Apr 28 '12 at 16:00

1 Answers1

0

Unless your old view was positioned at {0,0} in the coordinates of the window's contentView, it's going to be too high when you change its size to that of the window. Try this:

[newView setFrame:[_window.contentView frame]];

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thanks for your answer, this nearly works. I also have to resize the newView's container right? If i do this then the content still gets cut off. Any ideas? Thanks again this nearly had it – Cristian Apr 28 '12 at 16:04
  • You should be able to do this in IB, as @KenThomases was talking about in his comments. You need to set the second view to expand with the first, and the box to expand with the second view. – rdelmar Apr 28 '12 at 17:22
  • I have done this, but then programmatically how can I tell the view to resize to fit the window? I mean hide one WebView and fit the window without resize event – Cristian Apr 28 '12 at 17:26
  • Which view are you talking about? The code I posted should do that for the outer view. What is the purpose of the inner view anyway? – rdelmar Apr 28 '12 at 18:11
  • The code you posted works very well, but within that view there is a view and a webview, but with your code it seems only the webview is being resized. Any ideas? – Cristian Apr 28 '12 at 19:05
  • I've got it working by resizing its container the same way, could you show me how I would also go back to before? So I would save the old view and then set it to that? This is so that I can then exit the fullscreen. Thanks so much for your help your code is brilliant! – Cristian Apr 28 '12 at 19:38
  • You would just have to save the original frame to a property when your superview first loads (in the init or awakeFromNib methods, depending on whether you're making it in code or in IB) and the set it back to that with code similar to that above. NSRect OriginalFrame = [newView frame]; and then when you want to make it smaller again [newView setFrame:originalFrame]; – rdelmar Apr 28 '12 at 20:55
  • Thanks for that, I have one problem left, when the original sizes are saved, if the window is resized, the variables are wrong. How can i fix this? – Cristian Apr 28 '12 at 21:04
  • It's hard to tell you any more since I don't know exactly what you want or what the structure of your app is. There is a window delegate method, windowDidResize, that's called when you resize your window -- you could add code in there to redefine your originalFrame to be appropriate for your new window size. – rdelmar Apr 29 '12 at 00:06