简体   繁体   English

添加UIView subView不起作用

[英]Adding UIView subView not working

I have a UIViewController class MyClass that initially had no XIB, and was initialized programmatically. 我有一个UIViewControllerMyClass ,最初没有XIB,并且已通过编程进行了初始化。 Now, I don't want to load it from an XIB, but I do want to make a small 50x50 UIView (settings overlay view), and I want to add it MyClass , so instead of programatically declaring the new settings overlay view, I thought I would create an XIB file, set the file's owner to MyClass, declare in MyClass.h an IBOutlet UIView *settingsOverlay , and link it in the XIB. 现在,我不想从XIB加载它,但是我想制作一个小的50x50 UIView(设置覆盖视图),我想将其添加为MyClass ,所以我不是以编程方式声明新的设置覆盖视图,而是以为我会创建一个XIB文件,将文件的所有者设置为MyClass,在MyClass.h声明一个IBOutlet UIView *settingsOverlay ,并将其链接到XIB中。

Then in the viewDidLoad method, I do [self.view addSubview:settingsOverlay] , but for some annoying reason it doesn't work. 然后在viewDidLoad方法中,我执行[self.view addSubview:settingsOverlay] ,但是由于某些令人讨厌的原因,它不起作用。 It just doesn't appear. 它只是没有出现。 I tried creating a simple UIImageView programmatically and adding it to the subView, and it works just fine, but when done through the XIB, it doesn't work. 我尝试以编程方式创建一个简单的UIImageView并将其添加到subView中,并且效果很好,但是通过XIB完成后,它不起作用。 Can anyone point out what could possible be wrong? 谁能指出可能是错的吗? Please let me know what other details I might need to include. 请让我知道我可能需要包括哪些其他细节。

If you are trying to add a view using xib then you need to use loadNibNamed method. 如果尝试使用xib添加视图,则需要使用loadNibNamed方法。

[[NSBundle mainBundle] loadNibNamed:@"settingsOverlay" owner:self options:nil];

You can refer developer link for more info - http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/NSBundle_UIKitAdditions/Introduction/Introduction.html 您可以参考开发人员链接以获取更多信息-http: //developer.apple.com/library/IOs/#documentation/UIKit/Reference/NSBundle_UIKitAdditions/Introduction/Introduction.html

Creating an XIB for the settings overlay view and setting it's owner to MyClass does not implicitly cause that XIB to be loaded as a result of manually instantiating MyClass. 为设置覆盖视图创建XIB并将其所有者设置为MyClass不会隐式导致手动实例化MyClass导致加载XIB。 You would have to manually load the settings overlay view XIB in MyClass viewDidLoad and add it as a subview. 您将必须在MyClass viewDidLoad中手动加载设置覆盖视图XIB并将其添加为子视图。 Loading it and passing owner as self will cause it to be bound to the IBOutlet you created, but you still have to add it as a subview. 加载它并作为所有者传递所有者将导致它绑定到您创建的IBOutlet,但是您仍然必须将其添加为子视图。

The code in viewDidLoad would look like this: viewDidLoad中的代码如下所示:

[[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self];
[self.view addSubview:self.overlayView];

You need to load the MyClass xib before settingsOverlay will be set. 您需要在设置settingsOverlay之前加载MyClass xib。 Try adding this line before your addSubview call. 尝试在addSubview调用之前添加此行。

[[NSBundle mainBundle] loadNibNamed:@"MyClass" owner:self options:nil];

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

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