2014-11-09 74 views
0

嗨,這是我的第一篇文章。感謝您的幫助。如何以編程方式將我的imageView置於另一個ImageView中心

我創建了一個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) 

回答

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

+0

不幸的是,這並沒有解決問題。如果我在iphone 4/4s中運行這個圖片,圖片會被切斷。我希望圖像的大小能夠正確地適用於所有iPhone ...它已經爲所有iphone居中,但我希望圖像通過幀屬性變大/縮小,但我無法弄清楚。 – 2014-11-09 16:23:31

+0

@JeremySh對不起,我再次更新它,請檢查它。 – 2014-11-09 17:44:43

+0

謝謝你修復了circular圖像調整大小到blurPic邊界的問題...這可能是另一個話題的問題,但現在的實際圖像不會調整大小來填充圓...我試圖切換.ScaleAspectFit .ScaleAspectFill和其他一些,但沒有運氣......任何建議?謝謝你的幫助 – 2014-11-09 20:59:24

1

您是否嘗試過使用CGRectInset()函數而不是匹配中心?這樣,您可以將父UIImageView的框架分配給子對象,由x,y點插入,並且您不需要擔心將它置於中心位置。