Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift3 为动态缩小的图像设置约束。敏捷的_Swift3 - Fatal编程技术网

Swift3 为动态缩小的图像设置约束。敏捷的

Swift3 为动态缩小的图像设置约束。敏捷的,swift3,Swift3,我有一个相当大的徽标(58312x1478px),我正在使用alamofire图像缩小它的大小,然后使用可视流布局(VFL)设置约束,使其在视图(self.view)中水平和垂直居中,如下所示。 由于以下限制,徽标没有居中,请有人告诉我哪里出了问题 let viewWidth = self.view.bounds.width let viewHeight = self.view.bounds.height let logoView = UIIm

我有一个相当大的徽标(58312x1478px),我正在使用alamofire图像缩小它的大小,然后使用可视流布局(VFL)设置约束,使其在视图(self.view)中水平和垂直居中,如下所示。 由于以下限制,徽标没有居中,请有人告诉我哪里出了问题

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)