Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my UIView layer properties not being set when called within initWIthCoder:(NSCoder *)aDecoder?

I have a custom View class built which subclasses UIView.

This view is loaded through a nib file in the ViewController via:

[[NSBundle main] loadNibNamed:@"MyCustomView" owner:self options:nil];

Inside MyCustomView.h (which correctly hooked up in IB as the Main Views class) I have some subview properties linked:

@interface MyCustomView : UIView <UITextFieldDelegate> {
....
@public

UIView *backgroundLayer; // a subview of the Main View inside the nib
....
}

@property (nonatomic, retain) IBOutlet UIView *backgroundLayer;

This outlet is properly connected within Interface Builder.

Inside MyCustomView.m I have the following implementation:

#import <quartzcore/QuartzCore.h>

@implementation MyCustomView

@synthesize backgroundLayer;

- (id)initWithCoder:(NSCoder *)aDecoder {
   self = [super initWithCoder:aDecoder];
   if (self) {
      ....
      self.backgroundLayer.layer.cornerRadius = 12.0f;
      self.backgroundLayer.layer.borderColor = [UIColor lightGrayColor].CGColor;
      self.backgroundLayer.layer.maskToBounds = YES;
      ....
   }

NONE of those backgroundLayer.layer settings are being applied. When I run in simulator the custom view appears exactly how it appears in the NIB without any of those mods? What am I missing? Am I making the calls in the incorrect place?

like image 820
skålfyfan Avatar asked Mar 02 '11 17:03

skålfyfan


1 Answers

The correct answer to this issue was to use the method :

 -(void)awakeFromNib {
    ......
 }
like image 111
skålfyfan Avatar answered Nov 03 '22 07:11

skålfyfan