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

有些话、适合烂在心里 提交于 2019-12-22 03:58:57

问题


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 {
    ......
 }


来源:https://stackoverflow.com/questions/5171448/why-are-my-uiview-layer-properties-not-being-set-when-called-within-initwithcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!