gpt4 book ai didi

ios - 无法在 UIStackView 中设置按钮图像

转载 作者:行者123 更新时间:2023-11-29 00:11:09 25 4
gpt4 key购买 nike

我有一个 UICollectionViewCell,它有一个填充整个单元格的 UIStackView

UIStackView 中,我尝试添加 subview ,但我在这里遇到了两个问题。

第一个是我看不到 UIStackView 被添加到 cell,即使将堆栈 View 背景颜色设置为它不显示的不同颜色。

第二个问题是我的应用程序 - 在本例中是 Today Extension Widget 当我将按钮图像实例化为 button.setImage(:_)

代码如下:

@objc lazy var composeButton: UIButton = {
let button = UIButton(type: .system)
let image = #imageLiteral(resourceName: "compose_icon").withRenderingMode(.alwaysOriginal)
button.setImage(image, for: .normal)
button.addTarget(self, action: #selector(didTapCompose), for: .touchUpInside)
return button
}()

let stackview = UIStackView(arrangedSubviews: [composeButton, reloadButton])
stackview.distribution = .fillEqually
stackview.axis = .horizontal
addSubview(stackview)
stackview.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddinfLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)



extension UIView {
/// Anchor a specific view to a superview
@objc func anchor(top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddinfLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat) {
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
}
if let left = left {
leftAnchor.constraint(equalTo: left, constant: paddinfLeft).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true
}
if let right = right {
rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
}
if width != 0 {
widthAnchor.constraint(equalToConstant: width).isActive = true
}
if height != 0 {
heightAnchor.constraint(equalToConstant: height).isActive = true
}
}

/// Rounds the corners of a UIView object
/// parameters: corners - the corners to round (upperLeft, upperRight, lowerLeft, lowerRight);
/// radiud - the desired cornerRadius to apply to the desired corners
@objc func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}

应用程序在此行崩溃

button.setImage(image, for: .normal)

它崩溃并出现 SIGABRT 错误,如下图所示。

有人发现这里的问题吗?

SIGABRT

最佳答案

因为这是 Today Extension Widget 的一部分,请确保您已将图像包含在该构建目标中。

future 的调试提示:当您看到“确实不应该发生”的错误时,请尝试逐步考虑。在这种情况下,虽然 .setImage 行似乎是罪魁祸首,但如果您检查了 let image = 行的有效性,您可能会节省一点时间。

关于ios - 无法在 UIStackView 中设置按钮图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46431166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com