ios - 设置按钮图像和标题不会显示标题
问题描述
我正在尝试设置一个按钮,其图像和标题垂直对齐,以便标题位于图像下方。谷歌搜索说按钮不显示标题的原因是图像边缘将标题推开。但是当我设置边缘时,我仍然看不到标题。
我的按钮以编程方式设置如下:
private let dislikeButton = UIButton()
dislikeButton.contentMode = .scaleAspectFit
dislikeButton.setTitle(String.Localized.Button.dislike, for: .normal)
dislikeButton.setTitleColor(.white, for: .normal)
dislikeButton.setImage(UIImage.leftPaw, for: .normal)
dislikeButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 150)
dislikeButton.titleEdgeInsets = UIEdgeInsets(top: 50, left: 150, bottom: 0, right: 0)
按钮高度设置为 100,宽度设置为屏幕宽度的一半。
谁能看到我错过了什么?
解决方案
设置为按钮自定义
let dislikeButton = UIButton(type : .custom)
dislikeButton.translatesAutoresizingMaskIntoConstraints = false
推荐阅读
- r - 什么是 R 中的日期(数据类型)以及如何在不同格式之间进行转换?
- database - 如何在规范化的 nosql 数据库中向上/向下引用多个级别?
- typescript - 如何验证复选框是否已被单击?
- database-design - ERD 图关系 M:N
- java - 使用 SizeAndTimeBasedRollingPolicy 时出现 MaxFileSize 问题
- selenium - 如何在 selenium 中使用 javascript 选择下拉菜单?
- node.js - 将数组保存为任何格式的输出文件
- c# - 命令行解析器库在 C# 中给出“System.Type”错误
- sql-server - 在 SQL Server 2016 中启用更改日志
- swift - 在 Swift 中链接 Promise 以初始化自定义对象