【问题标题】:IBDesignable class drawing UIImage not displaying in storyboardIBDesignable 类绘图 UIImage 未显示在情节提要中
【发布时间】:2017-12-08 08:08:03
【问题描述】:

我一直在 XCode 中设计一个自定义控制器,它根据自己所处的状态进行绘制。在一种状态下,控制器通过在覆盖的 draw(_ rect: CGRect) 函数中调用 myImage?.draw(in: rect) 来显示图像。

图像在应用程序中正确显示,但未在情节提要中显示。为了说明这一点,我在同一个绘图函数中添加了一个贝塞尔环,它同时显示在应用和情节提要中:

Controller as displayed in app

Controller as displayed in storyboard

在调查了这个问题之后,我认为这可能与图片是可选的这一事实有关。但是,显式展开它(即调用 myImage!.draw(in: rect) 会导致情节提要自动布局崩溃:

error: IB Designables: Failed to render and update auto layout status for SermonViewController (1zk-bD-8it): The agent crashed

我已经剥离了类的完整实现:​​

import UIKit

@IBDesignable class DownloadIndicatorControl: UIView {

    let requestDownloadImage = UIImage(named: "requestDownloadImage")
    let lineWidth: CGFloat = 3.0
    override func draw(_ rect: CGRect) {

        // Draw Image
        requestDownloadImage?.draw(in: rect)

        // Draw ring around image
        let radius: CGFloat = fmin(self.bounds.width, self.bounds.height) / 2 - lineWidth / 2
        let center: CGPoint = CGPoint(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)
        let disk: UIBezierPath = UIBezierPath()
        disk.lineWidth = lineWidth
        disk.addArc(withCenter: center,
                    radius: radius,
                    startAngle: CGFloat(-Double.pi / 2),
                    endAngle: CGFloat(2 * Double.pi - Double.pi/2),
                    clockwise: true)
        disk.stroke()
    }

    override init(frame: CGRect){
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

我可以做些什么来使图像显示在情节提要中,有什么方法可以明确地解开图像而不会使情节提要不断崩溃?

【问题讨论】:

    标签: swift xcode cocoa storyboard


    【解决方案1】:

    UIImage(named:) 方法使用主包,但 Interface Builder 以不同的方式加载资源。

    试试这个:

    UIImage(named:in:compatibleWith:)

    import UIKit
    
    @IBDesignable class DownloadIndicatorControl: UIView {
    
    var requestDownloadImage: UIImage?
    let lineWidth: CGFloat = 3.0
    
    override func draw(_ rect: CGRect) {
    
        // Draw Image
        requestDownloadImage?.draw(in: rect)
    
        // Draw ring around image
        let radius: CGFloat = fmin(self.bounds.width, self.bounds.height) / 2 - lineWidth / 2
        let center: CGPoint = CGPoint(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)
        let disk: UIBezierPath = UIBezierPath()
        disk.lineWidth = lineWidth
        disk.addArc(withCenter: center,
                    radius: radius,
                    startAngle: CGFloat(-Double.pi / 2),
                    endAngle: CGFloat(2 * Double.pi - Double.pi/2),
                    clockwise: true)
        disk.stroke()
    }
    
    override init(frame: CGRect){
        super.init(frame: frame)
        self.setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setup()
    }
    
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        self.setup()
    }
    
    internal func setup() {
        let bundle = Bundle(for: DownloadIndicatorControl.self)
        requestDownloadImage = UIImage(named: "download", in: bundle, compatibleWith: nil)
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-29
      • 2015-01-02
      • 2019-09-15
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多