2017-02-19 30 views
0

我想添加一个背景图像到我的UITextView,但似乎没有这样做的选项。我曾尝试添加一个imageView与图像作为子视图如何添加一个背景图像到UITextView

self.bioTextView.delegate = self 
let imgView = UIImageView(frame: self.bioTextView.frame) 
imgView.image = #imageLiteral(resourceName: "text_field_bio") 
self.bioTextView.addSubview(imgView) 

但是,这也没有工作。 textView背景的颜色很清晰。我也尝试过把这个imgView发送到后面,但那也不管用。

+0

把图像视图后面的文本视图,使TextView的背景清晰 –

回答

4

添加foll0wing两行和检查一次

bioTextView.addSubview(imgView) 
bioTextView.sendSubview(toBack: imgView) 

或使用

bioTextView.backgroundColor = UIColor(patternImage: UIImage(named: "Image.png")) 

更新

let textView = UITextView(frame: CGRect(x: CGFloat(20), y: CGFloat(20), width: CGFloat(400), height: CGFloat(400))) 
    textView.text = "this is a test \n this is test \n this is a test" 
    let img = UIImageView(frame: textView.bounds) 
    img.image = UIImage(named: "1.jpg") 
    textView.backgroundColor = UIColor.clear 
    textView.addSubview(img) 
    self.view.addSubview(textView) 
    textView.sendSubview(toBack: img) 

你得到的输出

enter image description here

+0

我使用的第二个选项,它的工作,但有一种方法来UIImage的夹到的TextView的框架,或者设置图像大小调整模式,以便它自己调整到textView的框架? –

+0

@GabeSpound - chioce是你的 –

+0

这是一个问题,我问如何做到这一点大声笑。 –