【问题标题】:UIView.addSubview using a NIB-loaded view does only add root UIView使用 NIB 加载视图的 UIView.addSubview 只会添加根 UIView
【发布时间】:2012-10-06 08:43:17
【问题描述】:

我在一个 XIB 文件中外部化了一个组件,以便我可以在几个不同的视图中重用它。 现在在我的视图控制器中,我加载了基于 XIB 的 UIView 并将其添加到当前视图中:

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

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

所以我需要把这种代码(对于每个子组件)让它工作:

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

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

我确信有一个解决方案,因为我在 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];
}

使用上面的代码,一切都很好,我将整个视图(= 包含所有内容的根视图容器)作为我的表格部分的标题。

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

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

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

非常感谢您的帮助!

克里斯托夫

【问题讨论】:

    标签: ios uiview addsubview loadnibnamed


    【解决方案1】:

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


    (来源:mobypicture.com)

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

    编辑:

    加载笔尖

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

    并将视图添加到视图层次结构中

    [self.view addSubview: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>>
    

    【讨论】:

    • 这正是我所拥有的:
    • 请检查我编辑的问题。您可以自己检查,加载这样的 XIB 并通过 [self.view addSubView:loadedNib] 添加它只会加载您的根视图而不会加载它的子视图。
    • 加载NIB没问题,添加到视图时出现问题:[self.view addSubview:_likePreview];仅添加根视图。我需要一个一个地添加每个子视图: NSArray *subviews = _likePreview.subviews; for(UIView * subviews in subviews) { [self.view addSubview:subview]; }
    • 就像我说的,确保你的 UIButtons 和 UIViews 是你的根视图元素的直接兄弟。在[self.view addSubview:_likePreview]; 之后设置断点。然后在调试器中输入po [[self view] recursiveDescription] 并将输出添加到您的问题中。
    • 在 UITableViewController "viewForHeaderInSection" 方法中返回完全相同的视图效果很好(如我的问题中的最后一个代码 sn-p 所示),将完整组件(UIView + 所有子视图)显示为标题一个表部分,所以我确信我的 XIB 是好的,并且每个组件都是我正在加载的根子视图的子视图。我在加载的 UIView 数组中只有 1 个 UIView,即我的根视图,然后包含所有内容作为其子项。同样,调用 addSubView 时它不起作用。
    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 2013-10-08
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多