Ios 使用序列图像板生成的UIImageView的帧值错误

Ios 使用序列图像板生成的UIImageView的帧值错误,ios,swift,Ios,Swift,我想在UIImageView中添加模糊效果。我使用故事板将图像放置到视图中,并使用了以下代码: var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView visualEffectView.frame = bigProfileImage.frame bigProfileImage.addSubview(visualEffectView) println(

我想在
UIImageView
中添加模糊效果。我使用故事板将图像放置到视图中,并使用了以下代码:

var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
visualEffectView.frame = bigProfileImage.frame
bigProfileImage.addSubview(visualEffectView)

println(bigProfileImage.frame)
println(visualEffectView.frame)
这始终打印:

(0.0, 0.0, 240.0, 128.0)
(0.0, 0.0, 240.0, 128.0)
比我想象的要小得多。您可以在此处看到reuslt:


是什么原因导致通过代码访问时,
UIImageView
没有正确的帧?

我猜您的
UIImageView
设置了允许其填充屏幕的约束,但您的
UIVisualEffectView
没有此类约束。您可能只设置了一次帧,但
UIImageView
的帧随后可能会更改

我的建议是:因为您使用的是故事板,所以请拖出一个
UIVisualEffectView
并将其放置在
UIImageView
的顶部(可以是
UIImageView
的子视图,也可以是superview)。根据添加了
UIVisualEffectView
的superview,向其添加约束,使其填充屏幕或与
UIImageView
的框架相匹配。您也可以在现有代码中执行此操作

i、 e.添加约束,将
UIVisualEffectView
的上、下、左、右边缘固定到superview或
UIImageView


请记住,
UIVisualEffectView
必须位于
UIImageView
上方,模糊才能工作。不要将
UIImageView
添加为
UIVisualEffectView
或其
contentView
的子视图?使用自动布局?顺便说一句,这本质上是不正确的。假设UIImage的帧未定位在(0,0),则子视图将有偏移。使用
bigProfileImage.bounds
not
.frame
@LancelotdelaMare yes autolayout.when i
println(bigProfileImage.constraints())
它只是一个空数组。您需要在情节提要或代码中添加约束。