XibView 0.1.4

XibView 0.1.4

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jul 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by James Kauten.



XibView 0.1.4

XibView

Icon

This pod aims to simplify the process of building custom IBDesignables by reducing boilerplate and hiding the ugly interface builder details from what should be concise, and reusable subclasses of UIView.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS8.0

Installation

XibView is available through CocoaPods. To install it, add the following line to your Podfile:

pod "XibView"

Workflow

The basic workflow for creating a custom IBDesignable looks like this.

  1. start with stubbing your UIView subclass
import XibView

@IBDesignable class DummyView: XibView {

}

you’ll notice that instead of subclassing UIView, we have subclassed XibView instead. What this does is associate this class directly with a xib file of the same name. Don’t worry, XibView is a subclass of UIView.

  • dont forget the @IBDesignable flag. without it, the interface builder wont know that this is a view to build.
  1. Create the xib file

Next create the .xib file that you will design your custom view in, it’s important that the xib filename match the class that it will be owned by. This is how the XibView machinery automatically finds its xib file based on the subclass name. In our example case the file would be called DummyView.xib

  1. Set the owner of the nib file to the class you stubbed.

File Owner Navigator

File Owner Class

  • the view inside the xib should not be subclassed by the custom class
  1. Set up the view dimensions

Select View

Set Dimensions

  1. Design your view

Design

  1. Hookup IBOutlets, IBActions, write IBInspectables, etc.
import XibView

/// an example of the XibView subclassing pattern
@IBDesignable class DummyView: XibView {

    /// some segmented control
    @IBOutlet weak var segmentedControl: UISegmentedControl!

    /// some button
    @IBOutlet weak var button: UIButton!

    /// the background color of some button, can be changed
    /// in the interface builder
    @IBInspectable var buttonBackgroundColor: UIColor? {
        get {
            return button.backgroundColor
        }
        set {
            button.backgroundColor = newValue
        }
    }

    /// the top switch
    @IBOutlet weak var switchTop: UISwitch!

    /// the bottom switch
    @IBOutlet weak var switchBottom: UISwitch!

}
  1. Use your custom view in another view or storyboard or whatever!

Using View

PodXibView

The way CocoaPods handles resource bundles for views is a little funky compared to the way it’s done in a single target application. PodXibView is here to fix that problem for all the UI building CocoaHeads out there. All the steps are the same, plus the extra final steps below.

  1. Determine the name of the resource bundle that your IBDesignable subclasses xib is located in.

this information can be found in your podspec and usually looks something like this.

s.resource_bundles = {
   'SomeBundleName' => ['YourCocoaPodProjectDir/Classes/**/*.{storyboard,xib}']
}
  1. Conform your subclass to the PodXibViewSubclassing protocol and implement the required function returning the name of the bundle you got in step 7.
    • make sure the access control level for the class is either public or open if this class should be accessible when users install the pod
    • check that properties also have the appropriate access control level, or else they wont be available outside of the context of your framework. Again public, or open.

Naturally Apple has some pretty thorough documenation surrounding access control. If you’re new to frameworks, or wonder whether public or open is more applicable for your use case read this (should probably read it if you haven’t anyway):

Apple Access Control Docs

import XibView

/// An example IBDesignable for use within a published cocoapod
@IBDesignable public class DummyView: PodXibView, PodXibViewSubclassing {

    /// Return the name of the bundle that the xib for this class
    /// is located in.
    /// - returns: the name of the bundle that this view lives in
    func getBundleName() -> String {
        return "SomeBundleName"
    }
}
  1. Use your view in another project by installing it with CocoaPods!

Author

kautenja, [email protected]

License

XibView is available under the MIT license. See the LICENSE file for more info.