How To Debug UIView in the Blind
I’ve been working a lot with junior iOS devs this week, and a common issue keeps coming up. They’ll add a UIView
to the screen but, for whatever reason, it doesn’t display.
This type of problem isn’t in any way difficult to debug, but it does require experience. In an effort to help junior devs out there fill in some of those experiential gaps, I present:
Tips for Debugging UIView in the Blind
Implementing the following (often in combination) can help diagnose the source of common view problems.
Existence
- Set a breakpoint. Did it hit?
- Is the view non-
nil
? - Is it being added to a superview (
-addSubview:
)? - Is the superview non-
nil
? - Does the superview have a
-window
? - Is the superview what you think it is (try making it blue)?
Visibility
- Is it
-hidden
? - Is it under a sibling (use
-bringSubviewToFront:
check)? - Does the superview
-clipsToBounds
? - Make the view bigger. Can you see it now?
- Make it a lot bigger. How about now?
- Set its center to the superview’s center.
- Make it bright green.
- Make sure its
-alpha
is 1-ish.
Hierarchy
- Is the view a subclass? Does a stock
UIView
work instead? - Are any view classes extended with categories?
- What about in your CocoaPods?
- Do any view controller lifecycle methods (
viewDidLoad
,viewWillAppear
,viewDidLayoutSubviews
, etc) mess with the view? - Does your view controller call
super
everyplace it should (viewDidLoad
, etc)?
Containment
- Is your view controller contained by another view controller that draws on top of it (for example,
UINavigationController
,UITabBarController
, etc)? - Does your view controller
-wantsFullscreenLayout
? - Are there edges in
-edgesForExtendedLayout
? - Can you try rendering your view controller by itself outside of containment?
Autolayout
- Are you getting any constraint consistency errors in log/term?
- Have you set a width/height (or an intrinsic content size)?
- Is
-translatesAutoresizingMaskIntoConstraints
set appropriately?
Xcode
- Have you cleaned your project and rebuilt?
- Have you “Reset Content and Settingsā¦” in the simulator?
- Have you tried on both simulator and a device?
- Have you quit Xcode and started it again?
- Have you shutdown your computer and started again?
- Are you on a beta? Stop that.
Minimum Viable Reproduction
- If you create a fresh project with a fresh view and view controller, does the problem persist?
- If you copy and paste your nibs/storyboards?
- If you copy and paste your classes?