尝试在自定义UIAlertView中将UILabel置于UIButton之上

时间:2013-06-27 06:39:18

标签: ios objective-c uibutton uilabel uialertview

我要做的是在UILabel之上放置UIButtonUIButton将图片作为背景,结果是我无法在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

2 个答案:

答案 0 :(得分:4)

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

试试这个 -

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

答案 1 :(得分: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];

希望它有所帮助。