我有一个UIButton
子类,其中包含UITextView
和两个UILabels
我创建它们执行以下操作:
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];
问题在于最后一个 postText
使用了子类 frame
的UIButton
,我想使用该按钮,将此postText
添加到subviews
的底部,因为如果它位于顶部,我将无法点按该按钮。
我尝试使用
[self bringSubviewToFront:self];
和
[self sendSubviewToBack:postText];
并且
[self insertSubview:postText aboveSubview:authorName];
最后
[self insertSubview:postText atIndex:0];
但是他们都没有因某种原因而工作。
答案 0 :(得分:0)
问题的症结在于UIButton
是此处的父视图,您添加的每个子视图都是在 UIView
之上,即UIButton
}。所以像:
[self bringSubviewToFront:self];
当然无效,因为self
不是subview
的{{1}}
你在这里应该做的事情不是为此继承self
。您应该通过将UIButton
视图放在UIView
的所有其他子视图之上来制作符合您需要的自定义MyFancyButtonThing
类UIButton
。