Why can't I change the view's frame size in Swift?
Solution 1
Ok, So I found a solution here on stack ..
Change frame programmatically with auto layout
The correct answer, that works is, that:
- I need to create an IBOutlet of my heightConstraint
- Change the value of that constraint accordingly
Thank you all!
Solution 2
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.
Solution 3
Try this code, it will work on any device
let BackImage = UIImage(named: "[email protected]")!
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
}
kalafun
Updated on February 27, 2020Comments
-
kalafun about 4 years
So this is the weirdest thing I came across ever.
I add a simple
UIView
on top of anUIView
on anUIViewController
. I change the size to 200 x 200 and then I want to change theUIView
's size by placing this line of code inside theviewDidLoad
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.