无法在 UIStackView 中设置按钮图像
Cannot set button image inside UIStackView
我有一个 UICollectionViewCell
,其中有一个 UIStackView
填满了整个单元格。
在 UIStackView
里面,我正在尝试添加 subviews
但我 运行 遇到了两个问题。
第一个是我看不到 UIStackView
添加到 cell
,即使将堆栈视图背景颜色设置为它不显示的不同颜色。
第二个问题是,当我将按钮图像实例化为 button.setImage(:_)
时,我的应用程序 - 在本例中为 Today Extension Widget
崩溃
代码如下:
@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
错误,如下图所示。
有人发现这里的问题吗?
由于这是 Today Extension Widget
的一部分,请确保您已将图像包含在该构建目标中。
未来的调试提示:当您看到 "really shouldn't happen," 的错误时,请尝试逐步思考。在这种情况下,虽然 .setImage
行似乎是罪魁祸首,但如果您检查了 let image =
行的有效性,您可能会节省一点时间。
我有一个 UICollectionViewCell
,其中有一个 UIStackView
填满了整个单元格。
在 UIStackView
里面,我正在尝试添加 subviews
但我 运行 遇到了两个问题。
第一个是我看不到 UIStackView
添加到 cell
,即使将堆栈视图背景颜色设置为它不显示的不同颜色。
第二个问题是,当我将按钮图像实例化为 button.setImage(:_)
Today Extension Widget
崩溃
代码如下:
@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
错误,如下图所示。
有人发现这里的问题吗?
由于这是 Today Extension Widget
的一部分,请确保您已将图像包含在该构建目标中。
未来的调试提示:当您看到 "really shouldn't happen," 的错误时,请尝试逐步思考。在这种情况下,虽然 .setImage
行似乎是罪魁祸首,但如果您检查了 let image =
行的有效性,您可能会节省一点时间。