ios:没有显示按钮的子视图

时间:2014-08-12 19:41:56

标签: ios objective-c uibutton uilabel

我的代码如下:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect frame = CGRectMake(50, 125, 220, 40);
button.frame = frame;
button.layer.cornerRadius = 20;
button.backgroundColor = [UIColor  colorWithRed:0.42 green:0.66 blue:0.31 alpha:1.0];
button.layer.masksToBounds = YES;
[self.view addSubview:button];
button.autoresizesSubviews = NO;
button.clipsToBounds = YES;

UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 100, 100)];
addLabel.text = @"+";
addLabel.textColor = [UIColor blackColor];
addLabel.textAlignment = NSTextAlignmentCenter;
addLabel.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:80.0f];
[button addSubview:addLabel];
addLabel.autoresizesSubviews = NO;
addLabel.clipsToBounds = YES;

我的button在iOS / iPhone模拟器中显示正常,但addLabel标签根本没有显示。

我错过了什么?

2 个答案:

答案 0 :(得分:1)

标签上的框架太大。因此,标签文本将被绘制在按钮框架之外。理想情况下,标签的框架最多 与按钮高度一样高。

答案 1 :(得分:0)

UIButton已经有了一个标题标签。您是否尝试过使用它?

button.titleLabel.text = "+";

甚至使用不同的按钮类型:

UIButton* button = [UIButton buttonWithType:UIButtonTypeContactAdd];