简体   繁体   English

添加在XIB UIView内部以编程方式创建的自定义UIView

[英]Add custom UIView programmatically created inside XIB UIView

I've created a UIView programmatically that embeds several UIControl s ( UIButton s, UISwitch s and UILabel s mainly). 我已经以编程方式创建了一个UIView ,它嵌入了几个UIControl (主要是UIButtonUISwitchUILabel )。 I set in the -(id)initWithFrame: of this class the background color. 我在此类的-(id)initWithFrame:中设置背景色。 I have created a message to add the UIControls in the view in order to put inside of my custom view. 我创建了一条消息,将UIControls添加到视图中,以便放入自定义视图中。 It's something like that: 就像这样:

-(void) CreateGuiObjects
{
    UIView *container = self;

    //create uiswitch
    mOnOffSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(10, 20, 0, 0)];
    mOnOffSwitch.translatesAutoresizingMaskIntoConstraints = NO;   //used for constraint testing
    //add parent view (self)
    [container addSubview: mOnOffSwitch];

    /*
    other stuff like above
    */

}

then in my view controller there is an event handler for an external button; 然后在我的视图控制器中,有一个用于外部按钮的事件处理程序; the action is to add the above custom view in an empty UIView created with Storyboard Interface Builder in Xcode. 该操作是将上述自定义视图添加到用Xcode的Storyboard Interface Builder创建的空UIView中。

the code is like the following: 代码如下:

-(void)CreateButton
{
  MyCustomView *view = [MyCustomView alloc] initWithFrame:CGRectMake(20,20,300,200)];
  [self.view addSubview: view];
  //now call my create method
  [view CreateGuiObjects];
}

now, the problem is that it draws the controls, but it seems to position them in a different place...i set the frame origin for the container view in (20,20) and then put the switch in (10,20) where this point is relative to the custom view origin. 现在的问题是它绘制了控件,但似乎将它们放置在其他位置...我在(20,20)中设置了容器视图的框架原点,然后在(10,20)中放置了开关此点相对于自定义视图原点的位置。 Instead of that position the view seem to be far away from that position and the second problem is that the background color (gray) set in the initWithFrame is totally ignored. 而不是该位置的视图似乎远离该位置,第二个问题是initWithFrame中设置的背景颜色(灰色)被完全忽略了。 If i remove every call to addSubview inside the CreateGuiObjects, it draws the empty view with the correct background color and in the correct position. 如果我删除CreateGuiObjects内部对addSubview的所有调用,它将以正确的背景颜色和正确的位置绘制空视图。

Edit: if remove `mOnOffSwitch.translatesAutoresizingMaskIntoConstraints = NO; 编辑:如果删除`mOnOffSwitch.translatesAutoresizingMaskIntoConstraints = NO; it works fine...but if i put again it doesn't work. 它工作正常...但是,如果我再说一遍,它不起作用。 Need to understand deeply the meaning of this property. 需要深入了解此属性的含义。

Could someone can help me? 有人可以帮我吗? i think it is a silly question but i'm new to iOS development :( 我认为这是一个愚蠢的问题,但我是iOS开发的新手:(

thanks in advice 感谢建议

Method translatesAutoresizingMaskIntoConstraints = YES means that the UIView is using Auto Layout. 方法translationsAutoresizingMaskIntoConstraints = YES表示UIView使用自动布局。

The fundamental building block in Auto Layout is the constraint. 自动布局中的基本构建块是约束。 Constraints express rules for the layout of elements in your interface; 约束表达了界面中元素布局的规则; for example, you can create a constraint that specifies an element's width, or its horizontal distance from another element. 例如,您可以创建一个约束,该约束指定一个元素的宽度或与另一个元素的水平距离。 You add and remove constraints, or change the properties of constraints, to affect the layout of your interface. 您添加和删除约束,或更改约束的属性,以影响界面的布局。

When calculating the runtime positions of elements in a user interface, the Auto Layout system considers all constraints at the same time, and sets positions in such a way that best satisfies all of the constraints. 在计算用户界面中元素的运行时位置时,自动版式系统会同时考虑所有约束,并以最能满足所有约束的方式设置位置。

read more about Auto Layout Concepts 阅读有关自动布局概念的更多信息

If you don't know how to use Auto Layout I would recommend you to turn it off. 如果您不知道如何使用自动版式,建议您将其关闭。

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

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