0
votes

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, there 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?

1
This is my own implementation of my custom UISlider... ;-)pawi

1 Answers

0
votes

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:.