Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to determine UIView size

Summary: How should the UIViewController know the size of its UIView instance when initialising that view?

The dedicated initialisation method for an UIView is the initWithFrame:(CGRect)frame method. This sets the frame for the newly created UIView. This method could be called from the UIViewController's loadView method, if that view controller's view is requested. The documentation of UIViewController states with respect to subclassing and view size:

When creating the views for your view hierarchy, you should always set the autoresizing properties of your views. When a view controller is displayed on screen, its root view is typically resized to fit the available space, which can vary depending on the window’s current orientation and the presence of other interface elements such as the status bar.

So, the UIViewController instance should set those properties. So far so good, the UIViewController so far does not have to know how big its view is or will be.

When the view of a UIViewController is requested and the view property is nil, the loadView method of the view controller is called. Now there is a problem, because the UIView needs to be initialised, but the view controller still does not know what size the view should be. How big should you initialize that view? Where in code do you determine the view size?

You could initialize the view with a zero rect (CGRectZero):

- (void)loadView {     self.view = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; } 

And let the caller set the view frame like so:

UIViewController *viewController = [[MyUIViewController alloc] init]; // next two lines are normally combined, but for clarity they are not now UIView *view = viewController.view; view.frame = CGRectMake(0, 0, 200, 200); 

This requests the view from the view controller (viewController.view), and thus loads its view with the loadView method. This loadView method initializes the view with a CGRectZero. Then the caller sets its frame (view.frame = ...)

The thing is that the frame property on the view is set twice, possibly causing even more double work if your custom UIView is doing some advanced layout in the setFrame method (placing and resizing subviews for example). You could prevent this by creating a dedicated initializer method for the UIViewController which already asks the caller for a CGRect, which you would store in an ivar. At the time the loadView method is called, you use this ivar to create the view.

What is the good way to go here? Either setting the view's frame twice (initializing with CGRectZero, and setting afterwards), or giving the UIViewController a new initializer method with a CGRect (and thus giving it a frame property)? Or am I missing something and are there other possibilities?

like image 716
drvdijk Avatar asked Jul 09 '09 10:07

drvdijk


People also ask

What is the difference between UIView and UIViewController?

They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.

What is intrinsic size in swift?

Most views have an intrinsic content size, which refers to the amount of space the view needs for its content to appear in an ideal state. For example, the intrinsic content size of a UILabel will be the size of the text it contains using whatever font you have configured it to use.

What is sizeToFit?

sizeToFit()Resizes and moves the receiver view so it just encloses its subviews.


1 Answers

You don't have to use the designated initializer. Just use init as in [[UIView alloc] init]. The designated initializer has to be used from subclasses' initializers.

On the other hand, setting the frame twice should not do much harm. Performing a lot of tasks in setFrame: is unusual. Layouting is normally done in layoutSubviews and is only performed once.

like image 182
Nikolai Ruhe Avatar answered Oct 05 '22 06:10

Nikolai Ruhe