Joel
Joel

Reputation: 16124

Odd behavior when calling addSubView in viewDidLoad

I have created a UIView in IB and when I add it to my UIViewController in viewDidLoad the view's background color is changed to clearColor and the buttons are not clickable (they do not receive touches). If I try and set the background color in code nothing happens. It looks like this:

enter image description here

However, if I run the exact same code in viewWillAppear it appears normally and is clickable like this:

enter image description here

The code is this simple and is the only code in viewDidLoad (I have stripped out all other code in there to see if that was the issue):

[myView setFrame:CGRectMake(0, 0, 320, 30)];
[self.view addSubview:myView];

I have tried calling bringSubviewToFront and that did nothing. Any ideas?

Some other things to note:

Upvotes: 1

Views: 849

Answers (2)

jemmons
jemmons

Reputation: 18657

At issue here is we don't know what's up when you add a view in viewDidLoad. It's not enough to know that it works in viewWillAppear. We have to know it's state when it doesn't appear. Towards that goal, 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?
  • Is the superview non-nil?
  • Does the superview have a -window?

Visibility

  • Is it -hidden?
  • 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 -alpha is 1-ish.

Hierarchy

  • Is the view a subclass? Does a stock UIView work instead?
  • Are any view classes extended with categories?
  • 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 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?

I'll add more as they come to me.

Upvotes: 2

jemmons
jemmons

Reputation: 18657

Not enough info here to know for sure, but my guess is your controller is set to extend views behind top bars. I don't believe autolayout things like topLayoutGuide, etc. get computed until after viewDidLoad.

Possible solutions:

  • Uncheck the "Extend Edges Under Top Bars" box on your view controller if you don't need that.

  • Add the view in viewDidLoad but position it in viewDidLayoutSubviews

  • Position myView with autolayout constraints instead of explicitly setting its frame.

Upvotes: 4

Related Questions