last day (15 days later) » 

6:54 PM
0
A: Constraints affecting shadow of UIView?

MochaIn viewDidLoad, your constraints have not taken affect yet. So when you create your UIBezierPath, the bounds are not updated to the auto-layout constraints. The bounds that are used are probably the ones in your .xib file. Move your shadowMakingPath into viewWillLayoutSubviews override func vie...

 
I tried that but the result I'm getting is still the same. I created a viewWillLayoutSubviews function in the related file, any ideas what I could be doing wrong? And should I be updating the .xib file in some way? Sorry, I'm very new to Swift coding.
 
You shouldn't need to update the xib if your constraints are working. Instead of using bounds, try using frame? see updated answer.
 
I did that, and now the shadow is floating to the right instead of left. Same result though. And are you referring to my .storyboard file by .xib? Otherwise, I don't have a .xib file set up.
 
Yes, a .xib is usually an individual screen in a .storyboard file (You don't need to worry about this for your problem). Can you screenshot us your UIView constraints and how it looks in your storyboard?
 
Here you go. I hope that's enough! Basically, I just have a single UIView in the UIViewController with 40pt constraints on all sides. imgur.com/a/jrSmc8R
 
6:54 PM
I'm not sure what the problem can be. Are you overriding the func and calling super? If you put a breakpoint in the viewdidLoad and viewWillLayoutSubviews can you po the signUpView.frame in both functions?
 
Hi there!
 
First time using chat.. Hi
 
So I tried putting the code in viewDidLoad too, and I got a thread 1: breakpoint 1.1 error in the console.
I'm not sure if this helps anything but it the breakpoint displayed on the same line of super.viewWillLayoutSubviews().
 
What do you mean you put the code in there too? Wasn't it originally in viewDidLoad
 
Yeah it was, but then I defined the view.WillLayoutSubviews function in the same file under the viewDidLoad function.
 
6:56 PM
Nono, you do not call the view.WillLayoutSubviews function
 
And now I put the code twice in both functions, sorry I'm still a newbie at this.
Oh?
 
viewWIllLayoutSubviews is a function from the UIViewController class
You do not need to call it
It is part of the lifecycle of a view controller
What I wanted you to do is override that method
 
Oh yes sorry I overrode it.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

signUpView.clipsToBounds = true
signUpView.layer.cornerRadius = 20;
signUpView.layer.shadowPath = UIBezierPath(roundedRect: signUpView.frame, cornerRadius: signUpView.layer.cornerRadius).cgPath
signUpView.layer.shouldRasterize = true
signUpView.layer.shadowColor = UIColor.black.cgColor
signUpView.layer.shadowOffset = .zero
signUpView.layer.shadowOpacity = 1
signUpView.layer.shadowRadius = 20
signUpView.layer.masksToBounds = false
Like that you mean?
 
Yes, that is correct
can you try again without calling the method manually?
Your viewDidLoad() {
 
What do you mean?
 
6:58 PM
You were calling view.viewWIllLayout before right?
Take out that line and run it again
 
Alright so 1 second my brain is failing me at this point..
Uhh I don't think I called the methods manually at any point? Or maybe I did without realizing. Either way all I did was rewrite the code, and hit cmd + r.
Initializing the simulator and that's about it.
 
Haha sorry.. Can you show me everything you have in this VC?
 
Sure!
import UIKit

class RegistrationViewController: UIViewController {
@IBOutlet weak var signUpView: UIView!

override func viewDidLoad() {
super.viewDidLoad()

signUpView.clipsToBounds = true
signUpView.layer.cornerRadius = 20;
signUpView.layer.shadowPath = UIBezierPath(roundedRect: signUpView.frame, cornerRadius: signUpView.layer.cornerRadius).cgPath
signUpView.layer.shouldRasterize = true
signUpView.layer.shadowColor = UIColor.black.cgColor
signUpView.layer.shadowOffset = .zero
signUpView.layer.shadowOpacity = 1
That's every line of code I have related to the shadow.
Oh the viewDidLoad code is the same as the viewWillLayoutSubviews.
I just tried something out of desperation, lmao.
 
Take out the shadowPath logic in ViewDidLoad
You dont want it twice
So when you breakpoint, can you po the frame values?
 
Breakpoint, po frame values? xD
Sorry I feel like I'm a real noob now..
 
7:06 PM
Ah.. okay
print(signUpView.frame) in viewDidLoad and viewWIllLayoutSubviews
 
Thanks!
I got a ton of things, a few numbers and then some self info.
 
Sure, what are the frames in each function?
 
Uhhh, the numbers?
(40.0, 84.0, 295.0, 654.0) - not sure if that's the frames.
 
There should have been multiple prints
Was there not?
 
Nope, only that one.
We removed the frame in viewDidLoad so I assume there would only be one?
Or should the layoutSubviews be above viewDidLoad?
Alright I put it above viewDidLoad and got 2 outputs.
(40.0, 84.0, 295.0, 654.0)
(40.0, 84.0, 295.0, 654.0)
The same 2 times.
 
7:12 PM
I wanted you to print out the signUpView frames in both viewDidLoad and ViewWillLayoutSubviews. THe frames should change in both functions
If you were only shown 1 print, that means your viewWIllLayoutsubviews is never being called
 
Oh..
How can I solve that?
 
Can you change viewWIllLayoutSubviews to -- viewDidLayoutSubivews
 
Sure.
(40.0, 84.0, 295.0, 654.0)
(40.0, 84.0, 334.0, 738.0)
Different values, and the shadow is like offset by a mile to the right.
Almost as if there are two (which is probably why).
 
That's good.
You only want the shadow for the 2nd frame (Updated one with autolayout)
It should work now, right?
Oh, sorry the shadow is wrong?
 
Yeah it is.
 
7:16 PM
Can I see what it looks like now?
 
The color is darker and it's more to the right now than before.
And it doesn't display on all sides. :/
 
It might be because we used frames instead of bounds -- You can try changing it back to bounds.
 
IT WORKS
YES
THANK YOU SO MUCH
Changing it to bounds fixed everything
 
Yeah sorry about that -- I wasn't sure how the shadow was using the CGRect
 
If you'd like to submit this as your edited answer, I can mark it as complete.
THANK YOU LITERALLY SO MUCH
ive been struggling for the past hour lmao
Noo don't be!
By the way, do you have any suggestions on where to learn proper Swift coding etc?
I’m extremely new to the language and would very much like to learn, how did you learn?
 
7:23 PM
Eh there are tutorials online, but the best way I learnt is to go to a bootcamp/school.
Btw frame is relative to its superview (so it started at x:40 which is the offset) bounds is relative to itself so it started at (x:0)
It depends how you learn and what your motivation is.
 
Oh I see, so that’s why
I guess Swift is a language you can only master through time and dedication!
 
Lol, that's like any language
 
True, sounded good tho lmao
Anyway I’ll keep on fighting and learning iOS development, thanks for the help! Have a great night. :)
 

last day (15 days later) »