Xcode 14 IBDesignable preview crashes, however IBInspectable properties appear and work as expected

111 Views Asked by At

I am making a reusable view to draw a playing card. The reusable view (which consists of an xib and swift files). The screen with this reusable view build & runs on the simulator without any issues, however in the XIB file, the preview doesn't work. In attributes inspector it says "Designables: Error", and inside the content view, where I want to preview the reusable view, it says "NameOfMyReusableView ‼️Designable".

ContentView Attributes Inspector

Here's the reusable view code:

@IBDesignable class PlayingCardView: UIView {
    @IBOutlet var contentView: UIView!
    @IBOutlet private weak var cardBackground: UIImageView!
    @IBOutlet private weak var topLeadingLabel: UILabel!
    @IBOutlet private weak var bottomTrailingLabel: UILabel!
    
    @IBInspectable var rank: String = "" {
        didSet {
            topLeadingLabel.text = rank
            bottomTrailingLabel.text = rank
        }
    }
    
    @IBInspectable var suit: String = "" {
        didSet {
            topLeadingLabel.text?.append("\n\(suit)")
            bottomTrailingLabel.text?.append("\n\(suit)")
        }
    }
    
    
    private var isUpsideDown: Bool = false {
        didSet {
            if isUpsideDown {
                topLeadingLabel.alpha = 0
                bottomTrailingLabel.alpha = 0
                cardBackground.alpha = 1
            } else {
                topLeadingLabel.alpha = 1
                bottomTrailingLabel.alpha = 1
                cardBackground.alpha = 0
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        loadNibFile()
        setup()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        loadNibFile()
        setup()
    }
    
    func setup() {
        isUpsideDown = false
        contentView.layer.cornerRadius = 16
        
        topLeadingLabel.text = "5\n❤️"
        bottomTrailingLabel.text = "5\n❤️"
        bottomTrailingLabel.transform = CGAffineTransform.identity.rotated(by: CGFloat.pi)
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(flipCard(_:)))
        contentView.addGestureRecognizer(tapGesture)
    }
    
    private func loadNibFile() {
        Bundle.main.loadNibNamed("PlayingCardView", owner: self)
        addSubview(contentView)
        contentView.frame = self.bounds
        contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        contentView.clipsToBounds = true
    }
    
    @objc private func flipCard(_ sender: UITapGestureRecognizer) {
        isUpsideDown.toggle()
    }
}

I tried clean build, but obviously it didn't help.

0

There are 0 best solutions below