forked from Kentzo/IKSegmentedControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IKSegment.m
76 lines (60 loc) · 1.97 KB
/
IKSegment.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
//
// IKSegment.m
// IKSegment
//
// Created by Ilya Kulakov on 4.10.10.
// Copyright 2010. All rights reserved.
#import "IKSegment.h"
@implementation IKSegment
@synthesize contentView;
@synthesize width;
@synthesize backgroundImage;
@synthesize segmentPosition;
- (void)setContentView:(id)newContentView {
NSParameterAssert([newContentView isKindOfClass:[UIView class]]);
[contentView removeFromSuperview];
[self addSubview:newContentView];
contentView = newContentView;
if ([newContentView isKindOfClass:[UIImageView class]]) {
[contentView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin];
} else {
[contentView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
}
[contentView setBackgroundColor:[UIColor clearColor]];
[contentView setOpaque:NO];
}
- (void)setBackgroundImage:(UIImage *)newBackgroundImage {
backgroundImage = newBackgroundImage;
[self setNeedsDisplay];
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.userInteractionEnabled = NO;
}
return self;
}
- (void)drawRect:(CGRect)rect {
if (backgroundImage) {
rect = self.bounds;
switch (segmentPosition) {
case IKSegmentPositionLeft:
rect.origin.x -= backgroundImage.leftCapWidth;
rect.size.width += backgroundImage.leftCapWidth;
break;
case IKSegmentPositionRight:
rect.size.width += backgroundImage.size.width - (backgroundImage.leftCapWidth + 1);
break;
default:
break;
}
[backgroundImage drawInRect:rect];
}
}
- (CGSize)sizeThatFits:(CGSize)size {
size = [contentView sizeThatFits:size];
if (width != 0.0f) {
size.width = width;
}
return size;
}
@end