ios - 在 UIButton 上同时显示图像和标题 - Swift 3

标签 ios swift swift3

如何在 UIbutton 上同时显示标题和图像?我试着关注 Display image and text in Button它使用 swift 2,但相同的过程仅在 swift 3 中显示图像,标题被隐藏。图像源文件为 35x35 像素。我的代码:

  let folderButton = UIButton(frame: CGRect(x: 100 , y: 200, width: 200, height: 41))
  folderButton.layer.borderColor = UIColor.black.cgColor
  folderButton.layer.borderWidth = 1
  folderButton.imageEdgeInsets = UIEdgeInsets(top: 0,left: 162,bottom: 3,right: 3)
  folderButton.titleEdgeInsets = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 41)
  folderButton.setImage(UIImage(named:"FolderIcon35"), for: UIControlState.normal)
  folderButton.setTitle("Test", for: UIControlState.selected)
  folderButton.setTitleColor(.black, for: .normal)
  view.addSubview(folderButton)

current Output

需要一个程序化的解决方案,因为我没有使用 Storyboard。谢谢!

最佳答案

我认为你应该为 UIControlState.normal 设置标题

folderButton.setTitle("Test", for:.normal)

关于ios - 在 UIButton 上同时显示图像和标题 - Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43908717/

相关文章:

ios - 获取注释图钉点击事件 MapKit Swift

arrays - 显示错误,无法将类型 'x'(即数组模型对象)的值转换为预期类型 'x'(即数组模型对象)

ios - Twitter.sharedInstance() 不存在 swift TwitterKit

ios - 如何在 iOS 上以编程方式将 View 旋转 180 度?

arrays - swift 3 : Sort Ordered List of Objects by Property

ios - 如何在 iOS Swift 中设置特定索引的 Collection View 单元格?

php - 从 PHP 脚本获取 JSON

ios - 创建一行动态的 UITextfields

ios - 增加折线图中两个点标签之间的间隙

swift - inout String 不能转换为 Swift 3 范围内的 String