我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()
......
有什么想法吗?