【问题标题】:Swift setting image as button, clicking too hardSwift将图像设置为按钮,点击太用力
【发布时间】:2016-04-01 09:59:03
【问题描述】:

我已将transparent image 设置为background,用于我正在使用的UIButton。它看起来很完美,但问题是transparent image 相当小,因此用户在第一次尝试时很难正确单击它。


这是我的代码:

optionsButton.setBackgroundImage(UIImage(named: "CalendarEventArrowIcon"), forState: UIControlState.Normal)
optionsButton.addTarget(self, action: "optionsButtonPressed", forControlEvents: UIControlEvents.TouchDown)

有没有一种方法可以获取我正在使用的当前图像,并将其放置在具有更大框架的 UIImage 中,然后在Swift 中将其作为按钮programmatically 的背景图像?

通过这样做,用户可以更轻松地点击按钮,因为背景图片会占据更大的区域。

或者是我真正做这样的事情以返回 Photoshop 并使我正在使用的 background image 更大并且不再有 transparent background 的唯一方法?

【问题讨论】:

  • hm ...我建议您再次执行 Photoshop 工作以使图片足够大。
  • 最简单的方法是放大图片,或者将按钮嵌入到一个清晰的 UIView 中,并在其中添加一个点击手势识别器来触发 optionsButtonPressed 操作。

标签: ios swift uibutton swift2 ios9


【解决方案1】:

您可以使用contentEdgeInsets 扩展按钮的可点击区域。例如,要在按钮四周添加 15 个填充点,请添加以下内容:

optionsButton.contentEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15)

【讨论】:

    【解决方案2】:

    所以底线是你想以编程方式快速调整图像大小。 看看这是否有帮助(投票 12 和 cmets 说它有效):

    swift resize image

    【讨论】:

      【解决方案3】:
      optionsButton.setImage(UIImage(named: "CalendarEventArrowIcon"), forState: UIControlState.Normal)
      
      optionsButton.addTarget(self, action: Selector("optionsButtonPressed"), forControlEvents: UIControlEvents.TouchDown)
      

      【讨论】:

      • 您所做的只是获取我的代码并将Selector 标签添加到操作参数中。 Swift 2 不需要Selector 标签,所以这什么都不做
      猜你喜欢
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 2015-09-15
      • 1970-01-01
      相关资源
      最近更新 更多