Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIButton标题标签未显示_Iphone_Ios_Ios6_Uibutton - Fatal编程技术网

Iphone UIButton标题标签未显示

Iphone UIButton标题标签未显示,iphone,ios,ios6,uibutton,Iphone,Ios,Ios6,Uibutton,我见过很多类似的问题,但是没有人得到我想要的答案。当我使用这段代码创建UIButton并设置其标题标签时,UIButton会出现,但标题标签不会出现 UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal]; but

我见过很多类似的问题,但是没有人得到我想要的答案。当我使用这段代码创建UIButton并设置其标题标签时,UIButton会出现,但标题标签不会出现

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
button.titleLabel.text = @"Title";
button.titleLabel.font = [UIFont fontWithName:@"System-Bold" size:25.0f];
[self.view addSubview:button];
此代码显示按钮,但不显示标题视图,而是标题。为什么会这样?我正在为iOS 6及更高版本开发

注1:我不是在寻找这样的选项:

[button setTitle:@"Title" forState:UIControlStateNormal];
因为我必须使用标题标签来设置字体和字体大小

注2:我无法创建UILabel并将其添加为按钮的子视图。因为按钮稍后将设置动画

[button setTitle:@"Title" forState:UIControlStateNormal];
是设置标题字符串的正确方法


titleLabel用于设置字体和颜色。

更新按钮标题时,始终需要指定ControlState! UIButton有四个可能的值:

UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
例如:

[button setTitle:@"Title" forState:UIControlStateNormal];
UIButton *button                  = [UIButton buttonWithType: UIButtonTypeSystem];
button.titleLabel.font            = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode   = NSLineBreakByTruncatingTail;
然后可以为标题标签设置自定义设置:

[button.titleLabel setFont:[UIFont fontWithName:@"Zapfino" size:20.0]];
[button.titleLabel setTextColor:[UIColor blueColor]];
尝试使用

[button setNeedsLayout];
[button layoutIfNeeded];

它将强制按钮更新布局。

尽管此属性是只读的,但它自己的属性是读/写的。这些属性主要用于配置按钮的文本。例如:

[button setTitle:@"Title" forState:UIControlStateNormal];
UIButton *button                  = [UIButton buttonWithType: UIButtonTypeSystem];
button.titleLabel.font            = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode   = NSLineBreakByTruncatingTail;
不要使用标签对象设置文字颜色或阴影颜色。相反,可以使用此类的setTitleColor(:for:)和setTitleShadowColor(:for:)方法进行这些更改。若要设置标签的实际文本,请使用setTitle(quo:for:)(button.titleLabel.text不允许设置文本)。 即使按钮尚未显示,titleLabel属性也会返回一个值。系统按钮的属性值为零

迅速:

button.setTitle("your text", for: .normal)
button.setTitleColor(.white, for: .normal)
如果在故事板中使用来自像我这样的资产的自定义按钮图像

签入属性检查器

检入属性检查器将资源图像设置为背景


TLDR-试试这个
saveButton.titleLabel?.layer.zPosition=10

这对我来说是一件令人沮丧的事。我以编程方式创建了一个UIButton,添加了约束,并添加了渐变背景(我想这就是导致我的问题的原因)。然后我需要更新字体:

saveButton.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .semibold)
saveButton.setTitle("Save", for: .normal)
请注意,我正在以正确的方式设置标题。但仍然没有显示标题。最疯狂的部分-当我点击“调试视图层次结构”时,我可以看到标题就在那里!如果我不改变字体,标题也会显示出来。令人困惑然后我加了这个(字面上是扔意大利面,看看是什么卡住了):


成功了。似乎是渐变背景、设置字体等的组合将按钮的内部标题标签放在了其层的底部。

注1:我不是在寻找这个答案。我不能用这个字体!titleLabel用于设置由setTitle:forState设置的字符串的字体和颜色:非常感谢,我搜索了这么长时间:DYou应该使用
setTitleColor:forState
来设置标题的颜色(而不是在
titleLabel
上调用
setTextColor
)。