I have a UIButton with text "Explore the app" and UIImage (>) In Interface Builder it looks like:

[ (>) Explore the app ]

但我需要把这UIImage放在文本后面:

[ Explore the app (>) ]

我怎样才能把UIImage移到右边?

推荐答案

这是我自己做这件事的方法,(after about 10 years)

  1. 来自UIButton的子类(Button,因为我们生活在SWIFT时代)
  2. 将图像和标签放入堆栈视图.
class CustomButton: Button {

    var didLayout: Bool = false // The code must be called only once

    override func layoutSubviews() {
        super.layoutSubviews()
        if !didLayout, let imageView = imageView, let titleLabel = titleLabel {
            didLayout = true
            let stack = UIStackView(arrangedSubviews: [titleLabel, imageView])
            addSubview(stack)
            stack.edgesToSuperview() // I use TinyConstraints library. You could handle the constraints directly
            stack.axis = .horizontal
        }
    }
}

Ios相关问答推荐

如何调整包含extView的UICollectionViewListCell的大小?

SWIFT中具有可选返回类型和泛型的网络请求

避免从CoreData加载图像列表时出现SwiftUI挂起

如何使视图完全适合屏幕,而不会在旋转时溢出手机屏幕的边界?

底部导航栏在iOS上浮动

iOS Swift 应用程序实例的存活时间

在 iOS 上以后台模式安排任务(屏幕时间 API)

如何在Swift/Combine中从timer.publish属性传递两个参数到任意方法

SwiftUI HStack 文本字段对齐

不使用故事板创建 iMessage 应用程序

iOS - 判断开发者模式是否开启 (Swift)

如何根据设备时间设置 Flutter 应用时间线?

带有属性包装器的不可用属性

SwiftUI:如何用拇指移动滑块值

判断Apple LiveText是否可用?

在 UICollectionView 上从右到左对齐

如何获取 UICollectionViewCell 的矩形?

ios 崩溃 EXC_BAD_ACCESS KERN_INVALID_ADDRESS

iOS 7 半透明模态视图控制器

iOS 7 应用程序图标、启动图像和命名约定,同时保留 iOS 6 图标