嗨,這是我的第一篇文章。感謝您的幫助。如何以編程方式將我的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)
不幸的是,這並沒有解決問題。如果我在iphone 4/4s中運行這個圖片,圖片會被切斷。我希望圖像的大小能夠正確地適用於所有iPhone ...它已經爲所有iphone居中,但我希望圖像通過幀屬性變大/縮小,但我無法弄清楚。 – 2014-11-09 16:23:31
@JeremySh對不起,我再次更新它,請檢查它。 – 2014-11-09 17:44:43
謝謝你修復了circular圖像調整大小到blurPic邊界的問題...這可能是另一個話題的問題,但現在的實際圖像不會調整大小來填充圓...我試圖切換.ScaleAspectFit .ScaleAspectFill和其他一些,但沒有運氣......任何建議?謝謝你的幫助 – 2014-11-09 20:59:24