2015-06-05 20 views
0

我有一個繼承自UINavigationBar的類,我想添加一個分隔線作爲其子視圖來分隔導航欄和導航內容。添加一個0.5高度子視圖到UINavigationBar不顯示在iOS7

線的高度定義如下。

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

我的代碼:

@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; 
} 

這種運作良好,雙方在iOS 6中和iOS 8(所有視網膜),但我看不到我在的iOS 7 separatorLine(視網膜,太)!

iOS 6的& 8:

iOS6_8

的iOS 7:

iOS7

此外,當我試圖設置在分隔行高度苛求1,它示出了在所有iOS版本。

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

allversion

有什麼不對?

回答

0

自己解決了這個問題。這是autoresizingMask的錯。我強烈懷疑這是iOS7的一個錯誤。

我在lldb打印recursiveDescription,只發現separatorLine的高度在iOS7中自動調整爲零!相比之下,iOS8中的值爲0.5。

因此,刪除此行:

separatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; 

,並重新設置在MyPopNavigationBarlayoutSubviews方法框架,使其正確的:

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

那麼該行會顯示在所有iOS版本。

+0

[There is](http://stackoverflow.com/questions/19349072/why-does-ios-auto-layout-lead-to-apparent-rounding-errors-on-pre-retina-displays)是一個類似的問題。 – liushuaikobe

相關問題