Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interface Builder Usage #2

Open
tooolbox opened this issue Jan 31, 2018 · 0 comments
Open

Interface Builder Usage #2

tooolbox opened this issue Jan 31, 2018 · 0 comments

Comments

@tooolbox
Copy link

tooolbox commented Jan 31, 2018

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant