UIViewcontroller没有加载子视图(swift 3)

时间:2016-10-26 15:06:46

标签: ios uiview swift3

我在故事板中进行了以下设置:

UIView - > UIScrollView->的UIView(MyOwnView)

我有一个UIViewController并添加了一个UIScrollView,并在UIScrollView中添加了一个UIView。这个UIView是一个自定义的UIView:

class MyOwnView: UIView {


    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code


       self.backgroundColor = UIColor(patternImage: (UIImage(named: "menschVorne")?.resize(CGSize(width: rect.width, height: rect.height)))!)
print("loaded")

    }
}

当我加载UIViewController时,没有显示图像。当UIViewController出现时,将调用draw(rect ..)函数。有人能帮助我吗?

Kinldy问候!

1 个答案:

答案 0 :(得分:0)

我可以找到一个解决方法:

let image = UIImage(named: "menschVorne")?.resize(CGSize(width: rect.width, height: rect.height))
        let imageView = UIImageView(image: image)



            self.addSubview(imageView)
            imageView.center = self.center
self.sendSubview(toBack: imageView)

而不是

self.backgroundColor = UIColor(patternImage: (UIImage(named: "menschVorne")?.resize(CGSize(width: rect.width, height: rect.height)))!)