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