Reputation: 16124
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:
However, if I run the exact same code in viewWillAppear
it appears normally and is clickable like this:
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:
the UIView
is in the same NIB as the view controller's main view and is connected via an IBOutlet
.
the NIB file was created separately from the view controller and is
initialized calling initWithNibName
.
I have set the Custom Class and view outlet in the NIB file to match the view controller.
Autolayout is turned off for the NIB file
There is no Storyboard
Upvotes: 1
Views: 849
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:
UIView
in the BlindImplementing the following (often in combination) can help diagnose the source of common view problems.
nil
?-window
?-hidden
?-clipsToBounds
?-alpha
is 1-ish.UIView
work instead?viewDidLoad
, viewWillAppear
, viewDidLayoutSubviews
, etc) mess with the view?super
everyplace it should (viewDidLoad
, etc)?UINavigationController
, UITabBarController
, etc)?-wantsFullscreenLayout
?-edgesForExtendedLayout
?-translatesAutoresizingMaskIntoConstraints
set appropriately?I'll add more as they come to me.
Upvotes: 2
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