当前位置: 首页 > news >正文

基础类BaseViewController

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

   这个类比较适合纯代码开发项目,它的内部有两个公共属性,都是 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

相关文章:

  • IP-SAN实验笔记
  • Node.app – 用于 iOS App 开发的 Node.js 解释器
  • Python机器学习——线性模型
  • 不用图片,纯Css3实现超酷的类似iphone的玻璃气泡效果
  • Python基础教程---读书笔记二
  • WIN 7中开户UAC时在Program File保护目录下进行写文件
  • OpenGL生成轮廓
  • NFS文档
  • 第三章 oracle 10g 空间管理
  • .net连接MySQL的方法
  • asp遇到的一些问题
  • java代码---------陈勇老师的
  • Python cookbook-读书笔记01
  • 您还在用下一步下一步的方式安装SQLSERVER和SQLSERVER补丁吗?
  • PLSQL_统计信息系列04_统计信息的锁定和删除
  • __proto__ 和 prototype的关系
  • iOS | NSProxy
  • JAVA SE 6 GC调优笔记
  • LintCode 31. partitionArray 数组划分
  • maya建模与骨骼动画快速实现人工鱼
  • nginx 负载服务器优化
  • SQLServer之索引简介
  • SSH 免密登录
  • yii2权限控制rbac之rule详细讲解
  • 分布式任务队列Celery
  • 高性能JavaScript阅读简记(三)
  • 记录一下第一次使用npm
  • 前嗅ForeSpider采集配置界面介绍
  • 如何在 Tornado 中实现 Middleware
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 微信开放平台全网发布【失败】的几点排查方法
  • 为视图添加丝滑的水波纹
  • 小程序开发之路(一)
  • 用element的upload组件实现多图片上传和压缩
  • 云栖大讲堂Java基础入门(三)- 阿里巴巴Java开发手册介绍
  • 在electron中实现跨域请求,无需更改服务器端设置
  • hi-nginx-1.3.4编译安装
  • 阿里云服务器如何修改远程端口?
  • ![CDATA[ ]] 是什么东东
  • #微信小程序:微信小程序常见的配置传旨
  • #在 README.md 中生成项目目录结构
  • (52)只出现一次的数字III
  • (Java)【深基9.例1】选举学生会
  • (Python第六天)文件处理
  • (二)学习JVM —— 垃圾回收机制
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (简单) HDU 2612 Find a way,BFS。
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第3章 信息系统治理(一)
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (转)清华学霸演讲稿:永远不要说你已经尽力了
  • ... 是什么 ?... 有什么用处?
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .NET Standard、.NET Framework 、.NET Core三者的关系与区别?