在哪里确定UIView的大小 [英] Where to determine UIView size

查看:129
本文介绍了在哪里确定UIView的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结:在初始化该视图时, UIViewController 应该如何知道其UIView实例的大小?

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

UIView的专用初始化方法是 initWithFrame:(CGRect)frame 方法。这将为新创建的UIView设置框架。如果请求了视图控制器的视图,则可以从 UIViewController的 loadView方法调用此方法。关于子类化和视图大小的 UIViewController 的文档说明:

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.

所以, UIViewController 实例应该设置这些属性。到目前为止一直很好, UIViewController 到目前为止还不知道它的视图有多大。

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.

当请求 UIViewController 的视图且视图属性为nil时,将调用视图控制器的loadView方法。现在有一个问题,因为需要初始化 UIView ,但视图控制器仍然不知道视图的大小。你应该初始化那个视图有多大?您在代码中确定视图大小的位置?

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?

您可以使用零rect( CGRectZero )初始化视图:

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);

这会从视图控制器(viewController.view)请求视图,从而加载其视图loadView方法。此loadView方法使用 CGRectZero 初始化视图。然后调用者设置它的框架(view.frame = ...)

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 = ...)

事实是视图上的框架属性设置了两次,可能导致更多的双重工作如果你的自定义 UIView 在setFrame方法中进行一些高级布局(例如放置和调整子视图的大小)。您可以通过为 UIViewController 创建一个专用的初始化方法来阻止这种情况,该方法已经向调用者询问了一个CGRect,它将存储在一个ivar中。在调用loadView方法时,使用此ivar创建视图。

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.

这里有什么好的方法?将视图的帧设置两次(使用 CGRectZero 进行初始化,然后进行设置),或者给予 UIViewController 一个新的初始化方法使用CGRect(从而赋予它一个框架属性)?或者我错过了什么,还有其他可能性吗?

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?

推荐答案

你不必须使用指定的初始化程序。只需在 [[UIView alloc] init] 中使用init。指定的初始化程序必须在子类的初始值设定项中使用。

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.

另一方面,设置两次帧不应该造成太大的伤害。在 setFrame:中执行大量任务是不寻常的。 Layouting通常在 layoutSubviews 中完成,并且只执行一次。

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.

这篇关于在哪里确定UIView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆