【问题标题】:UIButton Image with PDF has full width image view带有 PDF 的 UIButton 图像具有全宽图像视图
【发布时间】:2021-05-16 12:01:46
【问题描述】:

我正在尝试构建一个类似单选按钮的按钮(左侧有一个圆点,旁边是文本 它在右边),很像这样:

我有 2 个 PDF (link to one of them),其中包含选定和未选定收音机的图像。我的按钮代码如下:

let radioBtn = UIButton()
radioBtn.setImage(UIImage(named: "radio", in: .module, compatibleWith: nil), for: .normal)
radioBtn.setImage(UIImage(named: "radio_ticked", in: .module, compatibleWith: nil), for: .selected)
radioBtn.contentHorizontalAlignment = .leading
radioBtn.titleLabel?.adjustsFontSizeToFitWidth = true
radioBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 0)
radioBtn.contentEdgeInsets = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
radioBtn.heightAnchor.constraint(equalToConstant: 40).isActive = true
radioBtn.contentVerticalAlignment = .center
radioBtn.imageView?.contentMode = .scaleAspectFit

问题是 UIImage 延伸到整个宽度(蓝色部分)并且没有空间供您显示文本:

我想要完成的是完全在左边的收音机,然后是它旁边的文字和一些插图。如何实现?

【问题讨论】:

    标签: ios swift uibutton uiimageview uiimage


    【解决方案1】:

    首先,您的代码没有为您的按钮设置 Width。在这种情况下,按钮会将其 imageView 的宽度设置为图像的大小——使用您的pdf,最终为510 pts 宽。

    所以,有几个选择...

    1. 使用一些缩放代码来调整图像大小。如果您将按钮高度设置为 40,顶部和底部插入 8 点,则需要一个 24x24 图像。

    2. 为您的按钮设置一个宽度约束,并“即时”计算 imageView 插图。

    可能最简单的方法是使用UIButton 子类,例如:

    class RadioButton: UIButton {
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            commonInit()
        }
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            commonInit()
        }
        func commonInit() -> Void {
    
            contentHorizontalAlignment = .leading
    
            // 8-pts inset "padding" on all 4 sides
            contentEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
    
            // set title inset Left to content inset Left
            var insets: UIEdgeInsets = titleEdgeInsets
            insets.left = contentEdgeInsets.left
            titleEdgeInsets = insets
    
            // set images for .normal and .selected
            if let img = UIImage(named: "radio") {
                setImage(img, for: .normal)
            }
            
            if let img = UIImage(named: "radio_ticked") {
                setImage(img, for: .selected)
            }
            
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            // make sure an image was set (otherwise, there is no imageView)
            if let imgView = imageView {
                // get height of imageView
                let h = imgView.frame.height
                // get current (default) image edge insets
                var insets: UIEdgeInsets = imageEdgeInsets
                // set inset Right to width of self minus imageView Height + Left and Right content insets
                insets.right = bounds.width - (h + contentEdgeInsets.left + contentEdgeInsets.right)
                // update image edge insets
                imageEdgeInsets = insets
            }
        }
    }
    

    使用示例:

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let radioBtn = RadioButton()
            
            // background color so we can see its frame
            radioBtn.backgroundColor = .red
            radioBtn.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(radioBtn)
    
            // give it a 22-pt bold font
            radioBtn.titleLabel?.font = .boldSystemFont(ofSize: 22.0)
            
            // set the Title
            radioBtn.setTitle("Testing", for: [])
    
            // Height: 40
            radioBtn.heightAnchor.constraint(equalToConstant: 40).isActive = true
            // Width: 240
            radioBtn.widthAnchor.constraint(equalToConstant: 240.0).isActive = true
            // centered in view
            radioBtn.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            radioBtn.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    
        }
    }
    

    结果:

    【讨论】:

    • 感谢您的广泛回答,它有效!
    • 我刚刚注意到这在 iOS 12 上不起作用(并且可能更低)。计算出的边缘插图似乎不正确。
    猜你喜欢
    • 2012-09-28
    • 1970-01-01
    • 2020-02-03
    • 2019-04-12
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多