繁体   English   中英

如何在右侧创建带有图标的按钮?

[英]How to create a button with an icon on the right-hand side?

我想在按钮标签旁边放一个图标。

问题:Interface Builder 没有将按钮图像放在右侧。 所以在 (x) 旁边还有一些空间。

谢谢你的帮助

在此处输入图片说明

您可以在 Interface Builder 中通过设置标题和图像插入来执行此操作:

在此处输入图片说明

在这种情况下,我将title right inset设置为 30 ,将image left inset 设置为 80 您可以通过设置UIButtonimageEdgeInsettitleEdgeInset属性在代码中实现相同的效果。 知道 UIButton 的子视图的大小,您可能可以使用以下方法计算边缘插入:

CGSize labelWidth = myButton.titleLabel.frame.size.width;
CGSize imageWidth = myButton.imageView.frame.size.width;
myButton.titleEdgeInsets = (UIEdgeInsets){0.0, -imageWidth, 0.0, imageWidth};
myButton.imageEdgeInsets = (UIEdgeInsets){0.0, labelWidth, 0.0, -labelWidth};

在 Xcode 8 中,您将在大小检查器中找到这些属性:

Xcode 大小检查器屏幕截图

在此处输入图片说明

在窗口上选择图像(如果(x)是图像)或标题(如果(x)只是关键)并根据您的需要设置左,右偏移

我已经做到了:

class CustomButton: UIButton {
    
    open var rightIcon: UIImage? {
        didSet {
            rightIconImage = UIImageView(image: rightIcon?.withRenderingMode(.alwaysTemplate))
            rightIconImage?.tintColor = .white
            if let rightIconImage = rightIconImage {
                addSubview(rightIconImage)
            }
            layoutSubviews()
        }
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        setupRightIconIfNeeded()
    }
    
    func setupRightIconIfNeeded() {
        if let rightIconImage = rightIconImage {
            rightIconImage.translatesAutoresizingMaskIntoConstraints = false
            titleLabel?.translatesAutoresizingMaskIntoConstraints = false
            if let titleLabel = titleLabel, titleLabel.frame.width > 0 {
                let paddingLeft = (frame.width - titleLabel.frame.width - spacing - iconSize) / 2
                NSLayoutConstraint.deactivate(constraints)
                NSLayoutConstraint.deactivate(titleLabel.constraints)
                NSLayoutConstraint.activate([
                    //set height constraint for button
                    heightAnchor.constraint(equalToConstant: apolloSize.height),
                    //set constraint for title label
                    titleLabel.widthAnchor.constraint(equalToConstant: titleLabel.frame.width),
                    titleLabel.heightAnchor.constraint(equalToConstant: titleLabel.frame.height),
                    titleLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
                    titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: paddingLeft),
                    //set constraint for right icon
                    rightIconImage.leadingAnchor.constraint(equalTo: titleLabel.trailingAnchor, constant: spacing),
                    rightIconImage.centerYAnchor.constraint(equalTo: titleLabel.centerYAnchor),
                    rightIconImage.widthAnchor.constraint(equalToConstant: iconSize),
                    rightIconImage.heightAnchor.constraint(equalToConstant: iconSize)
                ])
            }
        }
    }
}

暂无
暂无

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

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