loading custom slider from NIB vs from code: no subviews present when loaded from code
I am trying to create a custom UISlider. I need to insert subviews (like a label) to self. I'm using the
[self insertSubview:_label1 atIndex:2];
method to do so. Everything works fine when I create my slider in IB and assign my PWSlider class to it.
However, if I try to create my PWSlider from code, t开发者_开发技巧here are no subviews present when I do my inits (adding my subviews).
For the init in code I override initWithFrame
, for the NIB-case I tried both, awakeFromNib as well as initWithCoder - same result.
When I debug with
NSLog(@"subview count: %d", [self.subviews count]);
the result is that loaded from NIB I get 3 subviews, loaded programmatically I get 0.
Any ideas?
I assume your PWSlider is inherited from UISlider and also assume you have code which is something like:
-(id)initWithFrame:(CGRect)r {
self = [super initWithFrame:r];
if(self) {
// add your subviews here, starting from index 0
}
return self;
}
Did you debug and check that your initWithFrame gets called?
Also, if you don't care at which locations your subviews are, use addSubview instead of insertSubview:atIndex:.
精彩评论