基础类BaseViewController

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

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

具体代码如下:

#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


转载于:https://my.oschina.net/u/1418722/blog/183870

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值