繁体   English   中英

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

[英]Trying to place UILabel on top of UIButton in a custom UIAlertView

我想做的是将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];
}

结果:

按钮上没有文字

那意味着您只想在UIButton上添加Title?

尝试这个 -

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

更换

[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];

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM