Ibdesignable View Not Rendering

@IBDesignable - view not rendering as expected

BTW, I just did a quick @IBDesignable with an @IBInspectable that was a UIImage and a draw(rect:) that rendered that image and it worked fine:

@IBDesignable class CustomView: UIView {

@IBInspectable var image: UIImage?

override func draw(_ rect: CGRect) {
image?.draw(at: CGPoint.zero)
}

}

When I do that in a separate target (as one should with designables), I also see the same warning:

ld: warning: -pie being ignored. It is only used when linking a main executable

I suspect that warning may be a red herring. The man pages for ld described -pie option as follows:

This makes a special kind of main executable that is position independent (PIE).

You can toggle this setting if you go to the link settings for your designables' target and change the "Generate Position-Dependent Executable" in the "Linking" settings. Then that warning will go away. Personally, I've never noticed that warning and never changed this settings, and never noticed any adverse affects, but if you want to confirm this for your own sense of satisfaction, try changing this setting and see if you can get the warning to go away.

Bottom line, your @IBDesignable problem probably rests elsewhere, but it's hard to diagnose on the basis of the information provided. We need more information (or, ideally, a MCVE).


It's hard to comment on your autolayout warnings without seeing what those warning are, but I'd guess that (a) you're adding a subview; but (b) not setting the constraints for said subview; and therefore (c) that when you set translatesAutoresizingMaskIntoConstraints = false, that your constraints are ambiguous and therefore result in error messages and unexpected layout at runtime. Or maybe it's a symptom of the idiosyncrasies of scroll views. But it's hard to say without seeing (a) actual autolayout error; (b) what subviews your @IBDesignable added; and (c) what constraints you added for your subviews.

Bottom line, make sure that your programmatically added subviews have all of their constraints unambiguously defined. Also remember that constraints for scrollview subviews act differently that many constraints, defining the contentSize of the scroll view rather than the size of the subviews. (On this latter point, see TN2154.)

IBDesignable view not rendering

The issue is that Interface Builder does not appear to properly render a @IBDesignable macOS view in Interface Builder if that view is the top level view in a scene. It will in iOS, but not macOS. In macOS, it only renders the designable view in Interface Builder if it is a subview, but not the top level view.

I have filed a bug report. #27817119

IBDesignable drawings not displaying in storyboard

This finally solved my problem:
https://stackoverflow.com/a/26720740/5291486

To sum up, I enabled Editor > Automatically Refresh Views and then clicked Editor > Refresh All Views.

@IBDesignable not working in iOS swift 3

Try this

![enter image description here

import QuartzCore

@IBDesignable
class BorderedFloatingTF: UITextField {

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

override init(frame:CGRect) {
super.init(frame:frame)
setup()
}

override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 20, dy: 0)
}

override func editingRect(forBounds bounds: CGRect) -> CGRect {
return textRect(forBounds: bounds)
}

// properties..

@IBInspectable var enableTitle : Bool = false
@IBInspectable var borderColor: UIColor = UIColor.white {
didSet {
layer.borderColor = borderColor.cgColor
}
}
@IBInspectable var borderWidth: Int = 1 {
didSet {
layer.borderWidth = CGFloat(borderWidth)
}
}
@IBInspectable var placeHolderColor: UIColor = UIColor.white {

didSet {
self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: placeHolderColor])
}
}

func setup() {
borderStyle = UITextBorderStyle.none
layer.borderWidth = CGFloat(borderWidth)
layer.borderColor = borderColor.cgColor
placeHolderColor = UIColor.white
}
}

Your IBDesignables & IBInspectables both are working fine.

IBDesignable UI is not showing in Xcode 9 with Swift

I just changed the setup function. It working now. Final Result Image - It showing UI in IB

func setup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleWidth.rawValue) | UInt8(UIViewAutoresizing.flexibleHeight.rawValue)))

self.addSubview(view)
}

func loadViewFromNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "TestView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}

Designable view not rendered at correct position in Storyboard

While many people like to use "layout helper" functions, it's easy to get confused...

You are calling your layoutAttachAll func with:

contentView.layoutAttachAll(to: self)

but in that function, you are doing this:

func layoutAttachAll(to childView:UIView)
{
var constraints = [NSLayoutConstraint]()

constraints.append(NSLayoutConstraint(item: childView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0))
...

but you've passed self as childView, so you're constraining self to self.

If you put your constraint code "inline":

func fromNib<T : UIView>() -> T? {
guard let contentView = Bundle(for: type(of: self)).loadNibNamed(String(describing: type(of: self)), owner: self, options: nil)?.first as? T else {
return nil
}
self.addSubview(contentView)

var constraints = [NSLayoutConstraint]()

contentView.translatesAutoresizingMaskIntoConstraints = false

constraints.append(NSLayoutConstraint(item: contentView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0))
constraints.append(NSLayoutConstraint(item: contentView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0))
constraints.append(NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0))
constraints.append(NSLayoutConstraint(item: contentView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0))

self.addConstraints(constraints)

return contentView
}

you should no longer get the "misplaced" view.

If you really want to use your layoutAttachAll function, you want to call it with:

self.layoutAttachAll(to: contentView)

and change the last line:

    // adding to wrong view
//childView.addConstraints(constraints)
self.addConstraints(constraints)

Maybe worth noting, you can vastly simplify your "helper" extension to:

extension UIView {
@discardableResult
func fromNib<T : UIView>() -> T? {
guard let contentView = Bundle(for: type(of: self)).loadNibNamed(String(describing: type(of: self)), owner: self, options: nil)?.first as? T else {
return nil
}
self.addSubview(contentView)

contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
contentView.frame = bounds

return contentView
}
}

IBInspectable not updating in storyboard but works on simulator

Firstly, you need @IBDesignable directive to render the updates in Storyboard whereas @IBInspectable is only to access the property directly in the Storyboard.

Secondly, you have extended the UIView class to show the inspectable properties in the Storyboard but without @IBDesignable.

Now you would think adding @IBDesignable would solve your problem but sadly you can't apply @IBDesignable on an extension like so:

@IBDesignable //won't work on an extension
extension UIView {
//...
}

You can only apply the @IBDesignable directive on a class you have access to, like so:

@IBDesignable
class MyPrettyDesignableView: UIView {
//...
}


Solution:

Bottomline is that you should subclass UIView and apply the @IBDesignable directive on this class.

Basically, your only option is:

@IBDesignable
class MyPrettyDesignableView: UIView {

@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = cornerRadius
self.layer.masksToBounds = true
}
}

@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
self.layer.borderWidth = borderWidth
}
}

@IBInspectable var borderColor: UIColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) {
didSet {
self.layer.borderColor = borderColor.cgColor
}
}

}

Then in the storyboard, go to the view that you want designable in storyboard and change it's custom class to MyPrettyDesignableView.

  1. Change it's custom class to your IBDesignable subclass:
    IBDesignable

  2. Set the IBInspectable properties:
    IBInspectable

IBDesignable View Rendering times out

Apparently I needed to override init(frame: CGRect) along with init(code: NSCoder).

Got it working! If anyone could care to explain why this wasn't working, that would be great. Otherwise, I'm fine here.



Related Topics



Leave a reply



Submit