This repository has been archived by the owner on Dec 19, 2017. It is now read-only.
forked from domesticcatsoftware/DCIntrospect
/
DCStatusBarOverlay.m
99 lines (83 loc) · 3.72 KB
/
DCStatusBarOverlay.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// DCStatusBarOverlay.m
//
// Copyright 2011 Domestic Cat. All rights reserved.
//
#import "DCStatusBarOverlay.h"
@implementation DCStatusBarOverlay
@synthesize leftLabel, rightLabel;
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
#pragma mark Setup
- (id)init
{
if ((self = [super initWithFrame:CGRectZero]))
{
self.windowLevel = UIWindowLevelStatusBar + 1.0f;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
const CGFloat bar_size = 20;
if (UIInterfaceOrientationIsLandscape(orientation))
self.frame = CGRectMake(0, 0, screenHeight, bar_size);
else
self.frame = CGRectMake(0, 0, screenWidth, bar_size);
self.backgroundColor = [UIColor blackColor];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.frame];
backgroundImageView.image = [[UIImage imageNamed:@"statusBarBackground.png"] stretchableImageWithLeftCapWidth:2.0f topCapHeight:0.0f];
[self addSubview:backgroundImageView];
self.leftLabel = [[UILabel alloc] initWithFrame:CGRectOffset(self.frame, 2.0f, 0.0f)];
self.leftLabel.backgroundColor = [UIColor clearColor];
self.leftLabel.textAlignment = UITextAlignmentLeft;
self.leftLabel.font = [UIFont boldSystemFontOfSize:12.0f];
self.leftLabel.textColor = [UIColor colorWithWhite:0.97f alpha:1.0f];
self.leftLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addSubview:self.leftLabel];
self.rightLabel = [[UILabel alloc] initWithFrame:CGRectOffset(self.frame, -2.0f, 0.0f)];
self.rightLabel.backgroundColor = [UIColor clearColor];
self.rightLabel.font = [UIFont boldSystemFontOfSize:12.0f];
self.rightLabel.textAlignment = UITextAlignmentRight;
self.rightLabel.textColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
self.rightLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addSubview:self.rightLabel];
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)]];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateBarFrame) name:UIDeviceOrientationDidChangeNotification object:nil];
}
return self;
}
- (void)updateBarFrame
{
// current interface orientation
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat pi = (CGFloat)M_PI;
if (orientation == UIDeviceOrientationPortrait)
{
self.transform = CGAffineTransformIdentity;
self.frame = CGRectMake(0, 0, screenWidth, self.frame.size.height);
}
else if (orientation == UIDeviceOrientationLandscapeLeft)
{
self.transform = CGAffineTransformMakeRotation(pi * (90) / 180.0f);
self.frame = CGRectMake(screenWidth - self.frame.size.width, 0, self.frame.size.width, screenHeight);
}
else if (orientation == UIDeviceOrientationLandscapeRight)
{
self.transform = CGAffineTransformMakeRotation(pi * (-90) / 180.0f);
self.frame = CGRectMake(0, 0, self.frame.size.width, screenHeight);
}
else if (orientation == UIDeviceOrientationPortraitUpsideDown)
{
self.transform = CGAffineTransformMakeRotation(pi);
self.frame = CGRectMake(0, screenHeight - self.frame.size.height, screenWidth, self.frame.size.height);
}
}
#pragma mark Actions
- (void)tapped
{
[[NSNotificationCenter defaultCenter] postNotificationName:kDCIntrospectNotificationStatusBarTapped object:nil];
}
@end