views:

76

answers:

1

(IPhone newbie question)

I've subclassed a UIViewController and a UIView, and created a InitWithFrame for the view. My custom controller uses this to initialize its view, by frame, in its loadView function.

I plan to use the controller and view in my code at different places. At times under a navigation controller, toolbar, both, neither etc. This means that the code creating the view in loadView of the controller, should ideally know in what size to create the view.

How do I avoid hard coding these sizes for the different cases? Is there a way to build the surrounding controllers (navigation bar, etc.), then figure out the size of the viewable frame, and somehow only then create my controller and initialize my custom view using this frame size?

If my design is incorrect, any tip could be of great assistance... Thanks!

A: 

For the initial vew size you might be interested in [UIScreen applicationFrame]. Returned rectangle accounts for the status bar and interface orientation. When your controller is used inside a navigation controller or other wrapper that changes the available screen space, I think the view gets resized automatically (through UIViewController magic), so there’s nothing you have to do (apart from setting a permissive autoresizingMask).

zoul
Thanks zoul. though, I'm not exactly sure what to do. Since the applicationFrame can't take into account the navigation controllers, etc., does that mean that my controller will inevitably draw its view initially in the wrong size? I'd prefer not to count on autoresizing magic. I don't believe in magic :)
DannyA
You have to actually use: `[[UIScreen mainScreen] applicationFrame]` @DannyA: I can see that there would be cases where automatic resizing would be less than ideal
Casebash
It seems like other UI elements know the bounds of what they are in. How does this happen? A ScrollView for example, scrolls the inner view exactly to where the visible area ends, as if it "knows" whether it's inside a tabbar or not. How does it know? [If this is known to be impossible for me to figure out, let me know and I'll approve the answer]
DannyA
Views only know their geometry inside the super view (`frame`). They do not care about much else, for if they knew about the tab bar and generally about the context around them, a great mess would ensue. As for the scrolling view, that clips the content view using its own geometry. And its own geometry gets changed by somebody higher up the food chain (like the controller) when a tab bar comes on the screen.
zoul
That is exactly what I want! I want my super view (i.e. navigation controller, which is inside a tab controller) to know what size it has available to set the frame of its subview, without having to hardcode sizes or check its own superview. Somehow, when I put a scrollview, straight forward, in code, it simply "knows" its correct size without me explicitly setting its frame from its super view. Is that just behind-the-scenes resizing behavior magic?
DannyA
Set a breakpoint on the `setFrame` method in your view and you’ll see who and in what context calls it. Usually it’s some internal part of `UIViewController`. As long as you stick to ordinary scenarios (standalone controller, controller inside navigation controller, …), it should work according to the autosizing masks without you taking special care.
zoul
Good idea! will to that. I guess this turned out to be a more complicated question than I hoped. Thank you very much.
DannyA