iOS开发 纯代码适配技巧 baseViewController
来源:互联网 发布:类似imagemeter的软件 编辑:程序博客网 时间:2024/04/28 21:34
这个类的作用是“适配”,让所有继承它的子类自动适配各种版本及屏幕尺寸的情况,自动适配显示和隐藏状态栏的情况,自动适配屏幕旋转的情况。
这个类比较适合纯代码开发项目,它的内部有两个公共属性,都是 UIView。一个作为自定义导航栏的superView,另作为除导航栏外的其他界面元素的superView。
它会自动监测当前设备是什么IOS版本,设备的屏幕尺寸是多少。因为用到了autolayout,所以自动适配屏幕旋转。
001
#import <UIKit/UIKit.h>
002
003
@interface BaseViewController : UIViewController
004
005
@property (nonatomic, strong) UIView *navigationBarView;
006
@property (nonatomic, strong) UIView *backgroundView;
007
008
- (id)initWithBarHeight:(CGFloat)barHeight;
009
010
- (CGRect)getBaseNavigationBarFrame;
011
- (CGRect)getBaseBackgroundViewFrame;
012
013
@end
014
015
#import "BaseViewController.h"
016
017
@interface BaseViewController ()
018
019
@property (nonatomic, assign) CGFloat barHight;
020
021
@property (nonatomic, assign) CGRect navigationBarFrame;
022
@property (nonatomic, assign) CGRect backgroundViewFrame;
023
024
@end
025
026
@implementation BaseViewController
027
028
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
029
{
030
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
031
if
(self) {
032
// Custom initialization
033
}
034
return
self;
035
}
036
037
- (id)initWithBarHeight:(CGFloat)barHeight
038
{
039
self = [super initWithNibName:nil bundle:nil];
040
if
(self) {
041
042
self.barHight = barHeight;
043
}
044
045
return
self;
046
}
047
048
- (
void
)viewDidLoad
049
{
050
[super viewDidLoad];
051
//导航条View
052
self.navigationBarView = [[UIView alloc] init];
053
self.navigationBarView.backgroundColor = [UIColor clearColor];
054
055
//除导航条外的背景View
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];
061
062
self.navigationBarFrame = self.view.bounds;
063
self.backgroundViewFrame = self.view.bounds;
064
065
if
([UIApplication sharedApplication].statusBarHidden == YES) {
066
067
[self statusBarIsHidden];
068
069
}
else
{
070
071
[self statusBarIsShow];
072
}
073
}
074
075
- (
void
)statusBarIsHidden
076
{
077
[self autoLayoutWithV6Height:self.barHight V7Height:self.barHight];
078
}
079
080
- (
void
)statusBarIsShow
081
{
082
[self autoLayoutWithV6Height:self.barHight V7Height:self.barHight + 20];
083
}
084
085
- (
void
)autoLayoutWithV6Height:(CGFloat)v6Height V7Height:(CGFloat)v7Height
086
{
087
if
([self.view respondsToSelector:@selector(addConstraints:)]) {
088
089
[self.navigationBarView setTranslatesAutoresizingMaskIntoConstraints:NO];
090
[self.backgroundView setTranslatesAutoresizingMaskIntoConstraints:NO];
091
092
UIView *navigationBarView = self.navigationBarView;
093
UIView *backgroundView = self.backgroundView;
094
095
CGRect tmpNavigationBarFrame = self.navigationBarFrame;
096
CGRect tmpBackgroundViewFrame = self.backgroundViewFrame;
097
098
//横向自动布局
099
NSArray *layoutConstraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@
"H:|-0-[navigationBarView]-0-|"
options:0 metrics:nil views:@{@
"navigationBarView"
:navigationBarView}];
100
101
[self.view addConstraints:layoutConstraints1];
102
103
NSArray *layoutConstraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@
"H:|-0-[backgroundView]-0-|"
options:0 metrics:nil views:@{@
"backgroundView"
:backgroundView}];
104
105
[self.view addConstraints:layoutConstraints2];
106
107
//纵向自动布局
108
NSArray *layoutConstraints3 = nil;
109
110
if
([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
111
112
NSString *formatString = [NSString stringWithFormat:@
"V:|-0-[navigationBarView(==%f)]-0-[backgroundView]-0-|"
, v7Height];
113
114
layoutConstraints3 = [NSLayoutConstraint constraintsWithVisualFormat:formatString options:0 metrics:nil views:@{@
"navigationBarView"
:navigationBarView, @
"backgroundView"
:backgroundView}];
115
116
tmpNavigationBarFrame.size.height = v7Height;
117
tmpBackgroundViewFrame.size.height = self.view.frame.size.height - v7Height;
118
tmpBackgroundViewFrame.origin.y = v7Height;
119
120
}
else
{
121
122
NSString *formatString = [NSString stringWithFormat:@
"V:|-0-[navigationBarView(==%f)]-0-[backgroundView]-0-|"
, v6Height];
123
124
layoutConstraints3 = [NSLayoutConstraint constraintsWithVisualFormat:formatString options:0 metrics:nil views:@{@
"navigationBarView"
:navigationBarView, @
"backgroundView"
:backgroundView}];
125
126
tmpNavigationBarFrame.size.height = v6Height;
127
tmpBackgroundViewFrame.size.height = self.view.frame.size.height - v6Height;
128
tmpBackgroundViewFrame.origin.y = v6Height;
129
}
130
131
[self.view addConstraints:layoutConstraints3];
132
133
self.navigationBarFrame = tmpNavigationBarFrame;
134
self.backgroundViewFrame = tmpBackgroundViewFrame;
135
136
}
else
{
137
138
//Autoresizing代码
139
CGRect frame = [UIScreen mainScreen].bounds;
140
141
self.navigationBarView.frame = CGRectMake(0, 0, frame.size.width, v6Height);
142
self.navigationBarView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth;
143
144
self.backgroundView.frame = CGRectMake(0, v6Height, frame.size.width, frame.size.height - v6Height);
145
146
self.navigationBarFrame = self.navigationBarView.frame;
147
self.backgroundViewFrame = self.backgroundView.frame;
148
}
149
}
150
151
- (CGRect)getBaseNavigationBarFrame
152
{
153
return
self.navigationBarFrame;
154
}
155
156
- (CGRect)getBaseBackgroundViewFrame
157
{
158
return
self.backgroundViewFrame;
159
}
160
161
@end
0 0
- iOS开发 纯代码适配技巧 baseViewController
- iOS开发 纯代码适配技巧 baseViewController
- iOS开发 纯代码屏幕适配
- iOS开发 - 屏幕适配之纯代码
- iOS - BaseViewController设计
- iOS 纯代码适配iPhone6,6+
- iOS纯代码适配判断
- iOS 纯代码适配iPhone6,6+
- iOS 纯代码适配6、plus
- iOS 纯代码适配iPhone6,6+
- iOS 纯代码适配iPhone6,6+
- iOS纯代码手动适配
- iOS 纯代码适配iPhone6,6+
- iOS 纯代码适配iPhone6,6+
- iOS 纯代码适配iPhone6,6+
- iOS 纯代码适配iPhone6,6+
- iOS 纯代码适配iPhone6,6+
- iOS纯代码开发和xib开发
- jQuery方法区别:click() bind() live() delegate()区别
- js 放置位置
- 在Java中Highcharts前后台数据交互传输
- 压缩感知(Compressive Sensing)一
- GRE填空词汇——显性类+隐性类
- iOS开发 纯代码适配技巧 baseViewController
- 代码里 各种神注释
- JS——事件
- Android调用系统截图时候出现黑框
- Linux下使用Sigar 获取磁盘信息
- 爬虫技术和爬虫需求现状和展望
- Oracle索引(B*tree与Bitmap)的学习总结
- VLC for Android : 编译问题 & 解决办法
- NSString特性分析学习