【问题标题】:Objective C How can we change the background image of a button in just when it is clicked and then go back to normal?Objective C 我们怎样才能改变按钮的背景图像,当它被点击然后恢复正常?
【发布时间】:2018-02-23 11:38:21
【问题描述】:
[myButton setImage:[UIImage imageNamed:@"btn_normal"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"btn_highlighted"] forState:UIControlStateHighlighted];
[myButton setImage:[UIImage imageNamed:@"btn_highlighted"] forState:UIControlStateSelected];
myButton.showsTouchWhenHighlighted = YES;

我已经尝试过这段代码,但它不起作用。

【问题讨论】:

标签: ios objective-c uibutton


【解决方案1】:

您应该使用setBackgroundImage:forStatesetImage:forState 更改与按钮标题处于同一深度级别的图像。此外,backgroundImage 会自动适应按钮的视图边界。 image 不是。

【讨论】:

    【解决方案2】:

    实现该按钮的onTouchDown动作,并通过[btn setBackgroundImage: forState:]在该函数中设置按钮背景图像

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2019-01-27
      • 2021-08-20
      • 1970-01-01
      • 2019-08-05
      • 2022-01-10
      • 2012-01-16
      • 1970-01-01
      • 2020-09-09
      相关资源
      最近更新 更多