Interview Questions: 3 Commonly asked View and UI iOS Questions

Update: I've published a book that helps you prep and ace iOS interviews:

This is another post from our interview questions posts series. The following questions are often asked on tech iOS interviews to gauge your knowledge of UI, Views, Frames, and Autolayout. This article explains the rationale behind the questions, expected answer for each, and the red flags that you might raise.

What do you use to layout your views correctly on iOS?

Knowing your options for laying out things on the screen is crucial when you need to solve different UI challenges on iOS. This question helps gauge your knowledge about how you put and align views on the screen. Answering this question you should at least mention CGRect Frames and AutoLayout but it would be great to mention other options such a ComponentKit and other Flexbox and React implementation on iOS.

Expected answer: go to options for laying out views on the screen are good old CGRect Frames and AutoLayout. Frames along with autoresizing masks were used in the past before iOS 6 and are not a preferred option today. Frames are too error-prone and difficult to use because it's hard to calculate precise coordinates and view sizes for devices of various size and form.

Since iOS 6 we have AutoLayout which is the go-to solution these days and is preferred by Apple. AutoLayout is a technology that helps you define relationships between views, called constraints, in a declarative way letting the framework calculate precise frames and positions of UI elements instead.

There are other options for laying out views such as ComponentKit, LayoutKit that more or less inspired by React. These alternatives are good in certain scenarios when, for example, you need to build a highly dynamic and fast table views and collection views. AutoLayout is not always perfect for that and knowing that there are other options is always good.

Red flag: not mentioning at least AutoLayout and the fact that Frames are notoriously hard to get right is definitely going to be a red flag for your interviewer. These days no one in their sane mind would do CGRect frame calculations unless it is absolutely necessary (for example when you do some crazy drawings).

What are CGRect Frames? When and where would you use it?

This question is asked to learn if you have a background in building UI "the hard way" with using view position and size calculation. Frames were used previously before AutoLayout to position UI elements on the screen but these days there are other options you have to solve that problem. The interviewer is trying to figure out how advanced you are in and how well you know a lower level of UI rendering.

Expected answer: The simples way to position views on the screen is to give them explicit and specific coordinates and sizes with CGRects. CGRect is a struct that represents a rectangle that a view is placed at. It has origin with x and y values, and size with width and height values. They represent upper-left corner where the view starts to draw itself and width and height of that view respectively. Frames are used to explicitly position views on the screen and have the most flexibility in terms of what and how you position on the screen. But the disadvantage is that you have to take care of everything yourself (with great power comes great responsibility, you know). Meaning even though you're in full control of how your UI is drawn on the screen you will have to take care of all the edge cases and different screen size and resolutions.

A better option these days is AutoLayout. It helps you with layout positioning through constraints and sets specific frames for views for you. It makes your views scalable and adaptive to different screen sizes and resolutions.

Red flag: AutoLayout is de facto the standard of doing layouts these days, Frames are considered to be an outdated concept that is very error prone. Saying that frames are perfectly fine for laying out views would raise a red flag because most likely your interviewer would think that you don't know how to build adaptive and responsive UI.

What is AutoLayout? When and where would you use it?

This is very common UI related question on any interview. Virtually no interview will go without it. AutoLayout is one of the fundamental technologies that Apple pushed on for some time and now it is de facto the standard. Your interviewer either is expecting a very short and brief answer to get an understanding if you're versed in the topic or they'll drill down and ask you all the details about it. Be prepared for both.

Expected answer: AutoLayout is a technology that helps you define relationships between views, called constraints, in a declarative way letting the framework calculate precise frames and positions of UI elements instead. AutoLayout came as an evolution of previously used Frames and auto-resizing masks. Apple created it to support various screen resolutions and sizes of iOS devices.

In the nutshell using AutoLayout instead of setting view frames you'll create NSLayoutConstraint objects either in code or use nibs or storyboards. NSLayoutConstraints describe how views relate to each other so that at runtime UIKit can decide what CGRect frames specifically to set for each view. It will adjust to different screen sizes or orientation changes based on the "rules" you defined using constraints.

The main things you'll be using working with AutoLayout are: NSLayoutRelation, constant, and priority.

  • NSLayoutRelation defines the nature of a constraint between two UI items/views. It can be lessThanOrEqual, equal, or greaterThanOrEqual.
  • constant defines constraint value. It usually defines things like the distance between two views, or width of an item, or margin, etc.
  • priority defines how high of a priority a given constraint should take. Constraints with higher priority number take precedence over other. Priority typically is used to resolve conflicting constraints. For example, when there could be an absence of content we may want to align elements differently, in that scenario we'd create several constraints with different priority.

Bonus points: working with Apple's API for constraints in code is sometimes problematic and difficult. There are several different open source libraries out there that can help with it such as Masonry and PureLayout that dramatically simplify the process.

Red flag: AutoLayout is de facto the standard today for developing UI on iOS, disregarding it or trying to prove that Frames approach is better most likely is going to raise a red flag. There are alternatives of course but most likely your interviewer expects you to be very familiar with the technology since it's such a vital part of any iOS application.

If you liked these questions and answers check out my upcoming book The iOS Interview Guide. It covers similar UI questions, networking questions, fundamental questions, and many other that are commonly asked on technical iOS interviews. Sign up to the waitlist to get helpful tips and tricks and questions and answers right in your inbox.

Update: I've published a book that helps you prep and ace iOS interviews:

comments powered by Disqus