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

Support ObjC #35

Open
sebastianludwig opened this issue Jun 15, 2016 · 3 comments
Open

Support ObjC #35

sebastianludwig opened this issue Jun 15, 2016 · 3 comments

Comments

@sebastianludwig
Copy link

While it's debatable how long ObjC is going to be around, it's certain that it'll be with us for the next year, very very likely much longer. Because in many projects one doesn't have a free choice which language to use, it'd be great if NibDesignable would support Swift and ObjC. At the moment NibDesignable requires subclassing which is only supported within Swift and from an ObjC baseclass to a Swift subclass, not the other way round, wich makes it impossible to use NibDesignable in ObjC projects.

It'd be great to either find another way how NibDesignable could work that does not require subclassing or port the library to ObjC. I guess the latter is a bit unpopular, but it's the right move in my opinion. I'd volunteer to do the work.

@mbogh
Copy link
Owner

mbogh commented Jun 15, 2016

We could make NibDesignable protocol and then require that you call the setup method.

If a protocol it might also be possible to do default implementations of UIView inits, would be worth investigating.

@mbogh
Copy link
Owner

mbogh commented Jun 15, 2016

import UIKit

public protocol NibDesignable {
    var nibContainerView: UIView { get }
    func loadNib() -> UIView
    func nibName() -> String

    func setupNib()
}

extension NibDesignable {
    func loadNib() -> UIView {
        return UIView()
    }

    func nibName() -> String {
        return String(self)
    }

    func setupNib() {
        let view = self.loadNib()
        self.nibContainerView.addSubview(view)
        view.translatesAutoresizingMaskIntoConstraints = false
        let bindings = ["view": view]
        self.nibContainerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options:[], metrics:nil, views: bindings))
        self.nibContainerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options:[], metrics:nil, views: bindings))
    }
}

extension NibDesignable where Self: UIView {
    var nibContainerView: UIView {
        return self
    }
}

class MyView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupNib()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupNib()
    }
}

extension MyView: NibDesignable {}



let view = MyView()

@mbogh
Copy link
Owner

mbogh commented Jun 15, 2016

Short term could also be to add _ObjectiveCBridgeable (NSHipster Reader submissions)

Update this is a no go :)

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

2 participants