基类BaseViewController
来源:互联网 发布:java判断函数是否存在 编辑:程序博客网 时间:2024/05/21 11:17
这个类比较适合纯代码开发项目,它的内部有两个公共属性,都是 UIView。一个作为自定义导航栏的superView,另作为除导航栏外的其他界面元素的superView。
它会自动监测当前设备是什么IOS版本,设备的屏幕尺寸是多少。因为用到了autolayout,所以自动适配屏幕旋转。
这个类比较适合纯代码开发项目,它的内部有两个公共属性,都是 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
这个类比较适合纯代码开发项目,它的内部有两个公共属性,都是 UIView。一个作为自定义导航栏的superView,另作为除导航栏外的其他界面元素的superView。
它会自动监测当前设备是什么IOS版本,设备的屏幕尺寸是多少。因为用到了autolayout,所以自动适配屏幕旋转。
0 0
- 基类BaseViewController
- 基础类BaseViewController
- 基类控制器(BaseViewController)
- iOS - BaseViewController设计
- 基础类BaseViewController(未验证了)
- 实现BaseViewcontroller的滑动返回按钮效果
- (七)BaseViewController做父类的简单运用
- iOS开发 纯代码适配技巧 baseViewController
- iOS开发 纯代码适配技巧 baseViewController
- BaseViewController中默认设置左右导航栏按钮代码
- iOS开发之基类BaseViewController和BaseTableViewController应该做哪些事情
- 基类
- 基类
- 基类
- 基类
- 基类
- 基类
- Fragment基类,Activity基类
- 互联网创业必备工具盘点
- ScreenTapGesture类(Leap:: ScreenTapGesture)
- tomcat启动出错
- POJ 2096 期望DP
- 单片机的ISP是什么
- 基类BaseViewController
- 黑马程序员--集合框架(1)
- Solve the printer problem for H5-01、H3-00
- 定制自己的日志工具
- URAL 1242 Werewolf (DFS)
- 随机数
- a的样式设置css
- 关于push的使用
- zookeeper命令行(zkCli.sh&zkServer.sh)使用及四字命令