这个类比较适合纯代码开发项目,它的内部有两个公共属性,都是 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