简体   繁体   English

在interfacebuilder中将子视图添加到自定义uiview

[英]add subview to custom uiview in interfacebuilder

I have made a custom UIView composed of several subviews and added it to a Nib file. 我已经制作了一个由几个子视图组成的custom UIView ,并将其添加到Nib文件中。 The view is loading correctly, but my problem arises when I try to add a UIView to my custom UIView from InterfaceBuilder. 视图正确加载,但是当我尝试从InterfaceBuilder将UIView添加到custom UIView时出现问题。 I would like the added subviews to go to the topmost subview of my custom UIView , but not surprisingly, the subviews are added to the root custom UIView . 我希望添加的子视图进入自定义UIView的最顶层子视图,但是毫不奇怪,这些子视图被添加到根custom UIView

I could do some magic in the init of the custom UIView , moving the added view around. 我可以在custom UIViewinit中做一些魔术,移动添加的视图。

is there any alternatives? 还有其他选择吗?

Any thought on best practice to solve this? 是否有解决此问题的最佳做法的想法?

That seems to be right, since when you add the customView, int the nib file, you dont have access to its child subView, that means adding views to this custom view, will add them to the root view and not the childs, 这似乎是正确的,因为当您将int nib文件添加到customView时,您无权访问其子subView,这意味着向该自定义视图添加视图将把它们添加到根视图,而不是子视图,

As you said you will have to do some workaround to make this work, such as using tags to define the views and then move them by code 如您所说,您将必须采取一些变通办法来完成这项工作,例如使用标签定义视图,然后通过代码移动它们

You need to add your Custom UIView to subview of your Root view and all other subviews of root UIView as a subview of your Custom View .So that you can get what exactly you want. 您需要将“ Custom UIView添加到subview of your Root view并将根UIView的所有其他子subview of your Custom Viewsubview of your Custom View 。这样您才能获得所需的内容。 You IB hierarchy should be like below... 您的IB层次结构应如下所示...

 --->Root UIView
      --->Custom UIView
           --->UItextField
           --->UIlabel
           --->UiimageView

If you adding your Custom UIView in a code , then you need to connect IBoutlet for all subviews and then make them all as a subview of your custom view . 如果Custom UIView in a code添加Custom UIView in a code ,则需要为所有子视图连接IBoutlet ,然后将它们全部作为subview of your custom viewsubview of your custom view

There doesn't seem to be any clever solution. 似乎没有任何聪明的解决方案。 Instead i ended up overriding layoutSubviews like this: 相反,我最终像这样覆盖了layoutSubviews

-(void)layoutSubviews
{    
    [super layoutSubviews];
    for (UIView *subview in self.subviews){
        if (![self.internalViews containsObject:subview]) {
            [subview removeFromSuperview];
            CGRect f =subview.frame;
            f.origin.y += self.frame.origin.y;
            subview.frame = f;
            [self.contentContainer addSubview:subview];

        }
    }
}

Doing construction of the view i added all by subviews to the array internalViews . 在构造视图时,我将所有子视图都添加到了数组internalViews The contentContainer is the view intended to serve as parent for all InterfaceBuilder added view. contentContainer是旨在用作所有InterfaceBuilder添加的视图的父视图的视图。

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

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