Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Ios 将UIImageView居中放置在UIView内部_Ios_Swift_Uiview - Fatal编程技术网

Ios 将UIImageView居中放置在UIView内部

Ios 将UIImageView居中放置在UIView内部,ios,swift,uiview,Ios,Swift,Uiview,因此,我有一个名为currentEventImage的UIImageView,它位于UIView模糊背景中。我当前希望将ui图像视图置于ui视图的中心位置。下面是我目前的代码 //Subviews will be added here view.addSubview(blurryBackGround) view.addSubview(currentEventImage) view.addSubview(currentEventDate) //Constraints will be added

因此,我有一个名为
currentEventImage
UIImageView
,它位于
UIView
模糊背景中。我当前希望将
ui图像视图
置于
ui视图
的中心位置。下面是我目前的代码

//Subviews will be added here
view.addSubview(blurryBackGround)
view.addSubview(currentEventImage)
view.addSubview(currentEventDate)

//Constraints will be added here
_ = blurryBackGround.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 17, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: self.view.frame.width, height: 330)
currentEventImage.heightAnchor.constraint(equalToConstant: 330).isActive = true
currentEventImage.topAnchor.constraint(equalTo: blurryBackGround.topAnchor, constant: 5).isActive = true
currentEventImage.center = blurryBackGround.center
blurryBackGround.addSubview(currentEventImage)
你知道我会怎么做吗

currentEventImage.center = blurryBackGround.convert(blurryBackGround.center, from: blurryBackGround.superview)

尝试此方法后无效

您可能希望尝试以下基于
centerXAnchor
centerYAnchor
的解决方案(请随意删除宽度/高度限制):

  • 故事板示例,原始图标错位且没有任何约束
  • 在模拟器上:


  • 另一个解决方案(更详细一点)可能是在
    leftAnchor
    righanchor
    bottomAnchor
    topachor
    上添加约束,使用相同的常量。该常数表示超级视图和子视图本身之间的距离。

    您可以使用以下示例代码:-

    这里我给出了视图的宽度和高度为(200400),对于UIImageView,我给出了尺寸为(50,50)


    您检查过这个吗?您是指2012年使用旧语法和反种族方法@VincentJoy的版本@VincentJoy无效@VincentJoy请对包含
    heightAnchor
    &
    topAnchor
    的行进行注释,然后重试
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet var niceIcon:UIImageView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            niceIcon.translatesAutoresizingMaskIntoConstraints = false
            niceIcon.heightAnchor.constraint(equalToConstant: 100).isActive = true
            niceIcon.widthAnchor.constraint(equalToConstant: 100).isActive = true
            niceIcon.centerXAnchor.constraint(lessThanOrEqualTo: niceIcon.superview!.centerXAnchor).isActive = true
            niceIcon.centerYAnchor.constraint(lessThanOrEqualTo: niceIcon.superview!.centerYAnchor).isActive = true
        }
    }
    
    let whitView = UIView(frame:CGRect(self.view.frame.size.width/2-200/2,self.view.frame.size.height/2-400/2,200,400))
    whitView.backgroundcolor = UIColor.white
    view.addSubview(whitView)
    let imgView = UIImageView(frame:CGRect(whitView.frame.size.width/2-50/2,whitView.frame.size.height/2-50/2,50,50))
    imgView.image = UIImage(named: "abc.png")
    whitView.addSubview(imgView)