IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable

Reference: Chapter’s book “Intermediate iOS Programming with Swift” on appcode website 36

Example code: https://github.com/andyRon/LearniOSByProject/tree/master/77

Understanding IBInspectable and IBDesignable

Simply put, IBInspectable allows developers to add additional options to the IB property inspector. IBDesignable allows the changes to additional options added by * IBInspectable* to be displayed in real-time in IB. Take the fillet of Button as an example.

  • Normally, Button’s fillet needs code:
Button.layer.cornerRadius = 10, button.layer.masksToBounds = true

Or add it directly to the user defined runtime attributes in the Identity inspector of IB:

IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable

Notice that this modification is not real-time; in IB, Button is right angled and rounded only after running.

  • In the IBInspectable manner. Define a class that inherits to UIButton.
Class RoundedCornerButton: UIButton @IBInspectable var cornerRadius: {CGFloat = 0 {{didSet layer.cornerRadius = cornerRadius layer.masksToBounds = cornerRadius > 0;}}}

Then modify the class property of button to RoundedCornerButton:

IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable

Found that the button property inspector has more than Corner Radius.

Note: the cornerRadius attribute to Corner Radius, RoundedCornerButton Corner became Rounded Button, this is Xcode automatic change, developers need only in accordance with the specification of swift named it (the class name is capitalized hump naming rules, attribute names are written at the beginning of the hump naming rules)

IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable

The type of cornerRadius is CGFloat, and the corresponding number is selected in the property inspector. Of course, not all types can be added. In the property inspector, IBInspectable supports the following type:

Int, CGFloat, Double, String, Bool, CGPoint, CGSize, CGRect, UIImage, UIColor

If you add @IBDesignable before class RoundedCornerButton, the property changes defined in the property inspector can be displayed in real-time in IB.

@IBDesignable class RoundedCornerButton: UIButton @IBInspectable var cornerRadius: {CGFloat = 0 {{didSet layer.cornerRadius = cornerRadius layer.masksToBounds = cornerRadius > 0;}}}
IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable

Create Fancy Button

Create Fancy Button to gain a better understanding of IBInspectable and IBDesignable

  • Create new project FancyButton
  • Download icons, or optionally icons, drag into asset catalog
  • New class FancyButton, inherited to UIButton
  • Fillet, edge width, edge color. Update FancyButton:@IBDesignable class FancyButton: UIButton @IBInspectable var cornerRadius: {CGFloat = 0 {{didSet layer.cornerRadius = cornerRadius layer.masksToBounds = cornerRadius > @IBInspectable var borderWidth: CGFloat 0}} = 0 {{layer.borderWidth}} = didSet borderWidth @IBInspectable var borderColor: UIColor.Black didSet = {borderColor.cgColor}} = {layer.borderColor}
IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable
  • Title
    continues to add attributes in FancyButton: @IBInspectable var titleLeftPadding: {CGFloat = 0}} {didSet titleEdgeInsets.left = titleLeftPadding @IBInspectable var titleRightPadding: CGFloat = 0 {{titleEdgeInsets.right}} = didSet titleRightPadding @IBInspectable var titleTopPadding: CGFloat didSet {titleEdgeInsets.top = 0 = {titleTopPadding}} @IBInspectable {var titleBottomPadding: CGFloat = 0 didSet titleEdgeInsets.bottom = {titleBottomPadding}}
IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable
  • Round button can be created with rounded corners
IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable
  • The Image Padding
    attribute add picture: @IBInspectable var imageLeftPadding: CGFloat margin = 0 {{imageEdgeInsets.left}} = didSet imageLeftPadding @IBInspectable var imageRightPadding: CGFloat = 0 didSet imageEdgeInsets.right = {imageRightPadding} {@IBInspectable} var {didSet {imageTopPadding: CGFloat = 0 imageEdgeInsets.top = imageTopPadding @IBInspectable var imageBottomPadding: CGFloat}} = 0 {didSet {imageEdgeInsets.bottom} = imageBottomPadding}
IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable
  • According to the relationship between the picture right aligned
    , should be imageEdgeInsets.left = self.bounds.width – imageView.bounds.width – imageRightPadding.
IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable

Add the following code in FancyButton:

@IBInspectable var enableImageRightAligned: Bool override func (layoutSubviews = false) {super.layoutSubviews (if) enableImageRightAligned, let imageView = imageView imageEdgeInsets.left = {self.bounds.width}} - imageView.bounds.width - imageRightPadding
The + `enableImageRightAligned` attribute automatically calculates the `imageEdgeInsets.left`
IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable
  • The color gradient
    added three @IBInspectable properties, @IBInspectable var enableGradientBackground: Bool layoutSubviews: and update @IBInspectable var gradientColor1: = false UIColor = UIColor.black @IBInspectable var gradientColor2: UIColor override func (layoutSubviews = UIColor.white) {super.layoutSubviews (if) enableImageRightAligned, let imageView = imageView {imageEdgeInsets.left = self.bounds.width – imageView.bounds.width – imageRightPadding if enableGradientBackground {let} = CAGradientLayer (gradientLayer) gradientLayer.frame = self.bounds gradientLayer.colors = [gradientColor1.cgColor gradientColor2.cgColor] gradientLayer .startPoint = CGPoint (x: 0, y: 0.5), gradientLayer.endPoint = CGPoint (x: 1, y: 0.5), self.layer.insertSublayer (gradientLayer, at:, 0)}
IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable

The gradient color needs to be run before you can see it

IOS tutorial 8: Customizing UI using IBInspectable and IBDesignable