ewizard ewizard - 2 years ago 121
iOS Question

setHidden not working for UIView

I have a subclass of

. It is instantiated in
like this:


invitedView = [[InvitedView alloc] initWithFrame:CGRectMake(100, 244, 120, 80)];
invitedView.backgroundColor = [UIColor colorWithRed:156.0f/255.0f green:214.0f/255.0f blue:215.0f/255.0f alpha:0.9f];
[self.view addSubview:invitedView];
[invitedView setHidden:YES];

The class itself looks like this:


#import "InvitedView.h"
#import "AppDelegate.h"
#import "ViewController.h"

@class ViewController;

@interface InvitedView() {
UIButton *accept;
UIButton *decline;
UILabel *question;
UIView *gray;
ViewController *myViewController;


@implementation InvitedView

- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self) {
gray = [[UIView alloc] initWithFrame:frame];

NSString *holduser = [(AppDelegate*)[[UIApplication sharedApplication] delegate] invitedby];

[self addSubview:gray];

accept = [[UIButton alloc] init];
decline = [[UIButton alloc] init];
question = [[UILabel alloc] init];
question.text = [[NSString alloc] initWithFormat:@"You have been invited to a group game by %@", holduser];
question.numberOfLines = 0;
question.textAlignment = NSTextAlignmentCenter;
question.textColor = [UIColor colorWithRed:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f];

accept.backgroundColor = [UIColor clearColor];
accept.frame = CGRectMake(20, gray.frame.size.height / 2, (gray.frame.size.width / 2) - 10, (gray.frame.size.height / 2) - 20);
decline.backgroundColor = [UIColor clearColor];
decline.frame = CGRectMake((gray.frame.size.width / 2) + 10, (gray.frame.size.width / 2) - 20, (gray.frame.size.width / 2) - 20, (gray.frame.size.height / 2) - 20);
question.frame = CGRectMake(20, 20, gray.frame.size.width, (gray.frame.size.height / 2) - 20);
[question setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0]];

[accept addTarget:myViewController action:@selector(acceptInvite) forControlEvents:UIControlEventTouchUpInside];
[decline addTarget:myViewController action:@selector(declineInvite) forControlEvents:UIControlEventTouchUpInside];
[gray addSubview:accept];
[gray addSubview:decline];
[gray addSubview:question];

return self;


The method where the view is supposed to be shown is in the view controller showing the view. It ends up getting called, I can verify that the log messages happen all the way up until the


- (void)doSomethingWithTheNewValueOfFlagForHid {
dispatch_async(dispatch_get_main_queue(), ^(void){
[invitedView setHidden:NO];

I would like to know why
isn't being shown after
[invitedView setHidden:NO]
It gets all the way to
, and then nothing happens. I would appreciate any help, thanks in advance.

Answer Source

In ViewDidLoad, change line to

[invitedView setHidden:NO];

to make sure you can actually see the view (frame is ok, no view above ...)

You might also want to check Xcodes 3D View Debugging

UPDATE FROM POSTER: The only reason that it wasnt showing up, is that invitedView was being instantiated inside an if statement that wasn't being executed. However - shallowThought's idea to switch setHidden to YES started me down a more productive debugging tract, leading to the discovery.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download