如何以编程方式将imageView集中在另一个ImageView中

时间:2014-11-09 02:04:14

标签: ios iphone swift

嗨,这是我的第一篇文章。谢谢你的帮助。

我创建了一个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)

2 个答案:

答案 0 :(得分:1)

您尝试使用CGRectInset()函数而不是匹配中心吗?这样你就可以将父UIImageView的框架分配给一个由x,y点组成的子框架,你根本不需要担心它的居中。

答案 1 :(得分:0)

这对你有帮助吗?

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.contentModeblurProfilePic.conteMode都设置为.ScaleAspectFill时的快照,似乎很好

enter image description here

如果我将profilePic.contentMode设置为.ScaleAspectFit,您可以看到图片未调整大小以填充圆圈。

enter image description here