这个类的作用是“适配”,让所有继承它的子类自动适配各种版本及屏幕尺寸的情况,自动适配显示和隐藏状态栏的情况,自动适配屏幕旋转的情况。
这个类比较适合纯代码开发项目,它的内部有两个公共属性,都是 UIView。一个作为自定义导航栏的superView,另作为除导航栏外的其他界面元素的superView。
它会自动监测当前设备是什么IOS版本,设备的屏幕尺寸是多少。因为用到了autolayout,所以自动适配屏幕旋转。
001 | #import <UIKit/UIKit.h> |
003 | @interface BaseViewController : UIViewController |
005 | @property (nonatomic, strong) UIView *navigationBarView; |
006 | @property (nonatomic, strong) UIView *backgroundView; |
008 | - (id)initWithBarHeight:(CGFloat)barHeight; |
010 | - (CGRect)getBaseNavigationBarFrame; |
011 | - (CGRect)getBaseBackgroundViewFrame; |
015 | #import "BaseViewController.h" |
017 | @interface BaseViewController () |
019 | @property (nonatomic, assign) CGFloat barHight; |
021 | @property (nonatomic, assign) CGRect navigationBarFrame; |
022 | @property (nonatomic, assign) CGRect backgroundViewFrame; |
026 | @implementation BaseViewController |
028 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil |
030 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
037 | - (id)initWithBarHeight:(CGFloat)barHeight |
039 | self = [super initWithNibName:nil bundle:nil]; |
042 | self.barHight = barHeight; |
052 | self.navigationBarView = [[UIView alloc] init]; |
053 | self.navigationBarView.backgroundColor = [UIColor clearColor]; |
056 | self.backgroundView = [[UIView alloc] init]; |
057 | self.backgroundView.backgroundColor = [UIColor clearColor]; |
058 | self.backgroundView.clipsToBounds = NO; |
059 | [self.view addSubview:self.backgroundView]; |
060 | [self.view addSubview:self.navigationBarView]; |
062 | self.navigationBarFrame = self.view.bounds; |
063 | self.backgroundViewFrame = self.view.bounds; |
065 | if ([UIApplication sharedApplication].statusBarHidden == YES) { |
067 | [self statusBarIsHidden]; |
071 | [self statusBarIsShow]; |
075 | - ( void )statusBarIsHidden |
077 | [self autoLayoutWithV6Height:self.barHight V7Height:self.barHight]; |
080 | - ( void )statusBarIsShow |
082 | [self autoLayoutWithV6Height:self.barHight V7Height:self.barHight + 20]; |
085 | - ( void )autoLayoutWithV6Height:(CGFloat)v6Height V7Height:(CGFloat)v7Height |
087 | if ([self.view respondsToSelector:@selector(addConstraints:)]) { |
089 | [self.navigationBarView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
090 | [self.backgroundView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
092 | UIView *navigationBarView = self.navigationBarView; |
093 | UIView *backgroundView = self.backgroundView; |
095 | CGRect tmpNavigationBarFrame = self.navigationBarFrame; |
096 | CGRect tmpBackgroundViewFrame = self.backgroundViewFrame; |
099 | NSArray *layoutConstraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@ "H:|-0-[navigationBarView]-0-|" options:0 metrics:nil views:@{@ "navigationBarView" :navigationBarView}]; |
101 | [self.view addConstraints:layoutConstraints1]; |
103 | NSArray *layoutConstraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@ "H:|-0-[backgroundView]-0-|" options:0 metrics:nil views:@{@ "backgroundView" :backgroundView}]; |
105 | [self.view addConstraints:layoutConstraints2]; |
108 | NSArray *layoutConstraints3 = nil; |
110 | if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { |
112 | NSString *formatString = [NSString stringWithFormat:@ "V:|-0-[navigationBarView(==%f)]-0-[backgroundView]-0-|" , v7Height]; |
114 | layoutConstraints3 = [NSLayoutConstraint constraintsWithVisualFormat:formatString options:0 metrics:nil views:@{@ "navigationBarView" :navigationBarView, @ "backgroundView" :backgroundView}]; |
116 | tmpNavigationBarFrame.size.height = v7Height; |
117 | tmpBackgroundViewFrame.size.height = self.view.frame.size.height - v7Height; |
118 | tmpBackgroundViewFrame.origin.y = v7Height; |
122 | NSString *formatString = [NSString stringWithFormat:@ "V:|-0-[navigationBarView(==%f)]-0-[backgroundView]-0-|" , v6Height]; |
124 | layoutConstraints3 = [NSLayoutConstraint constraintsWithVisualFormat:formatString options:0 metrics:nil views:@{@ "navigationBarView" :navigationBarView, @ "backgroundView" :backgroundView}]; |
126 | tmpNavigationBarFrame.size.height = v6Height; |
127 | tmpBackgroundViewFrame.size.height = self.view.frame.size.height - v6Height; |
128 | tmpBackgroundViewFrame.origin.y = v6Height; |
131 | [self.view addConstraints:layoutConstraints3]; |
133 | self.navigationBarFrame = tmpNavigationBarFrame; |
134 | self.backgroundViewFrame = tmpBackgroundViewFrame; |
139 | CGRect frame = [UIScreen mainScreen].bounds; |
141 | self.navigationBarView.frame = CGRectMake(0, 0, frame.size.width, v6Height); |
142 | self.navigationBarView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth; |
144 | self.backgroundView.frame = CGRectMake(0, v6Height, frame.size.width, frame.size.height - v6Height); |
146 | self.navigationBarFrame = self.navigationBarView.frame; |
147 | self.backgroundViewFrame = self.backgroundView.frame; |
151 | - (CGRect)getBaseNavigationBarFrame |
153 | return self.navigationBarFrame; |
156 | - (CGRect)getBaseBackgroundViewFrame |
158 | return self.backgroundViewFrame; |