ios - 带有背景图像按钮的圆形边框 swift

标签 ios swift uibutton

我正在使用此代码创建带有背景图像的圆形边框按钮

{  
  Button.layer.cornerRadius = clear.frame.width/2 
  Button.clipsToBounds = true
  Button.layer.borderWidth = 5.0
  Button.layer.borderColor = UIColor(displayP3Red: 91/256, green: 87/256, blue: 115/256, alpha: 1.0).cgColor
}

这是结果:

我想在按钮和圆角边框之间为此结果创建一个边距:

提前致谢

最佳答案

您可以将其设置为 UIButton 的图像,而不是设置背景图像。

let image = UIImage(named: "imageName")
yourButton.setImage(image, for: .normal)

使用 Storyboard,来自属性检查器。

enter image description here

您还可以在尺寸检查器中设置内容、标题和图像插入(顶部、左侧、底部和右侧)。

  • 设置内容插入以同时设置图片和标题。
  • 设置Title Insets以一次设置标题。
  • 设置Image Insets以一次设置图像。

    enter image description here

关于ios - 带有背景图像按钮的圆形边框 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50987147/

相关文章:

ios - 如何在选项卡栏按钮触摸上创建滑动菜单

ios - AVAssetImageGenerator 使用动画工具和视频合成生成带有绿色像素的图像

iphone - 如何将 "transfer"第一响应者从一个 UIView 转移到另一个 UIView?

ios - 如何通过带参数的方法将 UIButton 设置为 Action 实现?

ios - 为什么这个方法会抛出异常?

ios - UILocalNotification 在字典中标记为不明确

ios - 如何在 NSXMLParser 中将不同 URL 的解析数据保存到不同的数组中?

ios - didBeginContact 不触发

xcode - 检测何时在 SKScene 中触摸按钮?

ios - 从 UIButton 获取对 UIBarButtonItem 的引用?