Xcode: Can I create reusable stack views?

Is it possible to create reusable stack views in the story panel, which can be used dynamically to create later? Type of template / widget / component.

I know that I can do this with a class, but if I can visually create a set of components that can be reused for the last time, I can let our designers directly make changes to the storyboards.

+4
source share
2 answers

Yes - you can do this with any UIView. There are many tutorials for this (e.g. http://onedayitwillmake.com/blog/2013/07/ios-creating-reusable-uiviews-with-storyboard/ )

The main idea is to drag it onto a Storyboard or XIB, create your own class for it, and then implement a view awakeFromNibto load it.

+2
source

Yes. This is true.

  • Create an empty xib, and then add a stack view to it.
  • Then create a class that extends UIStackView.

    class stackView: UIStackView {
    
    var contentView : UIStackView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }
    
    required init(coder: NSCoder) {
        super.init(coder: coder)
        xibSetup()    }
    
    func xibSetup() {
        contentView = loadViewFromNib() 
        contentView.frame = bounds
        contentView.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        addSubview(contentView)
    }
    
    func loadViewFromNib() -> UIStackView! {
    
        let view: UIStackView? = Bundle.main.loadNibNamed("stackView", owner: nil, options: nil)?.first as! UIStackView?
        return view
    }
    
  • Create a viewController.Add stackView for it. In the properties of the StackView, goto 3rd bar, which is named as a custom class, give the class name stackView for the class name

+2
source

All Articles