Subview doesn't get to the bottom
I have a UIButton subclass which contains a UITextView and two UILabels I create them doing the following:
UILabel *authorName = [[UILabel alloc] initWithFrame: CGRectMake(0, viewHeight, viewWidth, 16)]; authorName.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5]; authorName.textColor = [UIColor whiteColor]; authorName.font = [UIFont systemFontOfSize:13]; authorName.text = [NSString stringWithFormat:@"@%@",[data objectForKey:@"username"]]; [self addSubview:authorName]; UILabel *likes = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth-50, viewHeight-20, 50, 20)]; likes.backgroundColor = authorName.backgroundColor; likes.textColor = authorName.textColor; likes.textAlignment = NSTextAlignmentCenter; likes.font = authorName.font; likes.text = [NSString stringWithFormat:@"Likes: %@", [data objectForKey:@"likes"]]; [self addSubview:likes]; UITextView *postText = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewHeight)]; postText.editable = NO; postText.text = [NSString stringWithFormat:@"%@",[data objectForKey:@"text"]]; postText.font = [UIFont systemFontOfSize:16]; postText.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.6];
The problem is with the last one postText which uses the frame of the subclass UIButton, I want to use that button, by adding this postText to the bottom of subviews, because if it's on the top I will not be able to tap on the button.
I tried to use
[self insertSubview:postText aboveSubview:authorName];
[self insertSubview:postText atIndex:0];
But none of them work for some reason.
The crux of the problem is that the UIButton is the parent view here, every subview you add is on top of the UIView that is the UIButton. So things like:
of course won't work because self isn't a subview of self
Really what you should be doing here is not subclassing UIButton for this. You should make a custom UIView class MyFancyButtonThing that does what you want by putting the UIButton view on top of all the other subviews of MyFancyButtonThing.