Ios 如何以编程方式将我的imageView居中于另一个imageView中

Ios 如何以编程方式将我的imageView居中于另一个imageView中,ios,iphone,swift,Ios,Iphone,Swift,嗨,这是我的第一篇帖子。谢谢你的帮助 我创建了一个UIImageView并在其上添加了模糊效果。现在我添加了另一个UIImageView作为模糊图像视图的子视图。我试图弄明白如何通过编程使它的高度和宽度成比例,这样它在所有屏幕尺寸上都会很好看 我知道如何将它居中,但无法使大小看起来很好。 我希望高度比它的超级视图的高度小一点,它是一个圆,所以宽度等于高度 //create blurred profile picture //first create the uiimageview

嗨,这是我的第一篇帖子。谢谢你的帮助

我创建了一个UIImageView并在其上添加了模糊效果。现在我添加了另一个UIImageView作为模糊图像视图的子视图。我试图弄明白如何通过编程使它的高度和宽度成比例,这样它在所有屏幕尺寸上都会很好看

我知道如何将它居中,但无法使大小看起来很好。 我希望高度比它的超级视图的高度小一点,它是一个圆,所以宽度等于高度

//create blurred profile picture
    //first create the uiimageview
    let blurProfilePic = UIImageView(frame: CGRectMake(0, 64, view.frame.size.width, view.frame.size.height/3.0))
    //assign it a value
    blurProfilePic.image = UIImage(named: "placeholder")
    //make sure it stays within the created bounds
    blurProfilePic.layer.masksToBounds = true
    //center the picture
    blurProfilePic.contentMode = UIViewContentMode.Center


    //create blur effect
    var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
    visualEffectView.frame = blurProfilePic.bounds

    //add blur layer to blurProfilePic
    blurProfilePic.addSubview(visualEffectView)

    //create the real profile pic
    //arbitrary frame
    let profilePic = UIImageView(frame: CGRectMake(50, 50, 180, 180))
    //the 180,180 needs to be changed so that they are sized proportionally for all screens
    //center it inside the blurimage
    profilePic.center = CGPointMake(blurProfilePic.frame.size.width/2, blurProfilePic.frame.size.height/2)
    profilePic.image = UIImage(named: "placeholder")
    profilePic.layer.borderWidth = 5
    profilePic.layer.borderColor = UIColor.whiteColor().CGColor
    profilePic.layer.cornerRadius = profilePic.frame.size.width/2
    profilePic.clipsToBounds = true


    blurProfilePic.addSubview(profilePic)

    view.addSubview(blurProfilePic)

您是否尝试过使用CGRectInset()函数来代替匹配中心?这样,您就可以将父UIImageView的框架指定给子UIImageView,并通过x、y点插入,这样您就不必担心将其居中。

这对您有帮助吗

class ViewController: UIViewController {
        let blurProfilePic = UIImageView()
        let profilePic = UIImageView()

        override func viewWillLayoutSubviews() {
                blurProfilePic.frame = CGRect(x: 0, y: UIApplication.sharedApplication().statusBarFrame.height, width: view.bounds.width, height: view.bounds.height / 3)

                let blurProfilePicBounds = blurProfilePic.bounds
                profilePic.frame.size = CGSize(width: blurProfilePicBounds.height, height: blurProfilePicBounds.height)
                profilePic.center = CGPointMake(blurProfilePicBounds.width / 2, blurProfilePicBounds.height / 2)
                profilePic.layer.cornerRadius = profilePic.frame.size.width / 2
        }

        override func viewDidLoad() {
                super.viewDidLoad()

                //assign it a value
                blurProfilePic.image = UIImage(named: "placeholder")
                //make sure it stays within the created bounds
                blurProfilePic.layer.masksToBounds = true
                //center the picture
                blurProfilePic.contentMode = .ScaleAspectFill

                //create blur effect
                var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
                visualEffectView.frame = blurProfilePic.bounds
                visualEffectView.autoresizingMask = .FlexibleWidth | .FlexibleHeight
                //add blur layer to blurProfilePic
                blurProfilePic.addSubview(visualEffectView)

                profilePic.image = UIImage(named: "placeholder")
                profilePic.contentMode = .ScaleAspectFill
                profilePic.layer.borderWidth = 5
                profilePic.layer.borderColor = UIColor.whiteColor().CGColor
                profilePic.clipsToBounds = true

                blurProfilePic.addSubview(profilePic)

                view.addSubview(blurProfilePic)
        }
}
更新:

这是我将
profilePic.contentMode
blurProfilePic.contentMode
都设置为
.scalespectfill
时的快照,看起来很好

如果我将
profilePic.contentMode
设置为
.ScaleAspectFit
,您可以看到图像的大小没有调整为填充圆圈


不幸的是,这并不能解决问题。如果我在iPhone4/4s上运行此功能,图像将被切断。我希望所有iphone的图像大小都正确…所有iphone的图像都已居中,但我希望使用边框属性使图像变大/缩小,但我不知道如何调整。@JeremySh抱歉,我再次更新了它,请检查它。谢谢你修复了圆形图像大小调整到模糊图片边界的问题…这可能是另一个主题的问题,但现在实际图像的大小不能填充圆形。。。我试过切换。scaleSpectFit到。scaleSpectFill和其他一些,但运气不好……有什么建议吗?谢谢你的帮助