简体   繁体   English

UIView 从 xib 作为子视图加载:如何正确设置框架/大小?

[英]UIView loaded from xib as subview : How to set the frame/size properly?

I have a small problem with my iphone app (I am quite new to iphone programmation even if I had already done objective-c before).我的 iphone 应用程序有一个小问题(即使我之前已经完成了 objective-c,我对 iphone 编程还是很陌生)。

I have an UIView named centerView in my main window.我的主 window 中有一个名为centerView的 UIView。 I have created a singleton subclass of UIViewController (let's call it FooViewController ) linked with a xib file.我创建了一个与 xib 文件链接的 UIViewController 的 singleton 子类(我们称之为FooViewController )。 My goal is to load the associated view as a subview (of centerView using the whole frame of centerView).我的目标是将关联视图加载为子视图(使用 centerView 的整个框架的 centerView)。

Problem: The view controlled by FooViewController does not use the frame of its superview.问题:FooViewController 控制的视图不使用其父视图的框架。

I have tried to add:我试图添加:

  • self.view.frame = self.view.superview.frame in the "custom initialisation" part of the initWithNib... method of FooViewController self.view.frame = self.view.superview.frame self.view.superview.frame initWithNib的“自定义初始化”部分... FooViewController的方法

  • [[[FooViewController sharedController] view] setFrame:centerView.frame] when I add the view to centerview ( [centerView addSubView:[[FooViewController sharedController] view]] ) (I have tried before and after this line) [[[FooViewController sharedController] view] setFrame:centerView.frame]当我将视图添加到 centerview 时( [centerView addSubView:[[FooViewController sharedController] view]] )(我在这条线之前和之后都试过)

But I can't get the good behavior (ie the good size).但我无法获得良好的行为(即良好的尺寸)。 There are multiple questions related to this kind of problems but I was unable to find my answer in them so:有多个与此类问题相关的问题,但我无法在其中找到答案,因此:

  • Do you know how I can solve that?你知道我该如何解决吗?
  • [optional] How did the instanciation of a UIViewController linked with a xib works? [可选] 与 xib 链接的 UIViewController 的实例化如何工作? There must be a resource file where the name of the xib is indicated?必须有一个资源文件,其中指明了 xib 的名称? Or maybe does it looks for a xib with the same name as the class?或者它是否在寻找与 class 同名的 xib?

Thanks in advance for your help.在此先感谢您的帮助。

When loading from a NIB, a view is loaded in the view controller's loadView method, not in the initializer.从 NIB 加载时,视图会在视图控制器的loadView方法中加载,而不是在初始化程序中。 Since you shouldn't really override loadView , esp.由于您不应该真正覆盖loadView ,尤其是。 if you are loading it from a NIB, you should try doing #1 ( self.view.frame = self.view.superview.frame ) in the view controller's viewDidLoad method.如果您从 NIB 加载它,您应该尝试在视图控制器的viewDidLoad方法中执行 #1 ( self.view.frame = self.view.superview.frame )。 Of course, you should ensure that the view is added to a superview by the time you execute this statement.当然,您应该确保在执行此语句时将视图添加到超级视图中。 You also need to make sure that the superview's frame has been correctly set by this time.您还需要确保此时已正确设置超级视图的框架。

Another place where you can try this is in the viewWillAppear: method.另一个可以尝试的地方是viewWillAppear:方法。 Most probably your view's super view will have been set up properly by then.到那时,您的视图的超级视图很可能已经正确设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM