LoginSignup
5
4

More than 5 years have passed since last update.

iOS11でUIVisualEffectViewに直接addSubviewすると落ちる

Last updated at Posted at 2017-09-14

開発中のアプリが何故か原因不明のエラーで落ちるので原因を調査した内容のまとめです。

検証コード

let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
effectView.frame = self.view.bounds
self.view.addSubview(effectView)

let label = UILabel(frame: self.view.bounds.insetBy(dx: 100, dy: 100))
label.backgroundColor = UIColor.blue
label.text = "Hello iOS11"
effectView.addSubview(label)

修正方法

検証コードだけで実行した場合は正しくエラーと修正方法が提示されたので解決しました。

Do not add subviews directly to the visual effect view itself, instead add them to the -contentView.'

effectView.addSubview(label)
↓
effectView.contentView.addSubview(label)
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4