基础类BaseViewController

本文转自:点击打开链接

   这个类比较适合纯代码开发项目,它的内部有两个公共属性,都是 UIView。一个作为自定义导航栏的superView,另作为除导航栏外的其他界面元素的superView。

   它会自动监测当前设备是什么IOS版本,设备的屏幕尺寸是多少。因为用到了autolayout,所以自动适配屏幕旋转。

具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值