Why can't I change the view's frame size in Swift?

陌路散爱 提交于 2019-12-04 09:38:59
kalafun

Ok, So I found a solution here on stack ..

Change frame programmatically with auto layout

The correct answer, that works is, that:

  1. I need to create an IBOutlet of my heightConstraint
  2. Change the value of that constraint accordingly

Thank you all!

It is because of Auto Layout, however you could do it after Auto Layout done its work or change constraints of it. Set it your frame in your viewDidAppear() instead. It should be work.

I hope this help you.

Carlos Norena

Try this code, it will work on any device

let BackImage = UIImage(named: "backGrndImg@2x.png")!

var imageView: UIImageView!

override func loadView() {

    super.loadView()
    self.imageView = UIImageView(frame: CGRectZero)
    self.imageView.contentMode = .ScaleAspectFill
    self.imageView.image = BackImage
    self.view.insertSubview(imageView, atIndex: 0)

}

override func viewDidLayoutSubviews() {

    super.viewDidLayoutSubviews()
    self.imageView.frame = self.view.bounds

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!