【问题标题】:Setting button image and title wont show title设置按钮图像和标题不会显示标题
【发布时间】:2019-08-26 16:09:17
【问题描述】:

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

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

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

谁能看到我错过了什么?

【问题讨论】:

    标签: ios swift uibutton uikit


    【解决方案1】:

    设置为自定义按钮

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

    【讨论】:

      【解决方案2】:

      尝试设置按钮的backgroundImage 属性。

      dislikeButton.setBackgroundImage(UIImage(named: "your-image-name.jpg"), for: .normal)
      

      【讨论】:

      • 这似乎避免了这个问题,意味着我必须设置一个解决方法来正确缩放图像。使用边缘插图也一样
      【解决方案3】:

      这是一个 ios sdk 错误,图像和标题的自定义插图仅在固定宽度下才能成功运行

      【讨论】:

        猜你喜欢
        • 2021-01-24
        • 2016-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多