美文网首页swiftiOS 视图
SDAutoLayout的使用

SDAutoLayout的使用

作者: 恩哼恩哼蹦擦擦GH | 来源:发表于2019-06-03 18:03 被阅读0次

    1. 基础属性

    ##########

    1. left, right, top, bottom//左边, 右边, 上边, 下边

    2.width, height, maxWidth, maxHeight, minWidth, minHeight;//设置宽度, 高度, 最大宽度, 最大高度, 最小宽度, 最小高度

    3,centerX, centerY;//中心X轴, 中心Y轴

    4 offset(偏移量, 原点在左上角, 偏移远离值为正, 偏移靠近值为负);

    2.##########关系

    1, equalTo

    2.spaceTo

    3,ratioTo

    4.is

    #######################方法

    leftEqualToView(<UIView>) //左边与目标左边齐平

    rightEqualToView(<UIView>) //右边与目标右边齐平

    topEqualToView(<UIView>) //顶边与目标顶边齐平

    bottomEqualToView(<UIView>) //底边与目标底边齐平

    centerXEqualToView(<UIView>) //中心点X坐标与目标中心点X坐标相同

    centerYEqualToView(<UIView>) //中心点Y坐标与目标中心点Y坐标相同

    widthEqualToHeight() //自身宽高相等

    heightEqualToWidth() //自身高宽相等

    leftSpaceToView(<UIView>,<CGFloat>) //左边到目标view的距离,当目标是父视图,参照的是目标的左边(同边),否则,参照的是目标的右边(对边)

    rightSpaceToView(<UIView>,<CGFloat>) //右边到目标view的距离(同理)

    topSpaceToView(<UIView>,<CGFloat>) //顶边到目标view的距离(同理)

    bottomSpaceToView(<UIView>,<CGFloat>) //底边到目标view的距离(同理)

    spaceToSuperView(UIEdgeInsetsMake(top, left, bottom, right)) //快捷设置到父视图的上左下右边距

    centerXIs(<CGFloat>) //设置中心点X坐标

    centerYIs(<CGFloat>) //设置中心点Y坐标

    xIs(<CGFloat>) //原点x坐标

    yIs(<CGFloat>) //原点y坐标

    widthIs(<CGFloat>) //宽度

    heightIs(<CGFloat>) //高度

    maxWidthIs(<CGFloat>) //最大宽度

    maxHeightIs(<CGFloat>)  //最大高度

    minWidthIs(<CGFloat>) //最小宽度

    minHeightIs(<CGFloat>) //最小高度

    widthRatioToView(<UIView>,<CGFloat>) //宽度是目标宽度的多少倍

    heightRatioToView(<UIView>,<CGFloat>) //高度是目标高度的多少倍

    autoHeightRatio(<CGFloat>) //自身高宽比;特别的,label传入0,可实现文字高度的自适应

    autoWidthRatio(<CGFloat>) //自身的宽高比

    当需要cell自适应的时候时候, 需要在cell里面添加完约束之后添加

    //添加这句话目的就是为了cell高度自适应, 根据谁的底部进行适配高度就好了,lable1是cell最底部的视图, 10, 距离底部多少

        [self setupAutoHeightWithBottomView:self.lable1 bottomMargin:10];

    当赋值之后需要更改某一个视图的frame时, 只需要

     [self updateLayout];更新约束

    计算cell高度

    - (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

        return [self cellHeightForIndexPath:indexPath cellContentViewWidth:[UIScreen mainScreen].bounds.size.width tableView:tableView];

    }

    /** 

     * 设置类似collectionView效果的固定间距自动宽度浮动子view 

     * viewsArray      : 需要浮动布局的所有视图

     * perRowItemsCount : 每行显示的视图个数

     * verticalMargin  : 视图之间的垂直间距

     * horizontalMargin : 视图之间的水平间距

     * vInset          : 上下缩进值

     * hInset          : 左右缩进值

     */

    - (void)setupAutoWidthFlowItems:(NSArray*)viewsArray withPerRowItemsCount:(NSInteger)perRowItemsCount verticalMargin:(CGFloat)verticalMargin horizontalMargin:(CGFloat)horizontalMagin verticalEdgeInset:(CGFloat)vInset horizontalEdgeInset:(CGFloat)hInset;

    /** 

     * 设置类似collectionView效果的固定宽度带自动间距浮动子view 

     * viewsArray      : 需要浮动布局的所有视图

     * perRowItemsCount : 每行显示的视图个数

     * verticalMargin  : 视图之间的垂直间距

     * vInset          : 上下缩进值

     * hInset          : 左右缩进值

     */

    - (void)setupAutoMarginFlowItems:(NSArray*)viewsArray withPerRowItemsCount:(NSInteger)perRowItemsCount itemWidth:(CGFloat)itemWidth verticalMargin:(CGFloat)verticalMargin verticalEdgeInset:(CGFloat)vInset horizontalEdgeInset:(CGFloat)hInset;

    ######UIScrollewView

    ScrollerView的高度需要写死, 而constentSize需要根据最后一个view进行自适应就可以了

    /** 设置scrollview内容竖向自适应 */把最后一个view写进去就好了

    - (void)setupAutoContentSizeWithBottomView:(UIView*)bottomView bottomMargin:(CGFloat)bottomMargin;

    /** 设置scrollview内容横向自适应 */把最右边的view写进去就好了

    - (void)setupAutoContentSizeWithRightView:(UIView*)rightView rightMargin:(CGFloat)rightMargin;

    ############UIButton-----宽度自适应

    /*

     * 设置button根据单行文字自适应

     * hPadding:左右边距

     */

    - (void)setupAutoSizeWithHorizontalPadding:(CGFloat)hPadding buttonHeight:(CGFloat)buttonHeight;

    相关文章

      网友评论

        本文标题:SDAutoLayout的使用

        本文链接:https://www.haomeiwen.com/subject/ekbcxctx.html