繁体   English   中英

ios:未显示按钮的子视图

[英]ios : subview of a button not showing up

我的代码如下:

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标签根本没有显示。

我想念什么?

标签上的框太大。 结果,标签文本被绘制在按钮框的外部。 理想情况下,标签的框架最多应与按钮的高度一样高。

UIButton已经有一个titleLabel。 您是否尝试过仅使用它?

button.titleLabel.text = "+";

甚至使用其他按钮类型:

UIButton* button = [UIButton buttonWithType:UIButtonTypeContactAdd];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM