gpt4 book ai didi

ios - 尝试将 UILabel 放在自定义 UIAlertView 中的 UIButton 之上

转载 作者:行者123 更新时间:2023-11-28 18:22:36 25 4
gpt4 key购买 nike

我想做的是将 UILabel 放在 UIButton 之上。UIButton 有一个图像作为背景,结果是我在 UIButton 上看不到正常文本,因此我想放置 UILabel在它上面。

我的代码是:

NAlertView *customAlert = [[NAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"cancel", nil];
[customAlert show];

NAlertView.m:

- (void)layoutSubviews
{
for (id obj in self.subviews) //Search in all sub views
{

if ([obj isKindOfClass:[UIImageView class]]) // Override UIImageView (Background)
{
UIImageView *imageView = obj;
[imageView setImage:[UIImage imageNamed:@"CustomAlertView"]];
[imageView setAlpha:0.7];
[imageView sizeToFit];
}

if ([obj isKindOfClass:[UILabel class]]) // Override UILabel (Title, Text)
{
UILabel *label = (UILabel*)obj;

label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor clearColor];
}

if ([obj isKindOfClass:[UIButton class]]) // Override the UIButton
{
UIButton *button = (UIButton*)obj;

CGRect buttonFrame = button.frame; // Change UIButton size
buttonFrame.size = CGSizeMake(127, 35);
CGFloat newYPos = buttonFrame.origin.y + 9; // Change UIButton position
buttonFrame.origin.y = newYPos;

button.frame = buttonFrame;

[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

switch (button.tag)
{
case kFIRST_BUTTON:
{

[button setImage:[UIImage imageNamed:@"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"short_button_gold_on.png"] forState:UIControlStateHighlighted];
}
break;
case kSECOND_BUTTON:
{
[button setImage:[UIImage imageNamed:@"short_button_black_off.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"short_button_black_on.png"] forState:UIControlStateHighlighted];
}
break;
}
}
}

[self updateConstraints];
}

结果:

No text on the button

最佳答案

这意味着您只想在 UIButton 上添加标题?

试试这个 -

  [button setBackgroundImage:[UIImage imageNamed:@"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setTitle:@"Title" forState:UIControlStateNormal];

关于ios - 尝试将 UILabel 放在自定义 UIAlertView 中的 UIButton 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17336179/

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