Skip to content

Interface Builder Usage #2

@tooolbox

Description

@tooolbox

Amazing product, thank you for your contribution to the world of tvOS 👍

One thing that took me a little bit to figure out, might be good to put it in your docs or something, but this is an example of how you would make this work in Interface Builder:

// Stub container
class SimpleParallaxContainer: UIView, ParallaxContainer {
    func focusChanged(_ focus: ParallaxFocusState) {}
    func parallaxShadow(forFocus: ParallaxFocusState, defaultShadow: Shadow) -> Shadow? {
        return defaultShadow
    }
}

// Because ParallaxView doesn't implement initWith(coder:) you can't place it in Interface Builder.
// This UIView subclass substitutes itself with a ParallaxView at runtime.
class ParallaxPlaceholder: UIView {
    override func layoutSubviews() {
        super.layoutSubviews()
        guard let sup = self.superview else { return } // make sure we have a superview
        
        // Create a container
        let cont = SimpleParallaxContainer.init(frame: self.bounds)
        
        // Move all of the placeholder's subviews into the container
        self.subviews.forEach { (view) in
            let f = view.frame
            view.removeFromSuperview()
            cont.addSubview(view)
            view.frame = f
        }
        
        // Create a parallax view, using the container we just built
        let pView = ParallaxView<SimpleParallaxContainer>(layerContainer: cont)
        
        // Remove ourselves from the view hierarchy and replace with the parallax view
        sup.insertSubview(pView, aboveSubview: self)
        pView.frame = self.frame
        self.removeFromSuperview()
    }
}

Definitely no problem if I'm going about this wrong and you feel like pointing it out. Either way, thanks again!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions