【问题标题】:Resizing image inside of UIButton在 UIButton 内调整图像大小
【发布时间】:2021-05-10 19:03:22
【问题描述】:

我一直在尝试获取放置在 UIButton 内的系统图像以缩放比例,但我在网上找到的解决方案似乎都没有。这是我迄今为止尝试过的:

//the cells objects
var acceptButton = UIButton(type: .custom)
var declineButton = UIButton(type: .custom)


override init(frame: CGRect) {
    super.init(frame: frame)
    
    //add the contents and set its autolayout constraints
    setAutoLayoutConstraints()
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

//function to set the autolayout constraints for the cell's contents
func setAutoLayoutConstraints() {
    
    //create the stack view which will contain the accept and reject buttons
    let buttonStackView = UIStackView()
    buttonStackView.axis = .horizontal
    buttonStackView.distribution = .fillEqually
    buttonStackView.alignment = .fill
    
    //add the buttons to the stack view
    buttonStackView.addArrangedSubview(acceptButton)
    buttonStackView.addArrangedSubview(declineButton)
    
    //add the button stack view to the cell
    self.addSubview(buttonStackView)
    buttonStackView.translatesAutoresizingMaskIntoConstraints = false
    buttonStackView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    buttonStackView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
    buttonStackView.leadingAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    buttonStackView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
    
    //add the button images
    let addImage = UIImage(systemName: "checkmark.circle")
    acceptButton.imageView?.contentMode = .scaleAspectFit
    acceptButton.setImage(addImage, for: .normal)
    acceptButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    acceptButton.addTarget(self, action: #selector(addButtonClicked), for: .touchUpInside)
    
    let declineImage = UIImage(systemName: "xmark.octagon")
    declineButton.imageView?.contentMode = .scaleAspectFit
    declineButton.setImage(declineImage, for: .normal)
    declineButton.addTarget(self, action: #selector(declineButtonClicked), for: .touchUpInside)
    
                    
}

这是我得到的:

(“test test”标签是我为左半部分单元格内容添加的,可以忽略)

我尝试了多种解决方案,例如设置边缘插图、设置按钮的内容模式和按钮的图像视图、使用自定义按钮等,但我似乎无法调整图像大小。

注意:当我使用 setBackgroundImage 将图像设置为背景图像时,我可以调整按钮的大小,但无法缩放适合它的纵横比,只能水平和垂直填充它,这不是我想要做的。

感谢任何帮助,谢谢!

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

    这是一个简单的解决方案示例:首先添加您的 labelTest,然后将 stackView 前导锚设置为 labelTest 尾随... 这是我的表格视图:

    class YourViewController: UIViewController, UITabBarDelegate, UITableViewDataSource, UITableViewDelegate {
    
    
    let tableView = UITableView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(myCell.self, forCellReuseIdentifier: "cellId")
        tableView.backgroundColor = .white
        tableView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(tableView)
        tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! myCell
        
        return cell
     }
    }
    

    现在设置我的单元格,并设置内容视图的约束(这是正确的方法,而不是直接设置单元格):

    class myCell: UITableViewCell {
    
    let labelTest: UILabel = {
        let label = UILabel()
        label.text = "test Label"
        label.backgroundColor = .red
        label.translatesAutoresizingMaskIntoConstraints = false
        
        return label
    }()
    
    var acceptButton = UIButton(type: .custom)
    var declineButton = UIButton(type: .custom)
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        acceptButton.backgroundColor = .purple
        declineButton.backgroundColor = .brown
        setAutoLayoutConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //function to set the autolayout constraints for the cell's contents
    func setAutoLayoutConstraints() {
        
        contentView.addSubview(labelTest)
        labelTest.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        labelTest.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
        labelTest.trailingAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
        labelTest.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
        
        let buttonStackView = UIStackView(arrangedSubviews: [acceptButton, declineButton])
        buttonStackView.axis = .horizontal
        buttonStackView.distribution = .fillEqually
        buttonStackView.translatesAutoresizingMaskIntoConstraints = false
        
        //add the button stack view to the cell
        contentView.addSubview(buttonStackView)
        buttonStackView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        buttonStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
        buttonStackView.leadingAnchor.constraint(equalTo: labelTest.trailingAnchor).isActive = true
        buttonStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
        
        //add the buttons images
        let addImage = UIImage(systemName: "checkmark.circle")
        acceptButton.imageView?.contentMode = .scaleAspectFit
        acceptButton.setImage(addImage, for: .normal)
        acceptButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        acceptButton.addTarget(self, action: #selector(addButtonClicked), for: .touchUpInside)
        
        let declineImage = UIImage(systemName: "xmark.octagon")
        declineButton.imageView?.contentMode = .scaleAspectFit
        declineButton.setImage(declineImage, for: .normal)
        declineButton.addTarget(self, action: #selector(declineButtonClicked), for: .touchUpInside)               
     }
    }
    

    如果你想在左/右留一些空间,只需在相对约束中添加常量:

    labelTest.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 10).isActive = true
    buttonStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -10).isActive = true
    

    这是结果

    【讨论】:

      猜你喜欢
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 2012-05-02
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      相关资源
      最近更新 更多