3

This is my @IBInspectable: code:

@IBDesignable
class PBOView: UIView {
    @IBInspectable var borderRightColor: UIColor? {
        didSet {
            let borderRightView = UIView(frame: CGRectMake(frame.size.width - 10, 0, 10, frame.size.height))
            borderRightView.backgroundColor = borderRightColor

            addSubview(borderRightView)
        }
    }
}

This is the result in Storyboard:

the width of UIView are 150 enter image description here

and in iPhone Simulator: the width of UIView are 150, but should be 80 since it is iPhone. This is why the rectangles are not visible inside my custom views enter image description here

When I set clearColor to the background of my views, the result is following: enter image description here

Why there are a wrong bounds' and frames' width for that UIViews? Actually they are a width from Storyboard instead of a real widths at runtime.

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358

2 Answers2

0

I was digging some, but I haven't found elegant property or method to solve this problem. The only way so far is:

@IBDesignable
class PBOView: UIView {

    var borderRightView: UIView?

    @IBInspectable var borderRightColor: UIColor? {
        didSet {
            addBorderRightView()
        }
    }

    func addBorderRightView() {

        borderRightView = UIView(frame: CGRectMake(frame.size.width - 10, 0, 10, frame.size.height))
        borderRightView!.backgroundColor = borderRightColor
        addSubview(borderRightView!)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        if let borderRightView = borderRightView {
            borderRightView.removeFromSuperview()
        }
        if let borderRightColor = borderRightColor {
            addBorderRightView()
        }
    }
}

Just override layoutSubviews() method with your own content. Since layoutSubviews() is called almost everywhere it is working as it should.

The result is:

enter image description here

Still looking for elegant solution - method that do this for me.

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
-1

Try this: The subView's frame should be equal to the placeHolder's bounds. i.e

subView.frame = placeholder.bounds;
Kusal Shrestha
  • 1,643
  • 2
  • 13
  • 20