Ios 尝试将UILabel放置在自定义UIAlertView中UIButton的顶部
我想做的是在Ios 尝试将UILabel放置在自定义UIAlertView中UIButton的顶部,ios,objective-c,uibutton,uilabel,uialertview,Ios,Objective C,Uibutton,Uilabel,Uialertview,我想做的是在ui按钮上放置一个ui标签。 ui按钮有一个图像作为背景,结果是我看不到ui按钮上的正常文本,因此我想把ui标签放在它上面 我的代码是: NAlertView *customAlert = [[NAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"cancel", nil]; [customAlert s
ui按钮
上放置一个ui标签
。
ui按钮
有一个图像作为背景,结果是我看不到ui按钮
上的正常文本,因此我想把ui标签
放在它上面
我的代码是:
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];
}
结果:
更换
[button setImage:[UIImage imageNamed:@"short_button_gold_off.png"] forState:UIControlStateNormal];
到
改变所有这些可能性
并使用以下方法更改按钮的文本
[button setTitle:@"text" forState:UIControlStateNormal];
希望有帮助。更换
[button setImage:[UIImage imageNamed:@"short_button_gold_off.png"] forState:UIControlStateNormal];
到
改变所有这些可能性
并使用以下方法更改按钮的文本
[button setTitle:@"text" forState:UIControlStateNormal];
希望有帮助。这意味着您只想在UIButton上添加标题 试试这个-
[button setBackgroundImage:[UIImage imageNamed:@"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setTitle:@"Title" forState:UIControlStateNormal];
这意味着您只想在UIButton上添加标题 试试这个-
[button setBackgroundImage:[UIImage imageNamed:@"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setTitle:@"Title" forState:UIControlStateNormal];
不需要从定义本身继承“setTitle”。当您这样做时,“不需要从定义本身继承“setTitle”是什么意思:NAlertView*customAlert=[[NAlertView alloc]initWithTitle:@“title”消息:@“message”委托:自取消按钮:@“ok”其他按钮:@“cancel”,nil];[自定义警报显示];-->“initWithTitle”自行设置标题,因此无需设置标题。不需要“setTitle”,因为它继承自定义本身。执行以下操作时,“不需要“setTitle”,因为它继承自定义本身?”是什么意思:NAlertView*customAlert=[[NAlertView alloc]initWithTitle:@“title”消息:@“message”委托:self CancelButtontile:@“ok”OtherButtontiles:@“cancel”,nil];[customAlert show];-->“initWithTitle”自行设置标题,因此无需设置标题。