[Swift 1] Day 18 - Code Exercise Solution


#1

Please share your code or answers with your classmates for the Day 18 Code Exercise and related Notebook Activity questions.

Share code using

Solution

Only use code or watch video solution if you’re unsure how to solve or get stuck. It’s better to attempt writing the code first.


#2

Hi, @PaulSolt

How would you reference to a an element within the NIB ?
I was trying to do that for the space game and got stuck :smile:

As I load the NIB, the nib has let’s say UILabels inside.
The IBOutlets for the labels are within the NIB setup file.
However when you load the view, you would make an instance of your custom class - let’s say your popupView , but you can’t access anything inside the NIB, because there is no reference to it.

Basically in your example - how would you change the text of the UITextField programmatically from within the UIViewController ?
My thought was to somehow make the outlets public within the class that calls the nib, but I couldn’t do it…
Any help is appreciated. Thanks !


#3

Nevermind all that, I am retarded…all I had to do is place the outlets in the file owner class, not in the subclass of UIView I was using to call it… :slight_smile: All good I think :slight_smile:


#4

I tried but had to look at the solution. It was totally over my head.


#5

I had another problem that I couldnt connect the IBOutlet to the class. I know there is a bug somewhere to do with that but It worked in Pauls solution without a problem so I dont know what setting I had a mistake with.


#6

The most important thing is setting the OWNER and the CLASS correctly :wink:


#7

Hey guys,

I have finished the challenge: https://www.dropbox.com/sh/9uanx0p4u3ghfxo/AADbx_PqD6BHBWHQ6iY48vh1a?dl=0

Looking at @PaulSolt’s solution, I am wondering what exactly the intrinsic content size does?

override func intrinsicContentSize() -> CGSize { return CGSize(width: 600, height: 80) }

I have had the problem to find the center, create a variable that uses the center, set it to zero and then add the change in translation to calculate the bounds. I had to use the origin - frame.size.width since it took the view’s constraint’s into account.

It as quite a hassle to figure out what position were referring to the super view, the view or the object in question.

Best,

Max
P.S: I have left a red square which helped me understand the origin position.


#8

Haven’t looked at his code, but this function is to override the size of an UI element.
Let’s say if you have a UIButton or a UISlider, something like that, and you want to override the size, that was defined in the UI, using the auto layout…


#9

Thanks @ravenshore.

Btw, took on the challenge to create an universal slider. By that I mean that you are able to choose any width of the slider and the bounds are correctly computed from the widget dimensions, taking into account the SliderMarks origin and SliderMarks width.

Code is here: https://www.dropbox.com/sh/ujmd9rafd77cxk2/AADD2eMJvOFf8Y0bWpgpZykza?dl=0

Best,

Max


#10

Yes this is more of an advanced computer graphics topic. The position is based on what view you are inside. These numbers differ depending on if you are talking about the canvas (view) or the points inside a subview (UIButton).

This is for setting a default size for the UI element. For example, the UISwitch will do this along with so that you can just drag-and-drop it onto the screen with a set position. You don’t generally change the size of the default switch, you just use it’s intrinsic content size. That also means you don’t need to set size constraints using Auto Layout, only position constraints.