简体   繁体   中英

No preview of custom UIStackView in Storyboard despite using IBDesignable

I don't know what's causing the problem but I can't see the preview of my custom element in .storyboard file. When I run it on the device it is here and works fine (looks the same as my .xib file), but in the Storyboard it's just a blank space.

在此处输入图片说明

Moreover there are two erros. I've searched all over the internet but couldn't make them disappear.

在此处输入图片说明

My code:

ReusableStackView.swift:

import UIKit

@IBDesignable
class ReusableStackView: UIStackView {

    @IBOutlet var stackView: UIStackView!
    @IBOutlet weak var stackButton: UIButton!
    @IBOutlet weak var stackLabel: UITextField!


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

    required init(coder: NSCoder) {
        super.init(coder: coder)
        stackViewSetUp()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        stackViewSetUp()
        stackView.prepareForInterfaceBuilder()
    }

    func stackViewSetUp(){
        Bundle.main.loadNibNamed("ReusableView", owner: self, options: nil)
        stackView.frame = self.bounds
        stackView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        addSubview(stackView)

    }
}

My ViewController does not have any logic connected to the ReusableStackView.

I fixed it: Instead of addSubview(stackView) add add addArrangedSubview(stackView) . Now the preview appears in the IB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM