在 UIButton 上同时显示图像和标题 - Swift 3 [英] Display Image and Title Simultaneously on UIButton - Swift 3

查看:37
本文介绍了在 UIButton 上同时显示图像和标题 - Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I display a title and image simultaneously on a UIbutton? I tried to follow Display image and text in Button which uses swift 2 but the same process only displays the image in swift 3, with the title hidden. The image source file is 35x35 pixels. My code:

  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)

当前输出

需要一个程序化的解决方案,因为我没有使用故事板.谢谢!

Need a programatic solution as I am not using storyboard. Thanks!

推荐答案

我认为你应该为 UIControlState.normal

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

这篇关于在 UIButton 上同时显示图像和标题 - Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆