Ios 基于UIButton控件事件更改UILabel

Ios 基于UIButton控件事件更改UILabel,ios,uikit,uibutton,uilabel,uicontrolevents,Ios,Uikit,Uibutton,Uilabel,Uicontrolevents,我有一个ui按钮,它有一个默认图像,另一个图像用于突出显示/选定图像。按下按钮时,按钮的图像将变为高亮显示的图像,然后变为选定的图像(如果补漆在内部)。平常的事。。IB内的所有设置 然而,我试图在按钮上设置一个标签,在一个非常棘手的地方(没有对齐,硬编码) 我尝试在IB中的按钮上添加标签。问题:我需要标签的文本颜色随着按钮控件状态的更改而更改 因此,我创建了一个UIButton子类,添加了一个UILabelOutlet,并覆盖了以下方法: - (void)touchesBegan/Moved/C

我有一个
ui按钮
,它有一个默认图像,另一个图像用于突出显示/选定图像。按下按钮时,按钮的图像将变为高亮显示的图像,然后变为选定的图像(如果补漆在内部)。平常的事。。IB内的所有设置

然而,我试图在按钮上设置一个标签,在一个非常棘手的地方(没有对齐,硬编码)

我尝试在IB中的按钮上添加标签。问题:我需要标签的文本颜色随着按钮控件状态的更改而更改

因此,我创建了一个
UIButton
子类,添加了一个
UILabel
Outlet,并覆盖了以下方法:

- (void)touchesBegan/Moved/Cancelled/Ended:...;
- (void)setSelected:...
我能够实现我想要的。。。但是当我快速单击按钮时,更改不会反映出来。有时它不能正常工作。。。我甚至使用了异步调用。。。没用

因此,我前往
ui按钮
标题标签
。我试图使用它,但运气不佳

所以,我尝试了
ui按钮setTitle:forState:
,没有用。。。帮忙


额外详情:

- (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self.titleLabel setFrame:CGRectMake(0, 0, 100, 100)]; [self.titleLabel setText:@"THE TITLE LABEL"]; [self.titleLabel setHidden:NO]; [self.imageView setAlpha:0.2f]; NSLog(@"%@", self.subviews); [self setTitle:@"DEFAULT!!" forState:UIControlStateNormal]; } return self; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesCancelled:touches withEvent:event]; [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; } - (void)setSelected:(BOOL)selected { [super setSelected:selected]; [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; } - (void)check { if (self.isSelected || self.state == UIControlStateHighlighted || self.state == UIControlStateSelected) { [_label setHighlighted:YES]; } else { [_label setHighlighted:NO]; } } -(id)initWithCoder:(NSCoder*)aDecoder{ self=[super initWithCoder:aDecoder]; 如果(自我){ [self.titleLabel setFrame:CGRectMake(0,01000100)]; [self.titleLabel setText:@“标题标签”]; [self.titleLabel setHidden:否]; [self.imageView setAlpha:0.2f]; NSLog(@“%@”,自我子视图); [自我设置标题:@“默认!!”用于状态:UIControlStateNormal]; } 回归自我; } -(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{ [超级触摸开始:触摸事件:事件]; [自执行选择器:@selector(check)with object:nil afterDelay:0.1]; } -(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{ [超级触摸移动:触摸事件:事件]; [自执行选择器:@selector(check)with object:nil afterDelay:0.1]; } -(无效)触控取消:(NSSet*)触控事件:(UIEvent*)事件{ [超级触控取消:触控事件:事件]; [自执行选择器:@selector(check)with object:nil afterDelay:0.1]; } -(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{ [超级触控:触控事件:事件]; [自执行选择器:@selector(check)with object:nil afterDelay:0.1]; } -(无效)已选择:(布尔)已选择{ [超级用户选择:已选择]; [自执行选择器:@selector(check)with object:nil afterDelay:0.1]; } -(作废)支票{ 如果(self.isSelected | | self.state==UIControlStateHighlighted | | self.state==UIControlStateSelected){ [_标签设置突出显示:是]; }否则{ [_标签设置突出显示:否]; } } 输出:

(
    "<UIImageView: 0x8b24930; frame = (0 0; 243 39); clipsToBounds = YES; alpha = 0.2; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8b248e0>>",
    "<UIButtonLabel: 0x8b247a0; frame = (0 0; 100 100); text = 'THE TITLE LABEL'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8b25000>>"
)
(
"",
""
)

您需要为UIButton设置背景图像…可能是您正在设置按钮的图像属性…这就是为什么您的标签被图像重叠的原因

[optionButtonOutlet  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

 [optionButtonOutlet setBackgroundImage:[UIImage imageNamed:@"predict_match_btn_blue_pressed@2x.png" ]forState:UIControlStateNormal];

经过这几个月的努力,我发现最好的答案是:

- (CGRect)titleRectForContentRect:(CGRect)contentRect;
以我想要的方式放置标签。这使我能够利用:

[self setTitleColor:[UIColor offWhiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor burntCarbonColor] forState:UIControlStateSelected];
[self setTitleColor:[UIColor burntCarbonColor] forState:UIControlStateHighlighted];

对于更好奇的人:

- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    CGSize margin = CGSizeMake(15.f, 5.f);
    CGRect clay = contentRect;
    clay = CGRectInset(clay, margin.width, 0.f);
    clay.origin.x += margin.width;
    clay.origin.y += margin.height;

    return clay;
}

:/ .. 最后,我在按钮上方的
UILabel
中添加了一个背景色,使其与按钮的默认状态相匹配。Upoen高亮显示,标签显示为带边框…感谢您的回答。我的标签看起来很完美,没有重叠。我只需要它根据控件状态更改颜色,并将其位置更改为右上角(例如)。