很少說,直接上代碼:atom
#import <Foundation/Foundation.h> @interface AdaptiveServer : NSObject @property (nonatomic, assign) CGRect navigationBarFrame; @property (nonatomic, assign) CGRect backgroundViewFrame; - (void)setBarHeight:(CGFloat)barHeight; + (CGRect)getBaseNavigationBarFrameWithNavigationBarHeight:(CGFloat)barHeight; + (CGRect)getBaseBackgroundViewFrameWithNavigationBarHeight:(CGFloat)barHeight; @end #import "AdaptiveServer.h" @interface AdaptiveServer() @property (nonatomic, assign) CGFloat navigationBarHeight; @end @implementation AdaptiveServer - (id)init { self = [super init]; if (self) { } return self; } - (void)setBarHeight:(CGFloat)barHeight { self.navigationBarHeight = barHeight; } - (void)setup { if ([UIApplication sharedApplication].statusBarHidden == YES) { [self statusBarIsHidden]; }else { [self statusBarIsShow]; } } - (void)statusBarIsHidden { [self autoLayoutWithV6Height:self.navigationBarHeight V7Height:self.navigationBarHeight]; } - (void)statusBarIsShow { [self autoLayoutWithV6Height:self.navigationBarHeight V7Height:self.navigationBarHeight + 20]; } - (void)autoLayoutWithV6Height:(CGFloat)v6Height V7Height:(CGFloat)v7Height { CGRect baseFrame = [UIScreen mainScreen].bounds; self.navigationBarFrame = baseFrame; self.backgroundViewFrame = baseFrame; CGRect tmpNavigationBarFrame = self.navigationBarFrame; CGRect tmpBackgroundViewFrame = self.backgroundViewFrame; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { tmpNavigationBarFrame.size.height = v7Height; tmpBackgroundViewFrame.size.height = baseFrame.size.height - v7Height; tmpBackgroundViewFrame.origin.y = v7Height; }else { tmpNavigationBarFrame.size.height = v6Height; tmpBackgroundViewFrame.size.height = baseFrame.size.height - v6Height; tmpBackgroundViewFrame.origin.y = v6Height; } self.navigationBarFrame = tmpNavigationBarFrame; self.backgroundViewFrame = tmpBackgroundViewFrame; } + (CGRect)getBaseNavigationBarFrameWithNavigationBarHeight:(CGFloat)barHeight { AdaptiveServer *adaptiveServer = [[AdaptiveServer alloc] init]; [adaptiveServer setBarHeight:barHeight]; [adaptiveServer setup]; return adaptiveServer.navigationBarFrame; } + (CGRect)getBaseBackgroundViewFrameWithNavigationBarHeight:(CGFloat)barHeight { AdaptiveServer *adaptiveServer = [[AdaptiveServer alloc] init]; [adaptiveServer setBarHeight:barHeight]; [adaptiveServer setup]; return adaptiveServer.backgroundViewFrame; } @end
使用方法例子:spa
- (void)viewDidLoad { [super viewDidLoad]; self.navigationBarView.frame = [AdaptiveServer getBaseNavigationBarFrameWithNavigationBarHeight:44]; self.detailBackgroundView.frame = [AdaptiveServer getBaseBackgroundViewFrameWithNavigationBarHeight:44]; }
注意,輸入的自定義導航欄的高度是不包括狀態欄的。code