Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App (iOS7) Understanding Views and View Controllers CGRect and Frame

hdetail
hdetail
17,750 Points

Frame properties

I'm having trouble understanding the docs at times. Where is it shown that you can access the width and height by doing frame.size.width/height?

2 Answers

Lee Watkins
Lee Watkins
11,345 Points

Hi Spyros Lefkaditis

This is a good questions, and the vague answer is that it takes practice. Here's how you would go about this:

You would have learnt so far that UIViewControllers etc are all subclasses of UIView. The frame is a property of UIView, so you can start looking in Apple's UIView Reference docs here. In those docs you'll find that UIView has a frame property.

If you expand the frame property you will see that it's of type CGRect. That means that the frame property is a CGRect struct, and to get more information on it, you'll need to view the CGRect Reference documentation. You can do this by clicking on 'CGRect' in the definition of the frame property.

Navigating to the CGRect Reference documentation, you'll find that this struct consists of two "sub-structs": origin and size. These are structs of type CGPoint and CGSize respectively. CGSize contains two values: width and height.

So what you now know is that:

  • UIView contains a property called frame of type CGRect
  • CGRect contains two structs, one is the size of type CGSize
  • CGSize contains two values, width and height

This drill-down of properties is how we find that we can set frame.size.width or frame.size.height.

There is, of course, an easier way, and that is by searching tutorials and so on. However, if you understand how to drill drown into the documentation, it can be very useful.

I hope this helps, and let me know if you have any further questions.

Regards, UniekLee

hdetail
hdetail
17,750 Points

Thank you Lee,I kinda figured it out eventually after I kept looking at the documentation but I agree with you, it just takes practice. The biggest issue I have with iOS development is that I started off with Swift and I understood the tutorials but there don't seem to be enough examples or code samples using the new syntax. So I decided to do all the Objective C stuff and then try and translate them to Swift as a way of learning.

Thank you for responding.