之前用的storyboard以及xib挺多的,最近看到朋友用第三方框架---自动布局约束框架在添加控件约束的时候老实报错.后来自己就试了试纯代码创建以及约束控件.但是纯代码约束一个控件还可以,如果约束的控件两个以上就不如用框架好使了,因为代码量太大了.所以自己简单地写了一下这方面的框架,功能还不完善,今天就花了三个小时写了一下: 实现的功能就是: 调用工具类的方法能实现一个视图在父视图superview中的约束. 以及 一个视图相对于 父视图superview 以及 SubView 两个视图的约束. 剩下的功能最近有时间就会完善


//
// PBAutolayoutTools.h
// testAutolayoutAddAndDelete
//
// Created by 裴波波 on 16/4/29.
// Copyright © 2016年 裴波波. All rights reserved.
// #import <UIKit/UIKit.h> /** 相对于子视图的约束 */
typedef enum : NSUInteger{
PBTop = 1,
PBLeft = 2,
PBBottom = 3,
PBRight = 4,
}SubOptions; @interface PBAutolayoutTools : NSObject /** 上,左,下,右,相对于父视图superView的约束 */
-(void)constrainCustomerView:(UIView *)customeView toSuperView:(UIView *)superView withTopContrain:(CGFloat)top withLeftConstrain:(CGFloat)left withBottomConstrain:(CGFloat)bottom withRigthConstrain:(CGFloat)right; /**
1. customerView相对于父视图Superview以及一个子视图的Subview的约束
2. option选择的是customerView相对于SubView的约束方向,例如customerView在SubView的下方50.0出 就是PBTop 然后 top = 50.0 即可,意思就是customerView的顶部(PBTop)距离SubView的底部.
2.1 PBTop --- customerView的顶部距离SubView的底部距离(customerView在下,SubView在上)
3. PBLeft---customerView的左边与SubView的右边的距离 (customerView在右,SubView在左)
4. PBBottom---就是customerView的底部距离SubView的顶部 (customerView在上,SubView在下)
5. PBRight --- 就是customerView的右边 与 SubView的左边的距离 (customerView在左,SubView在右)
*/
-(void)constrainCustomerView:(UIView *)customeView toSuperView:(UIView *)superView andToASubView:(UIView *)subView withSubViewDirectionOption:(SubOptions)option withTopContrain:(CGFloat)top withLeftConstrain:(CGFloat)left withBottomConstrain:(CGFloat)bottom withRigthConstrain:(CGFloat)right; /** 单例工具对象 */
+(PBAutolayoutTools *)sharedLayoutTools; @end

下面看.m文件的代码


//
// PBAutolayoutTools.m
// testAutolayoutAddAndDelete
//
// Created by 裴波波 on 16/4/29.
// Copyright © 2016年 裴波波. All rights reserved.
// #import "PBAutolayoutTools.h" @interface PBAutolayoutTools () @property (nonatomic, strong) NSMutableArray *arrayConstrains; @end @implementation PBAutolayoutTools -(NSMutableArray *)arrayConstrains{ if (_arrayConstrains == nil) {
_arrayConstrains = [NSMutableArray array];
}
return _arrayConstrains;
} #pragma mark - 单例
static PBAutolayoutTools * instance = nil;
+(PBAutolayoutTools *)sharedLayoutTools{ static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[PBAutolayoutTools alloc] init];
});
return instance;
} /** customerView相对于父视图Superview以及一个子视图的Subview的约束 */
-(void)constrainCustomerView:(UIView *)customeView toSuperView:(UIView *)superView andToASubView:(UIView *)subView withSubViewDirectionOption:(SubOptions)option withTopContrain:(CGFloat)top withLeftConstrain:(CGFloat)left withBottomConstrain:(CGFloat)bottom withRigthConstrain:(CGFloat)right{ /** 相对于一个子视图的约束subView */
CGFloat subConstrain = 0.0;
NSLayoutConstraint * superConstrain1 = nil;
NSLayoutConstraint * superConstrain2 = nil;
NSLayoutConstraint * superConstrain3 = nil; //用switch来判断option选择的是子视图的那个方向与SubView的哪个方向的间距.当option == PBTop的时候也就是customerView的顶部与SubView的底部的距离.此时与superview的底部的距离就可以忽略了!!! 则只需要计算customerView与superview的除top外的其他的约束.以此类推
switch (option) {
case PBTop:
subConstrain = top;
/** 相对于父视图的约束 */
superConstrain1 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:left]; superConstrain2 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-bottom]; superConstrain3 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-right];
break;
case PBLeft:
subConstrain = left;
/** 相对于父视图的约束 */
superConstrain1 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1.0 constant:top];
superConstrain2 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-bottom];
superConstrain3 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-right];
break;
case PBBottom:
subConstrain = bottom;
/** 相对于父视图的约束 */
superConstrain1 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1.0 constant:top];
superConstrain2 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:left];
superConstrain3 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-right];
break;
case PBRight:
subConstrain = right;
/** 相对于父视图的约束 */
superConstrain1 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1.0 constant:top];
superConstrain2 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:left];
superConstrain3 = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-bottom];
break;
default:
break;
}
NSLayoutConstraint * constrainSub = [self constrainCustomerView:customeView aboutAnotherSubView:subView withSubviewOption:option withConstrain:subConstrain]; [superView addConstraints:@[superConstrain1,superConstrain2,superConstrain3,constrainSub]];
}

下面是customerView在一个方向(上 或 下 或 左 或 右) 与SubView的间距.返回值是一个约束,在上面方法中调用,并将约束添加到superview中


/** 单个视图单个方向上的约束 */
-(NSLayoutConstraint *)constrainCustomerView:(UIView *)customeView aboutAnotherSubView:(UIView *)subview withSubviewOption:(SubOptions)option withConstrain:(CGFloat)constrain{ //判断要约束的customerView与subview是哪个方向上的约束
NSLayoutConstraint * returnConstrain = nil;
switch (option) {
case PBTop:
returnConstrain = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:subview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:constrain];
break;
case PBLeft:
returnConstrain = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:subview attribute:NSLayoutAttributeRight multiplier:1.0 constant:constrain];
break;
case PBBottom:
returnConstrain = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:subview attribute:NSLayoutAttributeTop multiplier:1.0 constant:-constrain];
break;
case PBRight:
returnConstrain = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:subview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-constrain];
break; default:
break;
}
return returnConstrain;
}

下面的方法是控制器的view中只有一个子视图view的约束方法.比较简单


/** 相对于父视图superView */
-(void)constrainCustomerView:(UIView *)customeView toSuperView:(UIView *)superView withTopContrain:(CGFloat)top withLeftConstrain:(CGFloat)left withBottomConstrain:(CGFloat)bottom withRigthConstrain:(CGFloat)right{ /** 上 左 下 右 */
NSLayoutConstraint * constrainTop = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1.0 constant:top]; NSLayoutConstraint * constrainLeft = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:left]; NSLayoutConstraint * constrainBottom = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-bottom]; NSLayoutConstraint * constrainRigth = [NSLayoutConstraint constraintWithItem:customeView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-right]; self.arrayConstrains = [NSMutableArray arrayWithArray:@[constrainTop,constrainLeft,constrainBottom,constrainRigth]];
[superView addConstraints:self.arrayConstrains];
} @end

其他功能正在完善中.

使用方式只需要将Demo中的PBAutolayoutTools.h与.m文件拷贝到项目中即可

github框架下载地址 : https://github.com/adampei/PBautolAyoutTools.git

ios - 自动布局框架编写(更多功能完善中)的更多相关文章

  1. React-Native 之 GD (十一)加载更多功能完善 及 跳转详情页

    1.加载更多功能完善 GDHome.js /** * 首页 */ import React, { Component } from 'react'; import { StyleSheet, Text ...

  2. iOS 自动布局框架 – Masonry 详解

    目前iOS开发中大多数页面都已经开始使用Interface Builder的方式进行UI开发了,但是在一些变化比较复杂的页面,还是需要通过代码来进行UI开发的.而且有很多比较老的项目,本身就还在采用纯 ...

  3. iOS自动布局框架-Masonry详解

    首先,在正式使用Masonry之前,我们先来看看在xib中我们是如何使用AutoLayout     从图中我们可以看出,只要设置相应得局限,控制好父视图与子视图之间的关系就应该很ok的拖出你需要的需 ...

  4. iOS 网络框架编写总结

    一,常用 1> 不错的处理接收到的网络图片数据的方法 id img= ISNSNULL(pic)?nil:[pic valueForKey:@"img"]; NSString ...

  5. iOS自动布局——Masonry详解

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由鹅厂新鲜事儿发表于云+社区专栏 作者:oceanlong | 腾讯 移动客户端开发工程师 前言 UI布局是整个前端体系里不可或缺的一环 ...

  6. iOS常用框架源码分析

    SDWebImage NSCache 类似可变字典,线程安全,使用可变字典自定义实现缓存时需要考虑加锁和释放锁 在内存不足时NSCache会自动释放存储的对象,不需要手动干预 NSCache的key不 ...

  7. iOS开发UI篇—无限轮播(功能完善)

    iOS开发UI篇—无限轮播(功能完善) 一.自动滚动 添加并设置一个定时器,每个2.0秒,就跳转到下一条. 获取当前正在展示的位置. [self addNSTimer]; } -(void)addNS ...

  8. Fastjson是一个Java语言编写的高性能功能完善的JSON库。

    简介 Fastjson是一个Java语言编写的高性能功能完善的JSON库. 高性能 fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jackson. ...

  9. iOS各框架功能简述以及系统层次结构简单分析

    iOS各个框架所对应的功能简单介绍 iOS系统结构层次:

随机推荐

  1. Codeforces Round #246 (Div. 2) B. Football Kit

    题目的意思是求出每个队穿主场衣服和客场衣服的次数 每个队作为主场的次数是n-1,作为客场的次数是n-1 当每个队打主场的时候肯定穿的主场衣服 当每个队打客场时,如果客场与主场的衣服不同,则穿客场衣服 ...

  2. [深入浅出Windows 10]模拟实现微信的彩蛋动画

    9.7 模拟实现微信的彩蛋动画 大家在玩微信的时候有没有发现节日的时候发一些节日问候语句如“情人节快乐”,这时候会出现很多爱心形状从屏幕上面飘落下来,我们这小节就是要模拟实现这样的一种动画效果.可能微 ...

  3. [Cocos2D-x For WP8]CocosDenshion音频播放

    Cocos2D-x的音频分为长时间的背景音乐和短的音效两种,我们可以通过SimpleAudioEngine::sharedEngine()方法来获取音频播放的引擎,然后调用对音频相关的操作方法就可以了 ...

  4. [BZOJ2794][Poi2012]Cloakroom

    2794: [Poi2012]Cloakroom Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 167  Solved: 119[Submit][St ...

  5. URAL 1203. Scientific Conference(瞎搞)

    题目链接 本来觉得这不是经典的贪心吗..果断水一次,wa了,看了看discuss,发现貌似不好水,土土的DP了一下,复杂度很高了,又T了...然后想想单调队列,二分什么的...不好往上加,直接搞了标记 ...

  6. SQL笔记----在一个关系表中操作列

    使用alter关键字,可以为一个表添加新的列. 比如: 给Persons的表中添加一列,名字为Birthday,类型是date. ALTER TABLE Persons ADD Birthday da ...

  7. svn学习笔记(1)入门学习----安装及创建运行仓库

    学习及使用svn有一段时间了,但是以前学习的时候不怎么用,现在用只是简单的更新上传,又把基本理论忘了.为了以后自己看自己的笔记回忆,特此记录 svn学习博客:http://www.cnblogs.co ...

  8. VirtualMachine所支持的操作

    在JDK中com.sun.tools.attach.VirtualMachine提供了一些从外部进程attach到jvm上,并执行一些操作的功能.VirtualMachine的子类HotSpotVir ...

  9. Unity学习疑问记录之Apply Root Motion

    Should we control the character's position from the animation itself or from script. 如果我们勾选了Animator ...

  10. JS开发windows phone8.1系列之3

    http://msdn.microsoft.com/zh-cn/library/windows/apps/dn629638.aspx 这部分主要是使用页面导航 管理方式,在程序的default.htm ...