Programmatically adding XIB

Hello my fellow compatriots,


I've been learning to use XIB's as of late. As of right now, my XIB is not being programmatically added and all frames and bounds are 0,0.

Any assistance would be awesome.


XIB h file

@interface builder : UIView
@property (strong) IBOutlet UIImageView *theImage;
/
@property (nonatomic) NSInteger locX;
@property (nonatomic) NSInteger locY;

@property (strong) IBOutlet UIView *view;
@end




XIB m file


#import "builder.h"

@end
@implementation builder
@synthesize locX;
@synthesize locY;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


    UITouch *touch = [touches anyObject];
    UITouch *touched = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: touched.view];

    locX = location.x;
    locY = location.y;

    NSLog(@"%ld and %ld", (long)locX, (long)locY);

    /

}
- (id) initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
     
     
        /
        [[NSBundle mainBundle] loadNibNamed:@"builder" owner:self options:nil];
     
        self.bounds = self.view.bounds;

        /
        NSLog(@"%@", NSStringFromCGRect(self.view.bounds));
     
        /
     
        [self addSubview:self.view];
        /
     
    }
    return self;

}
- (id) initWithCoder:(NSCoder *)aDecoder{

    self = [super initWithCoder:aDecoder];

    if (self) {
     
        /
        [[NSBundle mainBundle] loadNibNamed:@"builder" owner:self options:nil];

        /
     
        [self addSubview:self.view];
     
     
    }
    return self; 
}
@end





Implementation m file

This creates a bar across the width of the screen with a UIView (the XIB) taking up covering the first 25%.


//called from viewDidLoad

- (void)setUpViews{
   
   
    holdView.backgroundColor = [UIColor blueColor];
   
    UIView *inside = [[UIView alloc] init];
    [inside setTranslatesAutoresizingMaskIntoConstraints:NO];
    inside.backgroundColor = [UIColor grayColor];
    [inside setTag:i];
    [holdView addSubview:inside];
   
    [self.holdView addConstraint:[NSLayoutConstraint constraintWithItem:inside attribute:NSLayoutAttributeTop
          relatedBy:NSLayoutRelationEqual toItem:self.holdView attribute:NSLayoutAttributeTop multiplier:1.0 constant:30.0]];
   
    [self.holdView addConstraint:[NSLayoutConstraint constraintWithItem:inside attribute:NSLayoutAttributeWidth
          relatedBy:NSLayoutRelationEqual toItem:self.holdView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
   
    [self.holdView addConstraint:[NSLayoutConstraint constraintWithItem:inside attribute:NSLayoutAttributeHeight
          relatedBy:NSLayoutRelationEqual toItem:self.holdView attribute:NSLayoutAttributeHeight multiplier:0.0 constant:localPoint]];
   
    builder *first = [[builder alloc] init];
    [first setTranslatesAutoresizingMaskIntoConstraints:NO];
    first.backgroundColor = [UIColor orangeColor];
    [first setTag:x];
   
    [inside addSubview:first];
   
   
   
    /
    [inside addConstraint:[NSLayoutConstraint
                              constraintWithItem:first
                              attribute:NSLayoutAttributeTop
                              relatedBy:NSLayoutRelationEqual
                              toItem:inside
                              attribute:NSLayoutAttributeTop
                              multiplier:1.0
                              constant:0.0]];
   
    [inside addConstraint:[NSLayoutConstraint
                              constraintWithItem:first
                              attribute:NSLayoutAttributeWidth
                              relatedBy:NSLayoutRelationEqual
                              toItem:inside
                              attribute:NSLayoutAttributeWidth
                              multiplier:0.25
                              constant:0.0]];
   
    [inside addConstraint:[NSLayoutConstraint
                              constraintWithItem:first
                              attribute:NSLayoutAttributeHeight
                              relatedBy:NSLayoutRelationEqual
                              toItem:inside
                              attribute:NSLayoutAttributeHeight
                              multiplier:1.0
                              constant:0.0]];
   
    /
   
   }
   
  

You are calling [[builder alloc] init], not either of your init methods that attempts to load the view from the XIB. I would think if you want it to load the XIB, you would have to call a method that loads the XIB.

  [[NSBundle mainBundle] loadNibNamed:@"builder" owner:self options:nil];


What is the name of the XIB in the Finder? Builder.xib or builder.xib


◅▻

KMT - It is "builder.xib"


Junkpile - I'm looking around at several tutorials and didn't seem to see anything apart from what I have.

I've tried several things such as addSubview with no success.

How would you approach this?

Call one of the methods that has code that loads the XIB, e.g. [[builder alloc] initWithFrame:CGRectZero]


Also I don't think you need that stuff setting your bounds. Your frame and bounds should be determined by the superview's layout and you shouldn't set it in the subview.


(edited to remove some stuff about addSubview)

No change, I've got this:


- (id) initWithFrame:(CGRect)frame{
   
    self = [super initWithFrame:frame];
    if (self) {
       
        /
        [[NSBundle mainBundle] loadNibNamed:@"builder" owner:self options:nil];
       
       
       
        [self addSubview: self.view];
        /
       
        NSLog(@"%@", NSStringFromCGRect(self.view.bounds));
       
    }
    return self;
   
}


This is called, but the bounds are always 0,0 and nothing happens.

Do you have sufficient constraints to specify the horizontal and vertical size and position of all your views? For "first", for example, I don't see anything specifying horizontal position. Or "inside" either for that matter. And I don't see any constraints at all on the view you're attempting to add as a subview of your "builder" UIView. Are there auto layout warnings in the console output?


The only thing the XIB loading does is instantiate subviews and connect up any outlets (assuming you have the File's Owner and outlets set up properly). The positioning and sizing of the view is up to the superview.

Here are the images:

All items involving constraints work as they should.

And here the connections:


(Post Note - the images do not seem to be appearing)

If all items involving constraints work as they should, then you're done. Ship it.


But seriously. What constraint specifies the horizontal position of "inside"?

Programmatically adding XIB
 
 
Q