Subview没有深究

时间:2014-02-11 21:42:37

标签: ios objective-c subview

我有一个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];

但是他们都没有因某种原因而工作。

1 个答案:

答案 0 :(得分:0)

问题的症结在于UIButton是此处的父视图,您添加的每个子视图都是 UIView之上,即UIButton }。所以像:

[self bringSubviewToFront:self];

当然无效,因为self不是subview的{​​{1}}

你在这里应该做的事情不是为此继承self。您应该通过将UIButton视图放在UIView的所有其他子视图之上来制作符合您需要的自定义MyFancyButtonThingUIButton