简体   繁体   中英

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). I set in the -(id)initWithFrame: of this class the background color. I have created a message to add the UIControls in the view in order to put inside of my custom view. 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.

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. 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. 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.

Edit: if remove `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 :(

thanks in advice

Method translatesAutoresizingMaskIntoConstraints = YES means that the UIView is using Auto Layout.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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