這個類比較適合純代碼開發項目,它的內部有兩個公共屬性,都是 UIView。一個做爲自定義導航欄的superView,另做爲除導航欄外的其餘界面元素的superView。
佈局
它會自動監測當前設備是什麼IOS版本,設備的屏幕尺寸是多少。由於用到了autolayout,因此自動適配屏幕旋轉。atom
具體代碼以下:spa
#import <UIKit/UIKit.h> @interface BaseViewController : UIViewController @property (nonatomic, strong) UIView *navigationBarView; @property (nonatomic, strong) UIView *backgroundView; - (id)initWithBarHeight:(CGFloat)barHeight; - (CGRect)getBaseNavigationBarFrame; - (CGRect)getBaseBackgroundViewFrame; @end #import "BaseViewController.h" @interface BaseViewController () @property (nonatomic, assign) CGFloat barHight; @property (nonatomic, assign) CGRect navigationBarFrame; @property (nonatomic, assign) CGRect backgroundViewFrame; @end @implementation BaseViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (id)initWithBarHeight:(CGFloat)barHeight { self = [super initWithNibName:nil bundle:nil]; if (self) { self.barHight = barHeight; } return self; } - (void)viewDidLoad { [super viewDidLoad]; //導航條View self.navigationBarView = [[UIView alloc] init]; self.navigationBarView.backgroundColor = [UIColor clearColor]; //除導航條外的背景View self.backgroundView = [[UIView alloc] init]; self.backgroundView.backgroundColor = [UIColor clearColor]; self.backgroundView.clipsToBounds = NO; [self.view addSubview:self.backgroundView]; [self.view addSubview:self.navigationBarView]; self.navigationBarFrame = self.view.bounds; self.backgroundViewFrame = self.view.bounds; if ([UIApplication sharedApplication].statusBarHidden == YES) { [self statusBarIsHidden]; }else { [self statusBarIsShow]; } } - (void)statusBarIsHidden { [self autoLayoutWithV6Height:self.barHight V7Height:self.barHight]; } - (void)statusBarIsShow { [self autoLayoutWithV6Height:self.barHight V7Height:self.barHight + 20]; } - (void)autoLayoutWithV6Height:(CGFloat)v6Height V7Height:(CGFloat)v7Height { if ([self.view respondsToSelector:@selector(addConstraints:)]) { [self.navigationBarView setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.backgroundView setTranslatesAutoresizingMaskIntoConstraints:NO]; UIView *navigationBarView = self.navigationBarView; UIView *backgroundView = self.backgroundView; CGRect tmpNavigationBarFrame = self.navigationBarFrame; CGRect tmpBackgroundViewFrame = self.backgroundViewFrame; //橫向自動佈局 NSArray *layoutConstraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[navigationBarView]-0-|" options:0 metrics:nil views:@{@"navigationBarView":navigationBarView}]; [self.view addConstraints:layoutConstraints1]; NSArray *layoutConstraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[backgroundView]-0-|" options:0 metrics:nil views:@{@"backgroundView":backgroundView}]; [self.view addConstraints:layoutConstraints2]; //縱向自動佈局 NSArray *layoutConstraints3 = nil; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { NSString *formatString = [NSString stringWithFormat:@"V:|-0-[navigationBarView(==%f)]-0-[backgroundView]-0-|", v7Height]; layoutConstraints3 = [NSLayoutConstraint constraintsWithVisualFormat:formatString options:0 metrics:nil views:@{@"navigationBarView":navigationBarView, @"backgroundView":backgroundView}]; tmpNavigationBarFrame.size.height = v7Height; tmpBackgroundViewFrame.size.height = self.view.frame.size.height - v7Height; tmpBackgroundViewFrame.origin.y = v7Height; }else { NSString *formatString = [NSString stringWithFormat:@"V:|-0-[navigationBarView(==%f)]-0-[backgroundView]-0-|", v6Height]; layoutConstraints3 = [NSLayoutConstraint constraintsWithVisualFormat:formatString options:0 metrics:nil views:@{@"navigationBarView":navigationBarView, @"backgroundView":backgroundView}]; tmpNavigationBarFrame.size.height = v6Height; tmpBackgroundViewFrame.size.height = self.view.frame.size.height - v6Height; tmpBackgroundViewFrame.origin.y = v6Height; } [self.view addConstraints:layoutConstraints3]; self.navigationBarFrame = tmpNavigationBarFrame; self.backgroundViewFrame = tmpBackgroundViewFrame; }else { //Autoresizing代碼 CGRect frame = [UIScreen mainScreen].bounds; self.navigationBarView.frame = CGRectMake(0, 0, frame.size.width, v6Height); self.navigationBarView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth; self.backgroundView.frame = CGRectMake(0, v6Height, frame.size.width, frame.size.height - v6Height); self.navigationBarFrame = self.navigationBarView.frame; self.backgroundViewFrame = self.backgroundView.frame; } } - (CGRect)getBaseNavigationBarFrame { return self.navigationBarFrame; } - (CGRect)getBaseBackgroundViewFrame { return self.backgroundViewFrame; } @end