Displaying UIView content downloaded from xib in a storyboard

Current setting

I have a subclass UIViewthat loads contents from an xib file - names it XibOwnerClass. In addition, among other classes, there is a class called Trianglethat helps me create triangles with or without a frame, with different colors for the stroke or fill, etc. This class can be divided into a storyboard, and some of its properties are defined as IBInspectable.

Currently, in my file, xibI use this kind of triangle and set its verifiable properties with IB. And it’s really cool and comfortable ... Therefore, if I look xib, I will really see the appearance of the triangle among other views.

So let's move on. To use this XibOwnerClass, I drag the item UIViewinto the storyboard and change my custom class to XibOwnerClass, so I get my characteristic properties for XibOwnerClass. So, I can configure everything in IB, and when I launch the application, everything works.

Problem

Even if this works, I wonder if there is a way for multiple views (classes XibOwnerClass) to be dragged into the storyboard and so that each of them can customize each one through the Builder interface?

, UIView XibOwnerClass, . , , + . , xib . ?

, xib ( ), , - xib, , , . xib, , ?

xib:

-(instancetype)initWithCoder:(NSCoder *)aDecoder{

    if ((self = [super initWithCoder:aDecoder])){

        UIView *myView = [[[NSBundle mainBundle] loadNibNamed:@"MyXib" owner:self options:nil] firstObject];

        if (myView){

            myView.frame = CGRectMake(myView.frame.origin.x, myView.frame.origin.y, self.frame.size.width, self.frame.size.height);
            [self addSubview:myView];

            //Initialize other stuff

            [self commonInit];
        }
    }

    return self;
}

awakeFromNib: ...

EDIT:

initWithFrame:, , , nib ( ). , , ?

+4
2

, . , Main Bundle, , .

, :

NSBundle *bundle = [NSBundle bundleForClass:[self class]];

@Mohammadalijf . . , , prepareForInterfaceBuilder, , , .

, , .

0

, , . , prepareForInterfaceBuilder. XibOwnerClass prepareForInterfaceBuilder , , .

override func prepareForInterfaceBuilder() {
        //prepare setups or things to show in storyboard

}
+2

Source: https://habr.com/ru/post/1664433/


All Articles