首页 > 解决方案 > 设置按钮图像和标题不会显示标题

问题描述

我正在尝试设置一个按钮,其图像和标题垂直对齐,以便标题位于图像下方。谷歌搜索说按钮不显示标题的原因是图像边缘将标题推开。但是当我设置边缘时,我仍然看不到标题。

我的按钮以编程方式设置如下:

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,宽度设置为屏幕宽度的一半。

按钮看起来像这个 atm

谁能看到我错过了什么?

标签: iosswiftuibuttonuikit

解决方案


设置为按钮自定义

let dislikeButton = UIButton(type : .custom)
dislikeButton.translatesAutoresizingMaskIntoConstraints = false

推荐阅读