heximal
heximal

Reputation: 10517

Determine if current screen has visible navigation bar

I have a singlton object. Is there any simple way to determine if current screen contains a navigation bar within singlton methods?

The singleton is UIView subclass. It's designed for showing prorgess activity, e.g. network exchange. It looks like black rectangle dropping down from top and hiding when the work is done. Why singleton? It's easy to call it from any place of code

The followed snippet is showing the initialization of activity singleton and published here just for better understaning my idea.

-(void) showUpdatingView:(NSString *) msg {
    [self initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
    activity = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
    activity.frame = CGRectMake(5, 10, 22, 22);
    labelView = [[[UILabel alloc] initWithFrame:CGRectMake(35, 10, [UIScreen mainScreen].bounds.size.width - 10, 22)] autorelease];
    labelView.font = [UIFont boldSystemFontOfSize:12];
    labelView.backgroundColor = [UIColor clearColor];
    labelView.textColor = [UIColor whiteColor];
    labelView.text = msg;
    [self addSubview:activity];
    [self addSubview:labelView];
    self.backgroundColor = [UIColor blackColor];
    self.alpha = 0.7;
}

The activity can be called by

[[ActivitySingleton getInstance] showUpdatingView:@"Getting data."];

it's not all. The singleton is being created in AppDelegate object and the view is added to

    inlineActivity = [[CHInlineActivityView alloc] initView];
    [self.window.rootViewController.view addSubview:inlineActivity];

I know it may look crazy. But when I was designing it seemed to me reasonable

Upvotes: 0

Views: 3466

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50089

if you have all in one navigationController:

BOOL navHidden = self.window.rootViewController.navigationController.navigatonBarHidden;


if you don't it is a bit harder.. you could check the window's subviews and see if you can find a UINavigationBar

id navbar = [self.window firstSubviewOfKind:[UINavigationBar class] withTag:NSNotFound];
BOOL navHidden = navbar == nil;


@implementation NSView (findSubview)

- (NSArray *)findSubviewsOfKind:(Class)kind withTag:(NSInteger)tag inView:(NSView*)v {
    NSMutableArray *array = [NSMutableArray array];

    if(kind==nil || [v isKindOfClass:kind]) {
        if(tag==NSNotFound || v.tag==tag) {
            [array addObject:v];
        }
    }

    for (id subview in v.subviews) {
        NSArray *vChild = [self findSubviewsOfKind:kind withTag:tag inView:subview];
        [array addObjectsFromArray:vChild];
    }

    return array;
}

#pragma mark - 

- (NSView *)firstSubviewOfKind:(Class)kind withTag:(NSInteger)tag {
    NSArray *subviews = [self findSubviewsOfKind:kind withTag:tag inView:self];
    return subviews.count ? subviews[0] : nil;
}

@end

Upvotes: 1

Related Questions