gpt4 book ai didi

ios - 当我设置图像时,UIButton 标题不显示

转载 作者:行者123 更新时间:2023-12-01 17:52:48 25 4
gpt4 key购买 nike

此问题最常见的解决方法是“设置为背景图像,而不是普通图像”

在我的例子中,我将背景图像放在我所有的按钮上,如果它是一个锁定的对象,我会在背景图像的顶部添加一个锁的图像。该图像看起来符合预期,但在那种情况下它不会显示图像的标题。

UIButton *button = (UIButton *)view;
if(self.currentArray[index][2] != NULL) //before it was if (button == nil)
{
NSString *name = [[NSString alloc] initWithString:self.currentArray[index][2]];
UIImage *image = [UIImage imageNamed:self.currentArray[index][5]];
button = [UIButton buttonWithType:UIButtonTypeCustom];

if ([self.currentArray[index][6] isEqualToString:@"true"]) {
//show it
}else{
//-------------------------
//--THE PROBLEM HAPPENS IF THIS PIECE OF CODE IS EXECUTED, SPECIFICALLY setImage
//-------------------------
button.alpha = 0.5;
UIImage *locked = [UIImage imageNamed:@"locked.png"];
button.imageEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
[button setImage:locked forState:UIControlStateNormal];
}

button.frame = CGRectMake(0.0, 0.0, 56, 56);
button.tag = index;

[button setBackgroundImage:image forState:UIControlStateNormal];

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UIEdgeInsets buttonInset = UIEdgeInsetsMake(30, -30, -25, -30);
[button setTitleEdgeInsets:buttonInset];
[button setTitle:name forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize: 8];

[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}

下面是无锁和有锁的图片:
enter image description here

最佳答案

答案是here .

引用:“看起来,当您在按钮中放置图像时,文本会被推到右侧。使用边缘设置将其带回图像上方。”

您需要做的是尝试插入标题,以便将标题标签放回正确的位置。

我建议使用类似 extraLeftInset 的变量,默认为 0 并在 else 分支中设置为正确的值。

上面的答案建议使用这个公式[Edge inset for Title] Left = -(imageWidth * 2),所以这个值应该是-512,因为你说图像通常是256。不过,最好在运行时检查图像的 size 来评估它。

希望这有帮助:)

关于ios - 当我设置图像时,UIButton 标题不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24330316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com