7

So this is the weirdest thing I came across ever.

I add a simple UIView on top of an UIView on an UIViewController. I change the size to 200 x 200 and then I want to change the UIView's size by placing this line of code inside the viewDidLoad method:

self.gameBackgroundView.frame.size.height = 10

which seems perfectly normal in Swift. Though, nothing happens. I can change the color, corner radius etc...

EDIT:

I have also deleted all constraints from that view and it still does not work.

kalafun
  • 3,512
  • 6
  • 35
  • 49
  • 1
    Two things. I remember in Obj-C you had to change the whole frame property to get the view to update. The other thing is if you have Autolayout constraints set it'll ignore frame changes. – Tobias May 11 '15 at 15:21
  • I have set like 4 constraints on another UIView though, does this apply on my other why which frame I want to change ? – kalafun May 11 '15 at 15:25
  • Can you show how you create the view and add it as a subview? – Léo Natan May 11 '15 at 15:27
  • I open my storyboard, drag a new UIView on top of my UIViewController, change the background color so I can see the frame changes and I create an IBOutlet in the corresponding class file – kalafun May 11 '15 at 15:28
  • If you don't set constraints in InterfaceBuilder some will be added automatically. Check out `gameViewBuilder.constraints`. Also make sure `gameViewBuilder.translatesAutoResizingMaskToConstraints` is true. – Tobias May 11 '15 at 15:30
  • when I print it out I can see only `[] []` – kalafun May 11 '15 at 15:34
  • Yes I agree with @Tobias. setting the gameViewBuilder.translatesAutoResizingMaskToConstraints to true. I was modifying the .frames() but it didn't work because I set the translatesAutoResizingMaskToConstraints to false. – joemalski Apr 27 '17 at 01:38

3 Answers3

19

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!

Community
  • 1
  • 1
kalafun
  • 3,512
  • 6
  • 35
  • 49
5

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

I hope this helps you.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • changing frame in auto layout will not help, it will reset once layout subivews is called. Instead change the constraint – Inder Kumar Rathore May 11 '15 at 15:21
  • I have no constraints set on that view, but I have another view above that but I don't think that has to do anything with it – kalafun May 11 '15 at 15:24
4

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

}
Forge
  • 6,538
  • 6
  • 44
  • 64