Reputation: 123
I am trying to create a simple UIView by calling the following function:
func loadCell() {
let cell : UIView!
cell = UIView(frame: CGRect(x:50, y:50, width:50, height:50))
cell.backgroundColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:1.00)
self.view?.addSubview(cell)
print("Test")
}
I do not get any errors, however the UIView doesn't seem to appear. The text "Test" is printed to the console which tells me it's not a function calling problem. Another thing to note is that I use the exact same code in a different function at a different part of my program to create a different UIView and that code works perfectly. I have absolutely no idea why this code isn't working. At first I thought it may be a zPosition issue, but after experimenting with the zPosition I see no difference.
I then thought it may have something to do with there already being other UIViews being displayed at the time of calling the function, and the new one that the function "should" be creating isn't getting displayed because the program is thinking "It's ok, you already have a UIView here sir! No need to display another!", as strange as that may be.
Update:
I am calling the loadCell function inside of another function called drawGrid()
. drawGrid() is simple a nested for loop which creates a grid of UIViews x along and y high. The drawGrid() function works perfectly when spawning in level 1, however once level 1 is complete and I try to spawn in level 2 that is when it doesn't spawn the next set of UIViews. It prints all of my print("Test")
s to the console multiple times which tells me it IS getting to that point in the program but doesn't want to create the next grid of UIViews.
Update 2:
var gridSizeX:Int = 10
var gridSizeY:Int = 10
var gridArray: [[UIView?]] = Array(repeating: Array(repeating: nil, count: gridSizeY), count: gridSizeX)
func loadCell(xPos:Int, yPos:Int) {
let xStart = Int(size.width/2/2) - ((gridSizeX * gridScale)/2)
let yStart = Int(size.height/2/2) - ((gridSizeY * gridScale)/2)
let cell : UIView!
cell = UIView(frame: CGRect(x: xStart + (xPos * gridScale), y:yStart + (yPos * gridScale), width:gridScale, height:gridScale))
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.02).cgColor
cell.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:0)
self.view?.addSubview(cell)
gridArray[xPos][yPos] = cell
}
func drawGrid() {
for y in 0 ... gridSizeY {
for x in 0 ... gridSizeX {
loadCell(xPos:x, yPos:y)
}
}
}
func breakGrid() {
for y in 0 ... gridSizeY - 1 {
for x in 0 ... gridSizeX - 1 {
gridArray[x][y]?.removeFromSuperview()
}
}
}
As you can see, the functions I posted here at the beginning are a little different as I removed unnecessary, irrelevant code not to get confused by. I have added it all back here, and if there is anything you are unsure about just ask :) I also added the "BreakGrid()" function with deletes/removes the previous grid of UIViews. and after deleted I run the drawGrid() again for the next level.
Upvotes: 0
Views: 5035
Reputation: 475
Please try this
func loadCell() {
let cell : UIView = UIView(frame: CGRect(x:50, y:50, width:50, height:50))
cell.backgroundColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:1.00)
self.view?.addSubview(cell)
print("Test")
print(self.view)
}
It may be work for you I have given the size at the time of initialization.
Upvotes: 0
Reputation: 173
I have checked your code it's work perfect. May your view not updated properly. Can you try this on main thread.
DispatchQueue.main.async {
let cell : UIView!
cell = UIView(frame: CGRect(x:50, y:50, width:50, height:50))
cell.backgroundColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:1.00)
self.view.addSubview(cell)
print("Test")
}
Upvotes: 0
Reputation: 1809
You are most likely calling your method too early, before your view is initialized (probably calling from viewDidLoad?). This would explain why print(self.view) is nil
Try calling your method for viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadCell()
}
Upvotes: 1
Reputation: 957
When I do print(self.view) it just prints nil
That means that your self.view
is nil
so nothing subviews your cell. You should check why your view is nil
. Probably it has not been added to self
by the time loadCell()
is called.
So focus on why the view is nil
.
Upvotes: 0