mbogh / NibDesignable

Elegant way of enabling IBDesignable on your nib-based views
MIT License
342 stars 74 forks source link

Support ObjC #35

Open sebastianludwig opened 8 years ago

sebastianludwig commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago
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 commented 8 years ago

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

Update this is a no go :)