麻烦 left-aligning UIButton 标题 (iOS/Swift)

Trouble left-aligning UIButton title (iOS/Swift)

我在 left-aligning UIButton 的文本中遇到了问题。我也尝试将其更改为 .Right 但它仍然居中。我也尝试 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)

试试这个:

button.contentHorizontalAlignment = .left

更新 Swift 3 或 Swift 4

yourButton.contentHorizontalAlignment = .left