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]];
/
}