Giter Site home page Giter Site logo

gsdios / sdautolayout Goto Github PK

View Code? Open in Web Editor NEW
5.9K 228.0 1.3K 15.22 MB

One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime.

License: MIT License

Objective-C 99.45% Ruby 0.06% Swift 0.49%

sdautolayout's Introduction

SDAutoLayout(一行代码搞定自动布局!)

一行代码搞定自动布局!致力于做最简单易用的Autolayout库。The most easy way for autolayout.

字节跳动 iOS 岗位招聘

北上广深杭大量招人,各种研发岗位都在找,有意向者可以直接点击后面链接投递简历或者联系我 [email protected] https://job.toutiao.com/s/eL91QMm,

众多公司和个人开发者已经使用本库布局:

SDAutoLayout使用者开发的部分app截图 http://www.jianshu.com/p/9bc04d3effb8

技术支持(QQ交流群):

497140713(1群) 519489682(2群已满)

Pod支持:

支持pod: pod 'SDAutoLayout', '~> 2.1.3'

更新记录:

2018.11.28 -- 修复部分开发者违规调用load的super方法导致自动布局失效的问题

2018.07.02 -- 修复了部分布局组合下view居中布局失效bug

2017.11.11 -- 实现控制富文本可显示行数功能

2017.11.11 -- 解决朋友圈demo在iOS11上文字收折或者展开时出现cell跳动问题

2017.06.26 -- 解决部分开发者反应因出现“UITableViewCellContentView”而导致应用审核被拒问题

2016.08.12 -- 实现在tableview插入新的cell数据时自动缓存管理

2016.06.30 -- 增加多参照view的leftSpaceToView和topSpaceToView约束,例:topSpaceToView(@[self.view3, self.view4], 30)

2016.06.24 -- 修复给button设置约束时在iOS8.x系统中出现的崩溃问题;发布2.1.2版本

2016.06.23 -- 实现删除某行cell时自动调整height缓存

2016.05.16 -- 修复用xib生成的view出现的部分约束失效问题(发布pod2.0.0版本)

2016.05.15 -- 增加设置偏移量offset功能

2016.04.30 -- 修复之前button作为父视图时内部控件不能自动布局问题

2016.04.05 -- 修复宽度自适应label在重用时候偶尔出现的宽度计算不准确的问题(发布pod1.51版本)

2016.03.23 -- 升级了缓存机制,新版本在tableview滑动cell时候流畅度和性能提升20%以上(发布pod1.50版本)

2016.01.23 -- 增加label对attributedString的内容自适应

2016.01.21 -- 实现tableview局部刷新cell高度缓存的自动管理

2016.01.20 -- demo适配在ios7上的屏幕旋转问题

2016.01.18 -- 推出“普通简化版”tableview的cell自动高度方法(推荐使用),原来的需2步设置的普通版方法将标记过期

2016.01.13 -- 增加在不确定bottom view的情况下的cell高度自适应方法

2016.01.07 -- 1.增加 scrollview 横向内容自适应功能;2.增加view宽高相等的功能

2016.01.03 -- 增加任何类型对象都可以实现一行代码搞定cell高度自适应;增加文档注释

2015.12.08 -- 重大升级:1.支持scrollview内容自适应;2.任意添加或者修改约束不冲突;3.性能提升40%以上;4.添加最大、最小宽高约束

视频教程:

☆☆ SDAutoLayout 基础版视频教程:http://www.letv.com/ptv/vplay/24038772.html ☆☆

☆☆ SDAutoLayout 进阶版视频教程:http://www.letv.com/ptv/vplay/24381390.html ☆☆

☆☆ SDAutoLayout 原理简介视频教程:http://www.iqiyi.com/w_19rt0tec4p.html ☆☆

☆☆ SDAutoLayout 朋友圈demo视频教程:http://v.youku.com/v_show/id_XMTYzNzg2NzA0MA==.html ☆☆

部分SDAutoLayout的DEMO:

完整微信Demo https://github.com/gsdios/GSD_WeiXin

用法简介

普通view的自动布局:

用法示例

 用法一
    _view.sd_layout
    .leftSpaceToView(self.view, 10)
    .topSpaceToView(self.view, 80)
    .heightIs(130)
    .widthRatioToView(self.view, 0.4);  
用法二 (一行代码搞定,其实用法一也是一行代码)
    _view.sd_layout.leftSpaceToView(self.view, 10).topSpaceToView(self.view,80).heightIs(130).widthRatioToView(self.view, 0.4);
综合示例
    
    *******************************************************************************
        
        注意:先把需要自动布局的view加入父view然后在进行自动布局,例: 

        UIView *view0 = [UIView new];
        UIView *view1 = [UIView new];
        [self.view addSubview:view0];
        [self.view addSubview:view1];
        
        view0.sd_layout
        .leftSpaceToView(self.view, 10)
        .topSpaceToView(self.view, 80)
        .heightIs(100)
        .widthRatioToView(self.view, 0.4);
        
        view1.sd_layout
        .leftSpaceToView(view0, 10)
        .topEqualToView(view0)
        .heightRatioToView(view0, 1)
        .rightSpaceToView(self.view, 10);

    *******************************************************************************

UILabel文字自适应

// autoHeightRatio() 传0则根据文字自动计算高度(传大于0的值则根据此数值设置高度和宽度的比值)
    _label.sd_layout.autoHeightRatio(0);

tableview和cell高度自适应:

普通(简化)版【推荐使用】:tableview 高度自适应设置只需要2步

    1. >> 设置cell高度自适应:
    // cell布局设置好之后调用此方法就可以实现高度自适应(注意:如果用高度自适应则不要再以cell的底边为参照去布局其子view)

    [cell setupAutoHeightWithBottomView:_view4 bottomMargin:10];
    2. >> 获取自动计算出的cell高度

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        id model = self.modelsArray[indexPath.row];
        // 获取cell高度
        return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[DemoVC9Cell class]  contentViewWidth:cellContentViewWith];
    }

升级版(适应于cell条数少于100的tableview):tableview 高度自适应设置只需要2步

    1. >> 设置cell高度自适应:
    // cell布局设置好之后调用此方法就可以实现高度自适应(注意:如果用高度自适应则不要再以cell的底边为参照去布局其子view)

    [cell setupAutoHeightWithBottomView:_view4 bottomMargin:10];
    2. >> 获取自动计算出的cell高度 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // 获取cell高度
    return [self cellHeightForIndexPath:indexPath cellContentViewWidth:[UIScreen mainScreen].bounds.size.width];
    }

自动布局用法简析

   1.1 > leftSpaceToView(self.view, 10)
   
   方法名中带有“SpaceToView”的方法表示到某个参照view的间距,需要传递2个参数:(UIView)参照view 和 (CGFloat)间距数值

   1.2 > widthRatioToView(self.view, 1)
   
   方法名中带有“RatioToView”的方法表示view的宽度或者高度等属性相对于参照view的对应属性值的比例,需要传递2个参数:(UIView)参照view 和 (CGFloat)倍数
 
   1.3 > topEqualToView(view)
   
   方法名中带有“EqualToView”的方法表示view的某一属性等于参照view的对应的属性值,需要传递1个参数:(UIView)参照view
   
   1.4 > widthIs(100)
   
   方法名中带有“Is”的方法表示view的某一属性值等于参数数值,需要传递1个参数:(CGFloat)数值

PS

// 如果需要用“断言”调试程序请打开此宏(位于UIView+SDAutoLayout.h)

//#define SDDebugWithAssert

sdautolayout's People

Contributors

fanyuexiang avatar gsdios avatar lixiang1994 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sdautolayout's Issues

我发现demo里又有一个小bug,在DemoVC0

原来的:

  • (void)animation
    {
    if ( _widthRatio == 0.4 ) {
    _widthRatio = 0.1;
    } else {
    _widthRatio = 0.4;
    }
    [UIView animateWithDuration:0.8 animations:^{
    self.view0.sd_layout
    .widthRatioToView(self.view, _widthRatio);
    [self.view0 updateLayout];
    [self.view5 updateLayout];
    }];

    /*

    SDAutoLayout(新建QQ交流群:497140713)
    github:https://github.com/gsdios/SDAutoLayout
    ☆☆ SDAutoLayout 视频教程:http://www.letv.com/ptv/vplay/24038772.html ☆☆
    一行代码搞定自动布局!致力于做最简单易用的Autolayout库。The most easy way for autolayout.

    */

}

应该改成以下:

  • (void)animation
    {
    if ( fabs(_widthRatio - 0.4) < 0.00001 ) {
    ......

}

9宫格图片在加载一张网络图片时 无法显示 代码如下 100的地方请自行根据图片设置

  • (void)setPicPathStringsArray:(NSArray *)picPathStringsArray
    {
    _picPathStringsArray = picPathStringsArray;

    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    if (_picPathStringsArray.count == 0) {
    self.height = 0;
    self.fixedHeight = @(0);
    return;
    }

    CGFloat itemW = [self itemWidthForPicPathArray:_picPathStringsArray];
    CGFloat itemH = 0;
    if (_picPathStringsArray.count == 1) {
    UIImage *image = [UIImage imageNamed:_picPathStringsArray.firstObject];
    if (image.size.width) {
    itemH = image.size.height / image.size.width * itemW;
    }
    } else {
    itemH = itemW;
    }
    long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray];
    CGFloat margin = 10;

    [_picPathStringsArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    long columnIndex = idx % perRowItemCount;
    long rowIndex = idx / perRowItemCount;
    UIImageView *imageView = [UIImageView new];

    //加载网络图片
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
    //判断是多张图片还是一张图片
    if (_picPathStringsArray.count > 1) {
        imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
    }else{
        imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), 100, 100);
    }
    
    [self addSubview:imageView];
    
    imageView.userInteractionEnabled = YES;
    imageView.tag = idx;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
    [imageView addGestureRecognizer:tap];
    

    }];

    CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
    int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount);
    CGFloat h = columnCount * itemH + (columnCount - 1) * margin;

    //NSLog(@"高度为%f 宽度为%f",h,w);
    if (_picPathStringsArray.count == 1) {
    h = 100;
    }

    self.width = w;
    self.height = h;

    self.fixedHeight = @(h);
    self.fixedWith = @(w);
    }

貌似跟什么库有冲突 总是crash

调用Demo里面的DemoCell, ViewController是代码代码创建的,其他的完全一样, 每次异常定位到这里

  • (void)setWidth:(CGFloat)width {
    if (self.ownLayoutModel.widthEqualHeight) {
    if (width != self.height) return;
    }
    CGRect frame = self.frame;
    frame.size.width = width;
    if (self.ownLayoutModel.heightEqualWidth) {
    frame.size.height = width;
    }
    self.frame = frame;
    }
    显示是frame的width是NaN导致异常

pod配置
platform :ios, "8.0"
use_frameworks!
pod 'AFNetworking','> 3.0.4'
pod 'ReactiveCocoa', '
> 2.5’
pod 'Masonry'
pod 'FDStackView', '1.0'
pod 'YYKit'
pod 'SVProgressHUD', '> 2.0-beta8'
pod 'Charts', '
> 2.2.1'
pod 'FMDB'

技术交流

你这是基于frame布局我写了一款基于autolayout布局
WHC_AutoLayoutKit:https://github.com/netyouli/WHC_AutoLayoutExample
WHC_AutoLayoutKit设计**借鉴android相对布局和线性布局(垂直和横向)
高度和宽度可按容器权重weight自动拉伸(一排很多视图时或者垂直方向很多视图时)

能不能自动设置图片的大小?

今天刚开始用,确实不错,好评一个。
但发现布局图片的时候,必须要明确的指定图片的宽高才能正确显示图片,感觉这样挺麻烦的。
因为之前一直在用Masonry,只要它确定了 imageView 的位置,就会自动按图片的大小显示出来,而不用人为的去设置死的数值。这样感觉是很方便的,UI 那边切出来的图都是合适的大小了,本身就不需要再重新给值。
如果你的框架里本身就有这样的功能那最好,示例一下就好了;没有的话希望可以添加。
谢谢。

leftEqualToView,topEqualToView等xxEqualToView的方法少了边距的设置

如果我有这种需求:
view0,view1上下两行排列,view1需要对view0的左边偏移一个距离,这时候,leftEqualToView没法满足这个要求,在你的demo5里,我试着用xIs方法来设置图片_view3的左边距离内容label(_view2)偏移10,如下代码:
_view3.sd_layout
.topSpaceToView(_view2, 10)
.xIs(_view2.left + 10)
.widthRatioToView(_view2, 0.7);
但是查看了你的left方法,仅仅是返回了fram的原点横坐标,这时候横坐标明显是0,所以图片只是显示在了cell左边沿10的位置.
然后有这种需求时,该怎么实现?

Can not support CocoaPods

Hello!
When I execute
pod search SDAutoLayout
in the terminal.
It shows this error :
[!] Unable to find a pod with name matching `SDAutoLayout'

So, I can't import your code into my project with cocoaPods.

demoVC9 部分优化

demoVC9 性能优化----在UITableViewDataSource中 不进行数据绑定, 在cell出现之前就会运行的方法 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 进行数据绑定
cell内容图片为3时,展示为一行
在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 中布局cell高度时,有个小建议

Cell 的自适应

/* model 为模型实例, keyPath 为 model 的属性名,通过 kvc 统一赋值接口 */
return [self.tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model"];

在实际应用中,可能是通过一个方法,给cell传递很多参数,而这些参数封装在一个Model里面的话有时并不合适,能否添加一个能通过方法赋多个值的途径?

如何设置子视图在父视图上面自适应居中

设置子视图居中只能设置固定的值widthIs和heightIs,在父视图自适应的时候,子视图大小不变时就显示不居中了,不能参照父视图直接设置居中吗?centerXEqualToView和centerYEqualToView要参照兄弟视图才行,如果没有兄弟视图,第一个添加的字视图如何设置居中?

有个问题

里面其实都是手动计算的,也没有做异步处理,这样稍微复杂些的页面,性能跟不上的吧

cell中高度的自适应

// >>>>>>>>>>>>>>>>>>>>> * cell自适应步骤2 * >>>>>>>>>>>>>>>>>>>>>>>>
/* model 为模型实例, keyPath 为 model 的属性名,通过 kvc 统一赋值接口 */
return [self.tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model"];

问下这里的keyPath具体的应用场景下应该写什么?

发现一个widthRatioToView使用bug

使用widthRatioToView与heightRatioToView函数时,传入的第二个参数必须为float类型。
这样就可以:widthRatioToView(self.view1, 0.5).heightRatioToView(self.view1, 0.5);
这样就不行:widthRatioToView(self.view1, 1/2).heightRatioToView(self.view1, 1/2);
这样也不行:widthRatioToView(self.view1, (1/2)).heightRatioToView(self.view1, (1/2));

您好,学习中有几个疑问在此请教一下

demo里面仿微信界面有些疑问:

  1. self.fixedHeight = @(h);
    self.fixedWith = @(w);
    这里“self.fixedHeight”和“self.fixedWith”具体什么意思呢 ?

2.代码里面“// ----------------- 以下为此库内部需要用到的类和方法(可以不用看)----------------”
下面的属性能否注释下呢,方便理解

如果在cell里面有UIView的嵌套,该如何做自适应?

cell里面包含了一个UIView A,A的高度不确定,A里面又包含了两个UIView B, C,其中B的高度动态变化,我在自定义cell类里调用了A的自适应高度方法,同时也调用了cell的自适应高度方法,然后cpu就飙升到100%了

功能修改

// 修改
@Property (nonatomic, copy, readonly) WidthHeightEqualToView widthRatioToView;
@Property (nonatomic, copy, readonly) WidthHeightEqualToView heightRatioToView;

// 新加
@Property (nonatomic, copy, readonly) WidthHeightEqualToView widthRatioToViewWidth;
@Property (nonatomic, copy, readonly) WidthHeightEqualToView widthRatioToViewHeight;
@Property (nonatomic, copy, readonly) WidthHeightEqualToView heightRatioToViewWidth;
@Property (nonatomic, copy, readonly) WidthHeightEqualToView heightRatioToViewHeight;

库中功能十分好用,如果能完善下面功能就更加完美了
(1)widthRatioToView heightRatioToView都是基于宽和宽 高和高的比例,缺少高和宽的比例约束
(2)没有等宽等高的约束

Xcode版本问题

// >>>>>>>>>>>>>> 多cell情景下调用以下方法(详细用法见demo7) >>>>>>>>>>>>>>>>>

  • (void)startAutoCellHeightWithCellClasses:(NSArray *)cellClassArray contentViewWidth:(CGFloat)contentViewWidth; // cellClassArray 为所有cell的类组成的数组,详细用法见demo7

这句class报错, Cannot find protocol ldeclaration for 'class",
是我的Xcode版本太低么?6.3

IOS7.1下横屏时仍然存在问题

1.DemoVC1先竖屏在横屏,有内存泄露;
2.DemoVC3横屏黄色的View位置不对
img_4435
3.DemoVC5,DemoVC7,DemoVC8和DemoVC9横屏cell的高度计算不对
img_4436
img_4437

请教一下用法问题?

想要设置一个View,相对于父View的中间偏上怎么弄?
API里只找到了centerYEqualToView,而没有centerYSpaceToView方法。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.