LabelView没有以编程方式出现IOS Swift

时间:2017-01-02 11:51:18

标签: ios iphone swift

标签视图在我以编程方式创建视图时没有出现,我使用了imageview,在另一个视图中我使用了imageview和两个标签,这里出现了图像视图,但是没有出现包含标签的第二个视图。请帮助我。提前致谢

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        view.backgroundColor = UIColor(colorLiteralRed: 238/255, green: 242/255, blue: 245/255, alpha: 1)
        setUI()
    }

    func setUI() {
        let imageView = UIImageView(frame:CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height * 0.4))

        imageView.image = #imageLiteral(resourceName: "one.png")
        imageView.contentMode = .scaleAspectFill
        view.addSubview(imageView)

        let descriptionView = DesciprtionView(frame: CGRect(x: 10, y: view.frame.height*0.4 * 20, width: view.frame.width - 20, height: view.frame.height * 0.25),logo:#imageLiteral(resourceName: "one.png"), name: "Sundar B", content: "sd,hjfsgfljdsagfjsdfgjhdsgfjhdsgfhsdgfkjhgdsfljhgsdlajfgsdafgds.jfhgfouygih")


        view.addSubview(descriptionView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

class  DesciprtionView: UIView {
    var logo: UIImage!
    var name: String!
    var content: String!

    init(frame: CGRect,logo: UIImage, name: String, content: String) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.logo = logo
        self.name = name
        self.content = content
        setLayout()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setLayout(){
        let logoview = UIImageView(frame:CGRect(x: 10, y: 10, width: 40, height: 40))
        logoview.image = logo
        addSubview(logoview)

        let nameLabel = UILabel(frame:CGRect(x: 60, y: 15, width: 200, height: 30))
        nameLabel.text = name
        nameLabel.font = UIFont(name: "SFUIText-Light",size: 14.0)
        addSubview(nameLabel)

        let contentLabel = UILabel(frame:CGRect(x: 10, y: 60, width: frame.width - 20, height: frame.height - 70))
        contentLabel.text = content
        contentLabel.numberOfLines = 4
        contentLabel.lineBreakMode = .byTruncatingTail
        contentLabel.sizeToFit()
        addSubview(contentLabel)
    }
}

1 个答案:

答案 0 :(得分:0)

替换代码

let descriptionView = DesciprtionView(frame: CGRect(x: 10, y: (view.frame.height*0.4 ) + 20, width: view.frame.width - 20, height: view.frame.height * 0.25),logo:UIImage.init(named: "Screenshot.png")!, name: "Sundar B", content: "sd,hjfsgfljdsagfjsdfgjhdsgfjhdsgfhsdgfkjhgdsfljhgsdlajfgsdafgds.jfhgfouygih")