What's the secret to getting a job as an iOS developer? Find out from Todd's personal experience what can give you a significant edge over others in getting hired.
- [Instructor] In my opinion, one of the biggest secrets to becoming an iOS developer is understanding how to create, place and manipulate user interface objects using code. Questions like this are nearly guaranteed to come up in a job interview. Employers want to know that you are proficient with iOS and that you have a deep understanding of how everything works. And being able to code UI elements is a key part of that.
So for that reason, let's talk about creating user interface objects with code. To do that we're going to go and view controller.swift. I'm in a new project that is a single view application template project. I'm going to delete this comment inside of viewDidLoad, and we'll write our code here. Remember a viewDidLoad is the code that runs when your user interface elements from your story board are loaded up and ready to use. So it's a great place to write any code that you want to happen, right when the app launches.
So for this we're going to create a label, and I'm going to define the label in code by using the keyword let, we'll name the label label, and then we'll set it equal to a UI label. So that's the label's name in code, and then some parenthesis after that. So that creates the label. Now before we can actually see the label on the screen, it needs a few things. One of those things is a placement. So it needs somewhere to appear on the screen.
To give it placement, we set its frame. You might recognize frame from earlier when we worked in story boards, a frame is a rectangular area that contains information about the width and the height of the object as well as X and Y coordinates. So let's set the rectangle of the label by typing label.frame and then we set it equal to CGRect, this is a code object that creates a rectangle, and it requires X and Y coordinates and width and height.
So open up the parenthesis, there's a whole bunch of different ways to create your rectangle, the one I'm going to choose here is X Y width height. For now, you don't have to worry about which one of these you choose, as long as it has X Y width and height. They're all gonna be mostly the same for what we're doing. So once you select that, let's pass in some numbers. X will be 50, Y let's have that be 52, and the width we'll make it 100 and the height will be 50.
In addition to creating a frame, we need to put some text inside of the label. Let's go to the next line to do that. Label.text and we'll set it equal to hello from code. So the label exists, line 15, it has a placement frame on line 16 and then on line 17 it has some text. There's one more step. And this is a step that developers who are new to creating UI elements often forget.
And that step is the label needs to be within another view. Just like how it works when we're working in a story board. When you put a label on the screen, it needs to go inside of some other view. We talked about this earlier in the course as the super view. So there are super views and subviews. The way that we get that view that we are working with in the story board is by the keyword view. So that's a property that exists in our class already, we don't have to create anything to access it.
So we type view and then a dot, and then add subview. And then in the parenthesis, passing the label. Once you've done these four steps, then you should be able to see the label on your screen when you run the application. And it says hello from and then we have some dots there, saying that the label is getting cut off. The problem is that it's not wide enough. Let's go back and fix it real quick. Back over to X code, stop the running application and just make this a little bit wider.
I'll change the width from 100 to 200 and run the app one more time. And there it is, you can see all the text right there. Let's go back to X code and review everything we talked about. The first one is that understanding how to create user interface elements in code is fundamental if you want to become an iOS developer for another company. To create user interface objects with code, you need to create them, set their position and size, modify them however you'd like and make sure to add them as a subview.
- Installing and using Xcode
- Working with storyboards
- Working with UI objects and events
- Resizing objects and layouts dynamically
- Coding UI objects such as buttons
- Loading and displaying data in table views
- Testing your iOS app