Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift IBDesignable view not show in storyboard

I want to custom a 5-star UIView,also I want it to be render in storyboard. So I decide to use @IBDesignable and @IBInspectable.The following is my code.

import UIKit

@IBDesignable
class RatingView: UIView {

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

        setUpView()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setUpView()
    }

    func setUpView() {

        let imageView = UIImageView(frame:CGRectMake(0, 0, 50,50))
        imageView.image = UIImage(named: "star")

        addSubview(imageView)
    }
}

And then in my storyboard , I pull a UIView into my canvas,and set Custom class to my custom view as RatingView.The compiler starts to compile storyboard file and I just wait for the custom view to be renderd in canvas.Here is the screenshot. enter image description here

The state is "up to date",but the view has not been renderd.The view is just staying white,what I want to see is the image I add to the parent view. When I use UILabel instead of UIImageView, the label is renderd in the canvas but not the UIImageView,how can I render my lovely star image in my canvas.(Images.xcassets has star.png file)

enter image description here

use UILabel instead of UIImageView

func setUpView() {

    let label = UILabel(frame: CGRectMake(0, 0, 50, 50))
    label.text = "text"
    addSubview(label)
}

result: enter image description here

like image 214
tounaobun Avatar asked Nov 10 '22 23:11

tounaobun


1 Answers

I was trying to do the same exact thing. You need to put the view in a framework. As @Benson Tommy said in the comments take a look at WWDC 2014 session 411.

Here is a link to the session:https://developer.apple.com/videos/wwdc/2014/#411

Here is a link to the transcript of the session: http://asciiwwdc.com/2014/sessions/411

like image 63
Ero Avatar answered Dec 28 '22 17:12

Ero