2017-02-23 66 views
1

我想在imageView的底部添加一个标签作为子视图,但当图像更改其高度或宽度时,标签无法响应。 所以我想制作一个动态标签。如何以编程方式在imageView底部添加标签?

This is what i did until now

+0

你使用自动布局,否则 –

+0

@ Anbu.Karthik不,我没有使用autolayout,我试图通过向标签添加框架来编程。 –

回答

0

这里例如添加标签,以图像的

let picImage:UIImageView = { 
    let imageView = UIImageView() 
    let image = UIImage(named: "test") 
    imageView.image = image 
    imageView.contentMode = .scaleAspectFill 
    imageView.translatesAutoresizingMaskIntoConstraints = false 
    return imageView 
    }() 
let durationLabel: UILabel = { 
    let label = UILabel() 
    label.text = "1:20:12" 
    label.font = UIFont(name:"HelveticaNeue-Bold", size: 15.0) 
    label.textAlignment = NSTextAlignment.right 
    label.numberOfLines = 1 
    label.textColor = UIColor.white 
    label.translatesAutoresizingMaskIntoConstraints = false 
    return label 
}() 
func setupView(){ 
// I will skip part of image setup 
self.picImage.addSubview(durationLabel) 
    durationLabel.bottomAnchor.constraint(equalTo: picImage.bottomAnchor).isActive = true 
    durationLabel.leftAnchor.constraint(equalTo: picImage.leftAnchor, constant: 5).isActive = true 
     durationLabel.rightAnchor.constraint(equalTo: picImage.rightAnchor, constant: -5).isActive = true 
} 

这段代码添加标签到设置约束下的图像和左右

https://i.stack.imgur.com/wGIZy.png

+0

谢谢,它的工作原理。 –

1

添加像

var lbl1 = UILabel(frame: CGRect(x: yourimageview.frame.origin.x, y: yourimageview.frame.origin.y + yourimageview.frame.size.height + 5, width: yourimageview.frame.size.width, height: asyourWish)) 


lbl1.textColor = UIColor.black 
lbl1.frame = position 
lbl1.backgroundColor = UIColor.clear 
lbl1.textColor = UIColor.white 

lbl1.text = "TEST" 
self.view.addSubview(lbl1) 
+0

你需要与此有关的帮助吗 –

相关问题