Add a 0.5 point height subview to UINavigationBar not show in iOS7

I have a class inherit from UINavigationBar and I want to add a separator line as its subview to separate the navigation bar and the navigation content.



The line's height is defined as following.



#define SEPERATOR_LINE_HEIGHT (1.0f / [UIScreen mainScreen].scale)


My code:



@interface MyPopNavigationBar : UINavigationBar
@end

@implementation MyPopNavigationBar {
UIView *separatorLine;
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.clipsToBounds = YES;
self.translucent = NO;

separatorLine = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - SEPERATOR_LINE_HEIGHT, CGRectGetWidth(self.bounds), SEPERATOR_LINE_HEIGHT)];
separatorLine.backgroundColor = [UIColor redColor];
separatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self addSubview:separatorLine];
}
return self;
}


This works well in both iOS 6 and iOS 8 (all Retina), but I can't see my separatorLine in iOS 7 (Retina, too)!



iOS 6 & 8:





iOS 7:





Besides, when I tried to set the separator line height to exact 1, it shows in all iOS versions.



separatorLine = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - 1, CGRectGetWidth(self.bounds), 1)];




What's wrong?



Answers

Solved this by myself. It's the autoresizingMask's fault 😂. I strongly suspect this a bug of iOS7.



I print the recursiveDescription in lldb, only to find the height of separatorLine is autoresized to zero in iOS7! In contrast, the value is 0.5 in iOS8.



So, remove this line:



separatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;


And set the frame in layoutSubviews method of MyPopNavigationBar again to make it correct:



- (void)layoutSubviews {
[super layoutSubviews];
separatorLine.frame = CGRectMake(0, CGRectGetHeight(self.bounds) - SEPERATOR_LINE_HEIGHT, CGRectGetWidth(self.bounds), SEPERATOR_LINE_HEIGHT);
}


Then the line displays in all iOS versions.