-->

Why are my UIView layer properties not being set w

2020-08-23 01:29发布

问题:

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?

回答1:

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

 -(void)awakeFromNib {
    ......
 }