简体   繁体   中英

Errors with rendering custom UITextField subclass in Interface Builder

I'm having issues trying to get a subclass of UITextField to render properly in Interface Builder with IBDesignable . The subclass is pretty simple, it allows the user to define insets for the text placement in a UITextField . The code is as follows:

import Foundation

@IBDesignable public class CLYInsetTextField: UITextField {

    @IBInspectable public var topInset: CGFloat = 0 {
        didSet {
            self.setNeedsDisplay()
        }
    }
    @IBInspectable public var leftInset: CGFloat = 0 {
        didSet {
            self.setNeedsDisplay()
        }
    }
    @IBInspectable public var bottomInset: CGFloat = 0 {
        didSet {
            self.setNeedsDisplay()
        }
    }
    @IBInspectable public var rightInset: CGFloat = 0 {
        didSet {
            self.setNeedsDisplay()
        }
    }

    override public func textRectForBounds(bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset))
    }

    override public func editingRectForBounds(bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset))
    }
}

When using this class in a storyboard, the properties show up in IB perfectly fine, but when I try to update one of the values, Xcode builds the project and spits out the following two warnings:

error: IB Designables: Failed to update auto layout status: dlopen([APP_NAME].app, 1): no suitable image found.  Did find:
[APP_NAME].app: can't map unslidable segment __TEXT to 0x100000000 with size 0x7EB000

error: IB Designables: Failed to render instance of CLYInsetTextField: dlopen([APP_NAME].app, 1): no suitable image found.  Did find:
[APP_NAME].app: can't map unslidable segment __TEXT to 0x100000000 with size 0x7EB000

I can build and run in the simulator just fine, and when I do the view is rendered as I expect it to. It's just when I try to render it in IB that I am coming up against this issue. Other examples I have seen for making interactive custom views in Interface Builder seem to be just as simple as mine and run without problems. Is there a step I am missing, or is what I am trying to do simply not going to work?

You need to enclose your class initialisers in

#if !TARGET_INTERFACE_BUILDER ... #endif

See http://digitalleaves.com/blog/2015/02/tutorial-building-your-own-custom-ibdesignable-view-a-uitextview-with-placeholder/ for an example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM