forked from weissi/FRLayeredNavigationController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FRLayerChromeView.m
135 lines (108 loc) · 5.39 KB
/
FRLayerChromeView.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* This file is part of FRLayeredNavigationController.
*
* FRLayeredNavigationController is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FRLayeredNavigationController is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FRLayeredNavigationController. If not, see <http://www.gnu.org/licenses/>.
*
*
* Copyright (c) 2012, Johannes Weiß <weiss@tux4u.de> for factis research GmbH.
*/
#import "FRLayerChromeView.h"
#import <QuartzCore/QuartzCore.h>
@interface FRLayerChromeView ()
@end
@implementation FRLayerChromeView
-(id)initWithFrame:(CGRect)frame titleView:(UIView *)titleView title:(NSString *)titleText
{
self = [super initWithFrame:frame];
if (self) {
_toolbar = [[UIToolbar alloc] initWithFrame:CGRectZero];
_toolbar.clipsToBounds = YES;
self.clipsToBounds = YES;
[self addSubview:_toolbar];
if (titleView == nil) {
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.text = titleText;
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.font = [UIFont boldSystemFontOfSize:20.5];
titleLabel.shadowColor = [UIColor whiteColor];
titleLabel.textColor = [UIColor colorWithRed:111.0f/255.0f
green:118.0f/255.0f
blue:126.0f/255.0f
alpha:1.0f];
self.titleView = titleLabel;
} else {
self.titleView = titleView;
}
[self addSubview:self.titleView];
[self manageToolbar];
}
return self;
}
- (void)manageToolbar
{
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
if (self.leftBarButtonItem != nil && self.rightBarButtonItem != nil) {
[self.toolbar setItems:[NSArray arrayWithObjects:_leftBarButtonItem, flexibleSpace, _rightBarButtonItem, nil]];
} else if(self.leftBarButtonItem != nil && self.rightBarButtonItem == nil) {
[self.toolbar setItems:[NSArray arrayWithObject:_leftBarButtonItem]];
} else {
[self.toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, _rightBarButtonItem, nil]];
}
[self setNeedsLayout];
}
- (void)setLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem
{
_leftBarButtonItem = leftBarButtonItem;
[self manageToolbar];
}
- (void)setRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem
{
_rightBarButtonItem = rightBarButtonItem;
[self manageToolbar];
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat barButtonItemsSpace = (self.leftBarButtonItem!=nil?44:0) + (self.rightBarButtonItem!=nil?44:0);
//self.toolbar.frame = CGRectMake(10, 0, self.bounds.size.width-20, self.bounds.size.height);
self.toolbar.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
CGRect headerMiddleFrame = CGRectMake(10 + (barButtonItemsSpace/2),
0,
self.bounds.size.width-20-barButtonItemsSpace,
self.bounds.size.height);
CGSize titleFittingSize = [self.titleView sizeThatFits:headerMiddleFrame.size];
CGRect titleFrame = CGRectMake(MAX((headerMiddleFrame.size.width - titleFittingSize.width)/2,
headerMiddleFrame.origin.x),
MAX((headerMiddleFrame.size.height - titleFittingSize.height)/2,
headerMiddleFrame.origin.y),
MIN(titleFittingSize.width, headerMiddleFrame.size.width),
MIN(titleFittingSize.height, headerMiddleFrame.size.height));
self.titleView.frame = titleFrame;
// Create the path (with only the top-left corner rounded)
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(10.0, 10.0)];
// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
// Set the newly created shape layer as the mask for the image view's layer
self.layer.mask = maskLayer;
}
@synthesize leftBarButtonItem = _leftBarButtonItem;
@synthesize rightBarButtonItem = _rightBarButtonItem;
@synthesize toolbar = _toolbar;
@synthesize titleView = _titleView;
@end