0

我想要做的是将 aUILabel放在 a 之上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];
}

结果:

按钮上没有文字

4

2 回答 2

4

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

尝试这个 -

  [button setBackgroundImage:[UIImage imageNamed:@"short_button_gold_off.png"] forState:UIControlStateNormal];
  [button setTitle:@"Title" forState:UIControlStateNormal];
于 2013-06-27T07:01:37.450 回答
2

代替

[button setImage:[UIImage imageNamed:@"short_button_gold_off.png"] forState:UIControlStateNormal];

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

改变所有这些可能性。

并使用以下方法更改按钮的文本

[button setTitle:@"text" forState:UIControlStateNormal];

希望能帮助到你。

于 2013-06-27T06:52:00.537 回答