UIButton触摸时不闪烁

问题描述 投票:2回答:2

我有一个UIButton,它是通过程序创建的,位于UIView内(层次结构中没有其他级别)。按钮确实正确响应touchUpInside事件,但是由于某些原因,文本触摸时没有表现出正常的“闪烁”行为。如果有人知道是什么原因引起的,我想找回。

[其他说明:我有userInteractionEnabled = TRUE,并且我的代码中没有自定义动画。相关的实例化代码(UIColor名称来自自定义类别):

self.loginButton = [[UIButton alloc] init];
self.loginButton.backgroundColor = [UIColor MPBlackColor];
[self.loginButton setTitle:@"LOG IN" forState:UIControlStateNormal];
[self.loginButton setTitleColor:[UIColor MPGreenColor] forState:UIControlStateNormal];
self.loginButton.titleLabel.font = [UIFont fontWithName:@"Oswald-Bold" size:24.0f];
ios objective-c uibutton
2个回答
7
投票

问题是,除非为高亮状态提供单独的颜色,否则您在正常状态下使用的自定义颜色也会应用于高亮状态。所有与按钮状态相关的值都是如此。


0
投票

为正常和突出显示的按钮状态都应用颜色。在Swift中:

loginButton.setTitleColor(.green, for: .normal)
loginButton.setTitleColor(.green, for: .highlighted)

注意:相同的逻辑适用于带有图像的闪烁按钮。您应该同时设置.normal.highlighted状态

© www.soinside.com 2019 - 2024. All rights reserved.