简体   繁体   English

如何使用Swift以编程方式在UIButton中向左对齐文本和向右对齐图像?

[英]How do I align text to the left and an image to the right in a UIButton programmatically using Swift?

I am trying to create a button that has 16 padding on the left and rights sides of a button. 我正在尝试创建一个在按钮的左侧和右侧具有16个填充的按钮。

My UIButton override 我的UIButton覆盖

override func awakeFromNib() {
    backgroundColor =  UIColor.white
    setTitleColor(UIColor.primary, for: .normal)
    setTitleColor(UIColor.primary, for: .highlighted)
    titleLabel!.font = UIFont.mediumHeadline


    imageEdgeInsets = UIEdgeInsets(top: 0, left: self.bounds.width - 32, bottom: 0, right: 0)
}

and in my controller: 在我的控制器中:

func setupHeadingButton() {
        headerButton.setImage(UIImage.rightNavigationArrow, for: .normal)

    }

It should look like this: 它看起来应该像这样: 在此处输入图片说明

you must change values for titleEdgeInsets and imageEdgeInsets button's properties. 您必须更改titleEdgeInsets和imageEdgeInsets按钮的属性的值。

and also for implement this you can create custom UIView with override method: func touchesBegan(_ touches: Set, with event: UIEvent?) 为此,您还可以使用重写方法创建自定义UIView:func touchesBegan(_ touches:Set,带有事件:UIEvent?)

you can do it by UIEdgeInsets 你可以通过UIEdgeInsets做到

button.imageEdgeInsets = UIEdgeInsets(top: 0, left: self.view.bounds.width - 32, bottom: 0, right: 0)

change the left property based on your desire. 根据您的意愿更改左侧属性。 the result of the above code is like this: 上面代码的结果是这样的:

在此处输入图片说明

don't forget to change the alignment of the button to the left: 不要忘记将按钮的对齐方式更改为左侧:

在此处输入图片说明

First option is to configure button with autolayout (Im using Snapkit framework beacuse of best readability) and the second option is to configure it with frame. 第一种选择是使用自动布局来配置按钮(由于具有最佳的可读性,因此我使用Snapkit框架),第二种选择是使用框架对其进行配置。

import UIKit

final class CustomButton: UIButton {

    let leftImageView: UIImageView
    let centerLabel: UILabel

    override init(frame: CGRect) {
        centerLabel = UILabel()
        leftImageView = UIImageView()
        super.init(frame: frame)
        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        centerLabel = UILabel()
        leftImageView = UIImageView()
        super.init(coder: aDecoder)
        configure()
    }

    fileprivate func configureWithSnapKit() {


        setTitle("", for: .normal)

        // Spacing view for space between imageview and label
        let spacingView1 = UIView()
        let spacingView2 = UIView()
        let spacingView3 = UIView()

        // Configuring stack view

        let stackView = UIStackView(arrangedSubviews: [spacingView1, centerLabel, spacingView2, leftImageView, spacingView3])
        stackView.distribution = .fill
        stackView.axis = .horizontal
        stackView.alignment = .fill

        leftImageView.isUserInteractionEnabled = true
        leftImageView.contentMode = .center

        centerLabel.lineBreakMode = .byTruncatingTail
        centerLabel.font = Fonts.sfui_medium(size: 12)
        centerLabel.textColor = Colors.white
        centerLabel.numberOfLines = 1

        // Adding subviews
        addSubview(stackView)
        stackView.isUserInteractionEnabled = false

        // SnapKit autolayout
        stackView.snp.makeConstraints { (make) in
            make.edges.equalTo(0)
        }

        spacingView1.snp.makeConstraints { (make) in
            make.width.equalTo(16)
        }

        spacingView3.snp.makeConstraints { (make) in
            make.width.equalTo(16)
        }

        leftImageView.snp.makeConstraints { (make) in
            make.width.equalTo(22)
        }
    }

    fileprivate func configureWithFrame() {

        setTitle("", for: .normal)

        // Spacing view for space between imageview and label
        let spacingView = UIView()

        // Configuring stack view

        let stackView = UIStackView(arrangedSubviews: [centerLabel, spacingView, leftImageView])
        stackView.distribution = .fill
        stackView.axis = .horizontal
        stackView.alignment = .fill

        leftImageView.isUserInteractionEnabled = true
        leftImageView.contentMode = .center

        centerLabel.lineBreakMode = .byTruncatingTail
        centerLabel.font = Fonts.sfui_medium(size: 12)
        centerLabel.textColor = Colors.white
        centerLabel.numberOfLines = 1

        // Adding subviews
        addSubview(stackView)
        stackView.isUserInteractionEnabled = false

        stackView.frame = CGRect(x: 16, y: 0, width: self.frame.width - 32, height: self.frame.height)
    }
}

First I set the image: 首先,我设置图像:

headerButton.setImage(UIImage.settingsUpArrow, for: .normal)

then swap the image and button and change their insets that incorporates the 16 padding on both sides: 然后交换图像和按钮并更改其插图,该插图在两侧都包含16个填充:

headerButton.titleEdgeInsets = UIEdgeInsetsMake(0, -(headerButton.imageView?.frame.size.width)! + 16, 0, (headerButton.imageView?.frame.size.width)!)
        let leftPadding =  headerButton.bounds.width - (headerButton.imageView?.bounds.width)!-16
        headerButton.imageEdgeInsets = UIEdgeInsetsMake(0, leftPadding, 0, -(headerButton.titleLabel?.frame.size.width)!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM