Code Monkey home page Code Monkey logo

nibdesignable's Introduction

Build Status Carthage compatible Pod version GitHub license

Nib Designable

Installation

CocoaPods

Add pod 'NibDesignable' to your Podfile

Carthage

Add github "mbogh/NibDesignable" to your Cartfile.

Manual

Simply drop NibDesignable.swift into your project and it is ready to use.

Usage

  1. Create a new class that subclass' NibDesignable like
class CustomView: NibDesignable {

}
  1. Create a nib, place a UIView and change the File's Owner to CustomView

    Correct:

    File's Owner is set to your custom class

    Incorrect:

    View class is set to your custom class

  2. Design your view and add @IBInspectable properties.

  3. Sometimes Xcode/Interface Builder does not recognize NibDesignable as @IBDesignable. Workaround Declare your custom class as @IBDesignable like:

@IBDesignable
class CustomView: NibDesignable {

}

Thanks

Contact

Follow me on Twitter (@mbogh) and/or visit my blog Just a Beech

License

Nib Designable is released under an MIT license. See LICENSE for more information.

Release Notes

Version 3.0.0

  • Swift 3.0 support, thanks to @sstadelman and @esetnik

Version 2.3.0

  • UICollectionReusableView support added by @ikesyo

Version 2.2.0

  • UICollectionViewCell support added by @pyankoff

Version 2.1.0

  • UIControl support added by @illaz

Version 2.0.0

  • Swift 2.0 for everyone, thanks to @bjarkehs

Version 1.1.1

  • Fixed minor issue with UITableViewCell

Version 1.1.0

  • Added support for UITableViewCells, thanks to @duemunk

Version 1.0.4

  • Swift 1.2 support, thanks to @ikesyo

Version 1.0.3

  • Switched to Auto Layout in setupNib(). This fixes issues when views have size of {0,0} initially.

Version 1.0.2

  • CocoaPods support

Version 1.0.1

  • proxyView is passe.
  • nibName() returns class name per default.

Version 1.0.0

  • Initial release

nibdesignable's People

Contributors

bhstrifork avatar buscarini avatar duemunk avatar esetnik avatar farzadshbfn avatar gabrielhauber avatar ikesyo avatar illaz avatar jachobsen avatar mbogh avatar naohta avatar pyankoff avatar sgtsquiggs avatar sstadelman avatar tingraldi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nibdesignable's Issues

Subclassing other base views

There are many different types of views that could be subclassed to make NibDesignable versions of them. The framework already includes many of them (UIView, UIControl, UITableViewCell, UITableViewHeaderFooterView, UICollectionViewCell, and UICollectionReusableView), but users may always have proprietary classes that may benefit from being subclassed.

Subclassing views within the framework is made trivial by the abstraction of NibDesignableProtocol and it's default implementation. However, the scoping of -setupNib() makes it difficult to do outside of that file. If the scope of that method were expanded to public (instead of fileprivate) it would greatly ease this task. Are there any obvious down sides to doing this?

Xib is not loaded

Hi,

thank you for your awesome view.

I did managed to make your library working once but is is not working anymore.

I set the file owner to my custom view
I let the main UIView as an UIView
I named the xib to the same name as the class.

The only thing I changed is that I used custom live rendering view inside my xib (Those views don't use xib file so it renders well in the xib).

Do you have any idea ?

Here is the code :

import UIKit

class HeaderFicheClientView: NibDesignable {

@IBOutlet weak var nameCustomer: UILabel!

@IBInspectable internal var nameText:String = "Nathalie Beaudry"{
    willSet{
        nameCustomer.text = nameText
    }
}

@IBInspectable internal var borderColor:UIColor = UIColor.blackColor(){
    willSet{
        self.layer.borderColor = borderColor.CGColor
        self.layer.borderWidth = 1
    }
}

}

init(coder aDecoder:NSCoder) crashing with EXC_BAD_ACCESS(code=2) on XCode 6.4

My custom view from NibDesignable is crashing on line 64 of NibDesignable.swift

    // MARK: - NSCoding
    required public init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)  // line 64
        self.setupNib()
    }

with EXC_BAD_ACCESS(code=2) when the view gets instantiated at run time. Placing breakpoints on the line suggests an infinite loop of sorts where the initializer gets called over and over again.

I am currently debugging in a bare project and have also stripped my custom class to a bare skeleton as:

import UIKit
import NibDesignable

class CustomView: NibDesignable {
}

Any ideas?

There is build error for Swift 4

Swift doesn't allow overriding a method in an extension. The fastest way to fix is adding @objc to the method:

extension UIView {
    @objc public var nibContainerView: UIView {
        return self
    }
    /**
     Called in the default implementation of loadNib(). Default is class name.

     - returns: Name of a single view nib file.
     */
    open func nibName() -> String {
        return type(of: self).description().components(separatedBy: ".").last!
    }
}

I don't have permission to make a PR, so please help release new version including that update.

Thanks & Best Regards,

NibDesignableTableViewCell not working with UITableView Auto Cell height

It seems that using NibDesignableTableViewCells does not work when using automatic cell height on the tableView. In my project I have a tableviewclass, subclass of NibDesignableTableViewCell, all set up hunky dory. The UI shows up (which is great), but it is squished (not so great). At first I thought this was due to using StackViews, but this was not the case (I have a very simple tableViewCell with just a label inside). I attached an image. The first cell is a nib designable cell (and it is squished, the ones below it ended up having a similar layout until I stopped them being a nib designable. My guess is that the issue comes when adding the view to the content view (lines 101-107) and how automatic heights are calculated.
simulator screen shot oct 23 2015 8 47 14 am

Nested Nibs, Speed of instantiateWithOwner

I have a fairly complex view hierarchy in a UITableViewCell (among other things) that uses nested NibDesignables. On slower devices the load time is very noticeable. For example, with 2 NibDesignables each with 5 subviews that are NibDesignable, it takes approximately 2 seconds on the main thread. Profiler doesn't show a large CPU burden, though.

Thoughts? Are there any easy fixes to make this kind of hierarchy instantiate faster? If I can't fix this then I'm afraid I'll be forced back to writing all of my views/autolayout programmatically. :-(

Designable UITableViewCell

Hello everyone.

I'm struggling with @IBDesignable UITableViewCells.

When I create a tableViewCell, it 's possible to change the background color of the cell to red:

screen shot 2017-03-27 at 15 54 11
screen shot 2017-03-27 at 15 54 18

But I can't see my label, and I can't update the text of the label.

Could someone make an example for at custom table view cell with a label, or explain how to update my custom designable table view cell?

Thank you!

FilesOwner as CustomView

Some developers take issue with setting FilesOwner to a UIView subclass. Is there any way around this to make NibDesignable more palatable to purists?

connecting to IBOutlet from a NibDesignable subclass

First i must say that the NibDesignable class is awesome.

I've created a subclass called LineField which contains a UITextField with a line under it.
when I plug in my LineField into my Storyboard, I wanted to be able to connect an IBOutlet on my UIViewController to the LineField's text field. but unfortunately that doesn't seem possible.
Is there a way to do that which I'm not aware of ?

@IBDesignable
public class LineField: NibDesignable {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var lineView: UIView!

    @IBInspectable public var lineColor: UIColor = UIColor.lightGrayColor() {
        didSet {
            self.lineView.backgroundColor = lineColor
        }
    }

    override public func awakeFromNib() {
        super.awakeFromNib()
    }

}

My ViewController:

class TextQuestionEditVC: UIViewController {

    @IBOutlet weak var saveButton: UIBarButtonItem!
    @IBOutlet weak var questionLineField: LineField!
    @IBOutlet weak var searchField: UITextField!

}

TLDR: how do i connect my searchField to the LineField->textField ?

Autolayout Constraints

I am not sure if this issue is because of a tableviewcell or something I missed in the documentation. I can't seem to get a TVC to work in a Nib file once I add AutoLayoutConstraints.

Support ObjC

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.

IBDesignable error

I imported the file NibDesignable in my project, followed the readme guidelines, but I get an error when the views are refreshed:

error: IB Designables: Failed to render and update auto layout status for ViewController (BYZ-38-t0r): The agent threw an exception.

I send my project as attachment.
ViewFromXib.zip

Infinite loop when loading my view

I have a UITableViewCell that contains two UIView both extending NibDesignable.

In each of those view I have an initializer like the following.

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

This init triggers

// MARK: - NSCoding
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setupNib()
    }

of NibDesignable.
Then everytime this line gets called

```return nib.instantiate(withOwner: self, options: nil)[0] as! UIView // swiftlint:disable:this force_castit calls myrequired init?


 and I end up having an infinite loop.

I used the same type of initialisation in another view (not in a table view cell) and it works fine.

Any idea what's wrong?

Thanks

Call for contributors

Hey @mbogh, how about adding more maintainers to the project? You've collected nearly 200 stars, now would be a great time to add more people to ensure it's development doesn't stale. Maybe @farzadshbfn, @ggrana or @Jachobsen are interested?

Would love to see this lib being actively maintained!

How does it work with UITableViewCell

I have a UITableViewCell that I would like to make Designable. How can I do?

Because as soon as I extend NibDesignable, it obviously stops being a UITableViewCell.

ProfileAvatarView stays blank in demo project

I downloaded the demo project and run it. On the device/simulator it works fin but when I open the Storyboard, the view stays blank.

In the identity inspector, Designables is always Updating.

Does it happen to someone else?

I haven't touched anything on the project.

Improper layout

I found a bug where if a view inherits from NibDesignable, layoutSubviews gets called with the original sizes from the nibs but not for subsequent resizes. I noticed it when I was making a circular image view and despite doing self.imageView.layer.cornerRadius = self.imageView.frame.size.width /2 in layoutSubviews, the image was rounded strangely, even when it had a 1:1 aspect ratio using autolayout.

My fix was simply to add self.layoutIfNeeded to awakeFromNib but I suspect the issue goes deeper than that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.