Can't get UINavigationBar height (Custom background) with UISegmentControl
I need to detect the height of the UINavigationBar
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
NSLog(@"NAV HEIGHT IS %d %d",self.frame.size.height, self.frame.size.width);
}
Prints 0 for the height
And while I'm at it why is the width 1078329344 and 1077936128? What units are Apple using?, is it some weird calculation based on 326dpi?
Background...
I'm using the drawrect overload technique to add a custom graphic to the UINavigationBars through the App.
All good but one issue: When in landscape mode and the UINavigationBar has a UISegmentControl in it, the height increases from the shorter landscape to match the same height as portrait mode.
The code can detect the width, why can't it detect the height? The NSLOg below returns 0
If I could tell how high the UINavigationBar was I could swap graphics accordingly
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
if ([self isMemberOfClass:[UINavigationBar class]] == NO) {
return;
}
UIImage *image = (self.frame.size.width > 320) ?
[UINavigationBar bgImageLandscape] : [UINavigationBar bgImagePortrait];
NSLog(@"NAV HEIGHT IS %d %d",self.frame.size.height, self.frame.size.width);
CGContextClip(ctx);
开发者_Python百科 CGContextTranslateCTM(ctx, 0, image.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextDrawImage(ctx, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}
Above code by Ahmet Ardal (thanks)
You are doing the formatting of data wrong. %d
is for integers, but Apple uses floats for interface coordinates and sizes. So you need to use %f
to format your string. Try changing to:
NSLog(@"NAV HEIGHT IS %f %f",self.frame.size.height, self.frame.size.width);
精彩评论