我下面的代码试图在图像视图上加载gif。当代码使用情节提要上的约束时,此代码有效,但是当我编写nslayout约束时,gif不会出现。 gif只是没有出现,没有运行时或编译错误。
let FIRE = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
FIRE.loadGif(name: "FIRE")
view.addSubview(FIRE)
FIRE.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
FIRE.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
FIRE.widthAnchor.constraint(equalToConstant: 400).isActive = true
FIRE.heightAnchor.constraint(equalToConstant: 80).isActive = true
}
答案 0 :(得分:0)
您需要禁用视图的自动调整大小掩码以手动设置约束:
let imageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// 1) turn off autoresizing mask
imageView.translatesAutoresizingMaskIntoConstraints = false
// 2) set contraints
self.setupSubviews()
// 3) update the view
imageView.loadGif(name: "FIRE")
}
func setupSubviews() {
self.view.addSubview(imageView)
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 400).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
}