ios - 使用 NIB 加载 View 的 UIView.addSubview 只会添加根 UIView

标签 ios uiview addsubview loadnibnamed

我在 XIB 文件中外部化了一个组件,因此我可以在多个不同的 View 中重用它。 现在在我的 View Controller 中我加载了基于 XIB 的 UIView 并通过以下方式将其添加到当前 View :

NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"ThumbsPreviewView" owner:self options:nil];
_likePreview = [views objectAtIndex:0];
[self.view addSubview:_likePreview];

起初,我认为它不起作用……但实际上它起作用了,只是将 Root View (即我所有图形组件的容器,它是透明的)放到我的 View 中。

所以我需要放置这种代码(针对每个子组件)以使其工作:

[self.view addSubview:_likePreview];
NSArray *subviews = _likePreview.subviews;
for(UIView * subview in subviews) {
    [self.view addSubview:subview];
}

这是非常违反直觉的,而且我认为它不会创建我想要的 View 层次结构。

我确信有一个解决方案,因为我在 UITableViewController 中做了同样的事情来从 XIB 加载自定义表头。在这种情况下使用完全相同的 XIB 文件可以正确加载所有内容。这是代码:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    switch(section) {
        case 0: {
            NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"ThumbsPreviewView" owner:self options:nil];
            _likePreview = [views objectAtIndex:0];
            return _likePreview;
        }
    }
    return [super tableView:tableView viewForHeaderInSection:section];
}

使用上面的代码,一切都很好,我得到了整个 View (= Root View 容器及其所有内容)作为我的表部分的标题。

所以问题是:我在第一个示例中做错了什么?如何将所有内容(容器 + 所有内容)添加到我的父 UIView 作为适当的 UIView 层次结构?

编辑:我需要准确地说,我的 XIB 文件中的所有内容都组织在单个根 UIView 中,如下所示(我无法发布图片):

* Thumbs preview view
|_ Button
|_ Image View
|_ Image View
|_ Image View
|_ Image View
|_ Button
|_ Button
|_ Button
|_ Label

非常感谢您的帮助!

克里斯托夫

最佳答案

加载组件的一种方法是将组件存储到顶部的 UIView 对象中(见附件截图)。


(来源:mobypicture.com)

然后您可以像往常一样加载您的 nib 并使用 -addSubview: 将您的组件添加到 View 层次结构中。

编辑:

加载 Nib

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"testView" owner:self options:nil] objectAtIndex:0];

并将 View 添加到 View 层次结构中

[self.view addSubview:view];

导致以下 View 层次结构:

(lldb) po [[self view] recursiveDescription]
(id) $1 = 0x088487b0 <UIView: 0x89595b0; frame = (0 0; 320 548); autoresize = W+H; layer = <CALayer: 0x8958b30>>
   | <UIView: 0x89755c0; frame = (0 0; 320 200); autoresize = RM+BM; layer = <CALayer: 0x8975080>>
   |    | <UIView: 0x8975620; frame = (200 20; 100 30); autoresize = W+H; layer = <CALayer: 0x8975680>>
   |    | <UIView: 0x8975700; frame = (20 20; 100 30); autoresize = W+H; layer = <CALayer: 0x8974ea0>>
   |    | <UIRoundedRectButton: 0x8975790; frame = (20 58; 73 44); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x8975890>>
   |    |    | <UIGroupTableViewCellBackground: 0x8976030; frame = (0 0; 73 44); userInteractionEnabled = NO; layer = <CALayer: 0x8976100>>
   |    |    | <UIImageView: 0x89768d0; frame = (1 1; 71 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x89770f0>>
   |    |    | <UIButtonLabel: 0x8977af0; frame = (36 22; 0 0); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8977be0>>
   |    | <UIRoundedRectButton: 0x8978990; frame = (227 58; 73 44); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x8978a60>>
   |    |    | <UIGroupTableViewCellBackground: 0x8978a90; frame = (0 0; 73 44); userInteractionEnabled = NO; layer = <CALayer: 0x8978b10>>
   |    |    | <UIImageView: 0x8978b80; frame = (1 1; 71 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8979230>>
   |    |    | <UIButtonLabel: 0x8978be0; frame = (36 22; 0 0); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8978cd0>>

关于ios - 使用 NIB 加载 View 的 UIView.addSubview 只会添加根 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12911488/

相关文章:

ios - 如何将透视变换应用于 UIView?

ios - 创建全局 UIView

class - 将 UIView 作为 SubView 添加到自定义类,UIView 的子类,Swift

ios - 在 Parse 中存储 GPS 位置数据

ios - 如何从存储在 Parse 列中的数组中删除对象

ios - 为什么我的 UIButton 没有显示在我的 subview 中? iPad应用程式

ios - 很难将 UIActivityIndi​​cator 放入 UIButton

iphone - 如何在其父 View 之上添加 subview 中的 View ?

ios - iOS中如何通过CMMotionManager获取实际加速度

ios - 如何让Master VC出现在UISplitViewController的右边?