Swift3 为动态缩小的图像设置约束。敏捷的
我有一个相当大的徽标(58312x1478px),我正在使用alamofire图像缩小它的大小,然后使用可视流布局(VFL)设置约束,使其在视图(self.view)中水平和垂直居中,如下所示。 由于以下限制,徽标没有居中,请有人告诉我哪里出了问题Swift3 为动态缩小的图像设置约束。敏捷的,swift3,Swift3,我有一个相当大的徽标(58312x1478px),我正在使用alamofire图像缩小它的大小,然后使用可视流布局(VFL)设置约束,使其在视图(self.view)中水平和垂直居中,如下所示。 由于以下限制,徽标没有居中,请有人告诉我哪里出了问题 let viewWidth = self.view.bounds.width let viewHeight = self.view.bounds.height let logoView = UIIm
let viewWidth = self.view.bounds.width
let viewHeight = self.view.bounds.height
let logoView = UIImageView()
logoView.translatesAutoresizingMaskIntoConstraints = false
let logo = UIImage(named: "logo")
let logoSize = CGSize(width: 200.0, height: 100.0)
let aspectScaledToFitImage = logo.af_imageAspectScaled(toFit: logoSize)
logoView.image = aspectScaledToFitImage
self.view.addSubview(logoView)
let logoHeight = logoView.frame.size.height
let logoWidth = logoView.frame.size.width
let logoViewTopSpacing = (viewHeight / 2) + (logoHeight / 2)
let logoViewSideSpacing = (viewWidth / 2) + (logoWidth / 2)
let views = [
"logoView" : logoView
]
// SETTING CONSTRAINTS
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(logoViewSideSpacing)-[logoView]", options: [], metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-\(logoViewTopSpacing)-[logoView]", options: [], metrics: nil, views: views))
我知道了
通过替换下面的
let imageWidth = aspectScaledToFitImage.size.width
let imageHeight = aspectScaledToFitImage.size.height
let logoViewTopSpacing = (viewHeight / 2) - (imageHeight / 2)
let logoViewSideSpacing = (viewWidth / 2) - (imageWidth / 2)
与
解决了徽标在视图中居中的问题
let logoHeight = logoView.frame.size.height
let logoWidth = logoView.frame.size.width
let logoViewTopSpacing = (viewHeight / 2) + (logoHeight / 2)
let logoViewSideSpacing = (viewWidth / 2) + (logoWidth / 2)