我对UIButton的文本左对齐有问题.我也试着把它改成.Right,但它仍然保持在中心位置.我也try 了aButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)而不是aButton.titleLabel?.textAlignment = .Left,但这也没有改变任何事情.有没有其他方法可以通过编程更改UIButton个标题的对齐方式?

        allButtonViews = UIView(frame: CGRectMake(0, 44, 100, 100))
        allButtonViews.backgroundColor = .redColor()

        let campusButton = UIButton(type: UIButtonType.System) as UIButton
        aButton.frame = CGRectMake(0, 0, 300, 70)
        aButton.setTitle("A", forState: .Normal)
        aButton.titleLabel?.textColor = .blueColor()
        aButton.titleLabel?.textAlignment = .Left
        aButton.backgroundColor = .whiteColor()

        sortView.addSubview(aButton)
        view.addSubview(allButtonViews)

enter image description here

推荐答案

试试这个:

button.contentHorizontalAlignment = .left

Swift相关问答推荐

AirPods手势不发送AVAudioApp静音状态通知

map 视图上的SwiftUI渐变折线

";async let和";async之间的差异让我们等待";

如何将CodingKeys作为数组而不是枚举传递到类外部的函数中?

如何在自定义视图中读取修改符子元素?

SWIFT:使字典数组值可变

在MacOS上检测键盘是否有Globe键

Swift UI中视图上的值未更新

为什么 UITapGestureRecognizer 对于 Swift 集合视图中的单元格图像无法正常工作?

当字符串包含 \r\n 时,NSRegularExpression 不起作用

Firebase removeObserver 在 Swift 5 中不起作用

NavigationStack 和 TabView - 工具栏和标题不显示

在 Swift 中增量写入大型文本文件的最佳方法

RxSwift 中 Single.just(...) 和 Observable.just(...) 之间有区别吗?

Swift并发:为什么不在其他后台线程上执行任务

持久化字符串列表属性 RealmSwift

P384 公钥获取IncorrectParameterSize

Swift 读写图片占用大量内存

AVPlayer 在 iOS 15.4 中寻求 completionHandler 返回 false

Swift 5 秒后关闭 UIAlertView