iOS Interview Questions Part 4: UIKit 🎨

Chetan Aggarwal
10 min readFeb 21, 2018
A tap is a zero-length swipe.― Apple Inc.

Here we will discuss some UIKit questions. If you have missed the previous one Part 3: Swift, do check it out. Now lets get started!!! 🎬 🌈

Q: Explain Can we have multiple UIWindows in iOS application ?

Yes, in scenarios like covering system UIAlertViews, default keyboard or covering whole app before it is placed in background to avoid screenshot. There are 3 default window enum levels defined such as: UIWindowLevelNormal, UIWindowLevelStatusBar and UIWindowLevelAlert. Usage — create a new UIWindow and set its makeKeyAndVisible property. More detail.

Q: Explain Drawbacks of UITableViews ?

Only vertical scrolling capability which is covered by UICollectionView.

Q: What is very important while updating the UI in a multithreaded application? How can you do that?

Update only on main thread or main queue.

Q: What should we do when the keyboard appears and hides some parts of the UI that are important — logic implementation ?

This can be resolved by using scrollview and keyboard notification delegates. Apple docs for more detail.

Q: What is UIStackView?

UIStackView provides a way to layout a series of views horizontally or vertically. We can define how the contained views adjust themselves to the available space. Don’t miss this article.

Q: Explain Difference between UIWindow and UIView ?

Windows do not have any visible content themselves but provide a basic container for your application’s views. Views define a portion of a window that you want to fill with some content. Apple Doc.

Q: Explain Difference between points and pixels ?

A pixel on iOS is the full resolution of the device, which means if I have an image that is 100x100 pixels in length, then the phone will render it 100x100 pixels on a standard non-retina device. However, because newer iPhones have a quadrupled pixel density, that same image will render at 100x100 pixels, but look half that size. A point is a standard length equivalent to 1x1 pixels on a non-retina device, and 2x2 pixels on a retina device.

Q: Explain What is the responder chain ?

The first object in the ResponderChain is called the First Responder. The responder chain is a series of linked responder objects. It starts with the first responder and end with the app object. If the first responder cannot handle an event, it forwards the event to the next responder in the responder chain. Apple Docs.

Q: Explain view transitions?

View Transitions are effective ways of adding one view on another view with a proper transition animation effect. More detail.

Q: Explain Update Cycle ?

When an iOS app launches, UIApplication in iOS starts the main run loop for an app which runs on the main thread. The main run loop processes events (such as user touches) and handles updates to view-based interfaces. As events occur (such as touch, location updates, motion, and multimedia control) the run loop finds the appropriate handler for the events, calling appropriate methods, which call other methods, and so on. At some moment in time, all events will be handled and control will return to the run loop. This point where control is returned to the run loop is called as the update cycle.

While the events are being processed and some changes are requested to the view, these changes are not updated immediately. Instead the system wait for the existing process to finish and when the next redraw cycle is going to happen. There is a periodic interval between the event processing and UI layout update handling.

Q: Explain setNeedslayout vs layoutIfNeeded vs layoutSubviews ?

The method setNeedsLayout for a UIView tells the system that you want it to layout and redraw that view and all of its subviews, when it is time for the update cycle. This is an asynchronous activity, because the method completes and returns immediately, but it isn’t until some later time that the layout and redraw actually happens and you don’t know when that update cycle will be. Call this method on your application’s main thread. Use it to invalidate the layout of multiple views before any of those views are updated. This behavior allows you to consolidate all of your layout updates to one update cycle, which is usually better for performance. Apple Docs

layoutIfNeeded is a synchronous call that tells the system you want a layout and redraw of a view and its subviews, and you want it done immediately without waiting for the update cycle. When the call to this method is complete, the layout has already been adjusted and drawn based on all changes that had been noted prior to the method call. Apple Docs

So, layoutIfNeeded says update immediately please, whereas setNeedsLayout says please update but you can wait until the next update cycle.

layoutSubviews: The default implementation uses any constraints you have set to determine the size and position of any subviews. Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly. You should not call this method directly. Apple Docs.

Great Example by iOS Insight

Q: Explain 3 phase of layouts ?

There are three phases associated with the layout and drawing of the views. The first is update of constraints (constraint pass), which happens bottom up. The second is layout of views and subviews (layout pass), which happens top down and is dependent on constraint settings. The third phase is the display pass, where the views get redrawn based on the layout pass.

Q: How to make UITAbleView scrolling effective ?

Scrolling in Table Views can be made efficient by following practices:

  • Reuse cell instances: for specific type of cell you should have only one instance, no more.
  • Don’t bind data at cellForRowAtIndexPath: method because at this time cell is not displayed yet. Instead use tableView:willDisplayCell:forRowAtIndexPath: method in the delegate of UITableView.
  • Reduce areas where iOS performs useless blending: don’t use transparent backgrounds, check this out using iOS Simulator or Instruments; gradient will be done better without blending if you can do this.
  • Perform code optimizations to achieve balance of loading CPU & GPU. You should clearly know which part of rendering must be done by GPU, and which one — by CPU for keeping balance.
  • Write specific code for specific cell types.
  • Perform rounding of all pixel-relating data: point coordinates, heights or widths of UIViews and many others.
  • Track your graphical resources: images must be pixel-perfect, else when they will be rendered on Retina displays, it will be doing with unnecessary antialiasing.
  • Load images in background.
  • Process image like clipToBound, corner radius etc and then assign image.
  • Move operation in background thread and refresh in main thread.
  • Last resort is setting up your CALayers for asynchronous displaying mode (even if they are about simple text or images) — this will help you to increase FPS.

For more detail, check this out!

Q: What is UIButton hierarchy ?

UIButton -> UIControl -> UIView -> UIResponder ->NSObject.

Q: Explain UIResponder and UIControl ?

Instance of UIResponder—constitute the event-handling backbone of a UIKit app. UIApplication, UIViewController, and all UIView objects inherit from UIResponder. As events occur like touch events, motion events, remote-control events, and press events, UIKit dispatches them to your app’s responder objects for handling. If a given responder does not handle an event, it forwards that event to the next event in the responder chain. Apple Docs.

Controls implement elements such as buttons and sliders,it uses the Target-Action mechanism to report user interactions to your app. Its instance cannot be created directly. A control’s state of UIControlState type determines its appearance and its ability to support user interactions. Apple Docs.

Q: Explain unwind segue

An unwind segue moves backward through one or more segues to return the user to a scene managed by an existing view controller. Nice Example.

Q: What is intrinsic content size?

With Auto Layout, a view’s content plays as important a role in its layout as its constraints. This is expressed through each view’s intrinsicContentSize, which describes the minimum space needed to express the full view content without squeezing or clipping that data. It derives from the natural properties of the content that each view presents.

Q: What are layer objects?

Layer objects are data objects which represent visual content and are used by views to render their content. Custom layer objects can also be added to the interface to implement complex animations and other types of sophisticated visual effects. An introduction and getting stated tutorial by Raywenderlich.

Q: Explain push and modal segue ?

To create push (or Show) segue, the parent view controller needs to be embedded in navigation controller. The child view controller will inherit this navigation bar on top and a back button that gets you back to the parent view controller. It will be added on top of the navigation stack. You can also swipe right to pop the child view controller. The animation for push segue is like sliding pages horizontally. Child View Controller will loose all these feature if navigation controller is not embeded in parent.

A modal segue (i.e. present modally), is presenting over the current view controller. The child view controller will not inherit navigation view controller so the navigation bar will be lost if you present modal segue. Animation for modal segue is that the child view controller will comes up from the bottom of the page.

For more detail check this Stackoverflow answer. Apple Docs.

Q: Explain tabbar vs toolbar vs navigationbar: when to use which, can you combine them?

The UINavigationBar class implements a control for navigating hierarchical content. It’s a bar, typically displayed at the top of the screen, containing buttons for navigating up and down a hierarchy. The primary properties are a left (back) button, a center title, and an optional right button.

An instance of the UIToolbar class is a control for selecting one of many buttons, called toolbar items. A toolbar momentarily highlights or does not change the appearance of an item when tapped. Use the UITabBar class if you need a radio button style control.

The UITabBar class implements a control for selecting one of two or more buttons, called items. The most common use of a tab bar is to implement a modal interface where tapping an item changes the selection.

Q: Explain Auto resizing mask ?

An integer bit mask that determines how the receiver resizes itself when its superview’s bounds change. When a view’s bounds change, that view automatically resizes its subviews according to each subview’s autoresizing mask. You specify the value of this mask by combining the constants described in UIViewAutoresizing using the C bitwise OR operator. Combining these constants lets you specify which dimensions of the view should grow or shrink relative to the superview. The default value of this property is none, which indicates that the view should not be resized at all. Apple Docs

Q: Explain Auto layout ?

Auto Layout dynamically calculates the size and position of all the views in your view hierarchy, based on constraints placed on those views. For example, you can constrain a button so that it is horizontally centered with an Image view and so that the button’s top edge always remains 8 points below the image’s bottom. If the image view’s size or position changes, the button’s position automatically adjusts to match.

This constraint-based approach to design allows you to build user interfaces that dynamically respond to both internal and external changes. Apple Docs

Nice tutorial by Rawenderlich.

Q: What are the most important application delegate methods a developer should handle ?

The operating system calls specific methods within the application delegate to facilitate transitioning to and from various states. The seven most important application delegate methods a developer should handle are:

application:willFinishLaunchingWithOptions :Method called when the launch process is initiated. This is the first opportunity to execute any code within the app.

application:didFinishLaunchingWithOptions :Method called when the launch process is nearly complete. Since this method is called is before any of the app’s windows are displayed, it is the last opportunity to prepare the interface and make any final adjustments.

applicationDidBecomeActive: Once the application has become active, the application delegate will receive a callback notification message via the method applicationDidBecomeActive. This method is also called each time the app returns to an active state from a previous switch to inactive from a resulting phone call or SMS.

applicationWillResignActive: There are several conditions that will spawn the applicationWillResignActive method. Each time a temporary event, such as a phone call, happens this method gets called. It is also important to note that “quitting” an iOS app does not terminate the processes, but rather moves the app to the background.

applicationDidEnterBackground: This method is called when an iOS app is running, but no longer in the foreground. In other words, the user interface is not currently being displayed. According to Apple’s UIApplicationDelegate Protocol Reference, the app has approximately five seconds to perform tasks and return. If the method does not return within five seconds, the application is terminated.

applicationWillEnterForeground: This method is called as an app is preparing to move from the background to the foreground. The app, however, is not moved into an active state without the applicationDidBecomeActive method being called. This method gives a developer the opportunity to re-establish the settings of the previous running state before the app becomes active.

applicationWillTerminate: This method notifies your application delegate when a termination event has been triggered. Hitting the home button no longer quits the application. Force quitting the iOS app, or shutting down the device triggers the applicationWillTerminate method. This is the opportunity to save the application configuration, settings, and user preferences.

Where to go from here ⏩

Thank you for reading 🧑🏻‍💻

Be sure to clap 👏🏼 and follow 🚶🏻‍♂️

Questions❓Feedback 📫 — please drop you comments 💭

If you like this article, feel free to share it with your friends 📨

Follow me: Linkedin | X(Twitter) | Github 🤝🏼

--

--