// Outlets
@IBOutlet weak var topView: UIStackView!
//view did load
override func viewDidLoad() {
super.viewDidLoad()
let topLayer = CAGradientLayer()
topLayer.frame = topView.frame
topLayer.colors = [UIColor(red: 0.13, green: 0.22, blue: 0.35, alpha: 0.80).cgColor , UIColor(red: 0.18, green: 0.26, blue: 0.39, alpha: 1.00).cgColor]
topLayer.locations = [0.0, 1.0]
topView.layer.insertSublayer(topLayer, at: 0)
}
If I use this code and run it on the simulator the layer is not as big as the view. Unfortunately I can not attach a picture to show you how it looks. I want the layer always as big as the view.
Maybe it is clearer now ?
Post
Replies
Boosts
Views
Activity
Thank you, it worked and you are right, I am using UIKit, sorry for that mistake
Yes that should happen, but if I set the topLayer.frame = topView.frame the layer is not shown correctly. Depending which device I choose in the simulator the layer is either shown too big or too small. I am looking for a solution, where the layer is always as big as the view. In my example the topView is a view where a button and a caption is placed. Depending on the device the height and width are changing, so I want that the size of the layer is changing too. I hope my problem is understandable.
I simply worked with an extension and it works fine, thank you!
Thank you for your answer, but this is not quite that what I was looking for, yesterday I was a little bit confused what to do next, now I have a plan that do not work.
I want this JSON file (as described above), convert into a dictionary. With this dictionary I'd like to create Arrays, which I insert into CoreData. Inserting into CoreData ist working. I am struggling with convert the JSON file into a Dictionary and extract values from this Dictionary to create an Array.
Maybe there is a simpler way to do it?
Thank you now it works
I changed the code like you suggested, but the layer still covers the text.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if filtered {
cell.textLabel?.text = filteredData[indexPath.row]
}
else{
cell.textLabel?.text = collectionEintraege[indexPath.row]
}
let layer = CAGradientLayer()
layer.frame = cell.bounds.insetBy(dx: 4, dy: 7)
layer.colors = [UIColor(red: 0.19, green: 0.22, blue: 0.25, alpha: 1.00).cgColor, UIColor(red: 0.25, green: 0.26, blue: 0.28, alpha: 1.00).cgColor]
cell.contentView.layer.addSublayer(layer)
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.white.cgColor
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.font = UIFont(name: "Helvetica", size: 25.0)
return cell
}
Do you have an idea what else I could try?
This is the code, where I add the sublayer
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let layer = CAGradientLayer()
layer.frame = CGRect(x: 0, y: 0, width: 5000, height: 70)
layer.colors = [UIColor(red: 0.19, green: 0.22, blue: 0.25, alpha: 1.00).cgColor, UIColor(red: 0.25, green: 0.26, blue: 0.28, alpha: 1.00).cgColor]
cell.layer.addSublayer(layer)
}
This is the code, where I add the entrie/text Label, the filtered part I need for the search
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if filtered {
cell.textLabel?.text = filteredData[indexPath.row]
}
else{
cell.textLabel?.text = collectionEintraege[indexPath.row]
}
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.white.cgColor
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.font = UIFont(name: "Helvetica", size: 25.0)
return cell
}
The content of the table was not the problem. The stack view set the height of the table to 0, I do not know why, I changed the distribution from Fill proportionally to equal centering, then I added some constraints where needed and it worked. Thank you
Thank you, worked well!
Thank you 🙏
@OOPer thank you very much for you advice and the solution! It works like it should. Now I call a method in the viewDidLoad() called blinkred(), is that correct?
I tired this in the viewDidLoad() function, but this does not show me the blinking, instead the View show up, when the for-loop has finished.
caption.backgroundColor = UIColor.black
				for _ in 0...2{
						var bool = false
								if bool{
										caption.backgroundColor = UIColor.black
										bool = false
								}
								else {
										caption.backgroundColor = UIColor.systemRed
										bool = true
						}
						sleep(1)
				}
Thank you now it works!
I call saveProcedures() in the view did load of the view where I want to add data.
The same with load Procedure(), if I dont want to add more data I make save Procedure a comment in the viewdidload().
Thank you for your advice, I will try it out!