私TNContainer
はUIViewのサブクラス化されており、drawRect
メソッドに対して次のようにしています
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationBar setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBlackOpaqueBackground.png"]
forBarMetrics:UIBarMetricsDefault];
self.containerView = [[TNContainer alloc] initWithFrame:CGRectMake(0, 0, 100, 10)];
self.navigationBar.topItem.titleView = self.containerView;
}
私の中TNContainer.m
で、私はやっています(境界とフレームを出力するためにいくつかのステートメントを入れています)
#import "TNContainer.h"
@implementation TNContainer
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
NSLog(@"frame at initWithFrame is %@", NSStringFromCGRect(self.frame));
NSLog(@"bound at initWithFrame is %@", NSStringFromCGRect(self.bounds));
return self;
}
-(void) drawRect:(CGRect)rect{
[super drawRect:rect];
NSLog(@"frame at drawRect is %@", NSStringFromCGRect(self.frame));
NSLog(@"bounds at drawRect is %@", NSStringFromCGRect(self.bounds));
CGRect rectangle = self.bounds;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, rectangle);
}
initWithFrame
とで枠の境界drawRect
が全然違うのがとても面白い
2013-09-25 16:19:47.926 TNSegmentController[10458:a0b] frame at initWithFrame is {{0, 0}, {100, 10}}
2013-09-25 16:19:47.928 TNSegmentController[10458:a0b] bound at initWithFrame is {{0, 0}, {100, 10}}
2013-09-25 16:19:47.934 TNSegmentController[10458:a0b] frame at drawRect is {{110, 17}, {100, 10}}
2013-09-25 16:19:47.934 TNSegmentController[10458:a0b] bounds at drawRect is {{0, 0}, {100, 10}}
なぜ{{110, 17}, {100, 10}}
フレームを取得しているのかdrawRect()
....
これについてのアイデアはありますか?