天天看点

iOS MyLayout使用笔记

前言

记录一些使用过程中需要注意的地方。

目录

  • 5.​

    ​布局​

    ​​视图添加到​

    ​UIScrollView​

    ​​上,​

    ​UIScrollView​

    ​ 无法滚动。(2022-02-09 11:28:29)
  • 4.​

    ​布局​

    ​​视图添加到​

    ​非布局​

    ​视图,先添加,后设置约束,会导致约束无效,应该先设置约束,再添加到父视图。(2020-09-25 16:33:09)
  • 3.表格布局下,可以设置某一行或某一列的分隔线颜色,或者进一步设置某个单元格的分隔线颜色。(2020-01-06 16:23:23)
  • 2.线性布局下,设置子视图整体居中的情况下,还可以设置某个视图的居中偏移量。(2019-12-26 18:43:40)
  • 1.​

    ​布局​

    ​​视图添加到​

    ​UIScrollView​

    ​上,滚动到底部时,出现抖动的现象。(2019-12-16 11:16:17)

正文

  • 5.​

    ​UIScrollView​

    ​ 无法滚动
  • iOS MyLayout使用笔记
  • 由于​

    ​MyFloatLayout​

    ​​ 设置了​

    ​自动高度​

floatLayout.myHeight = MyLayoutSize.wrap;      

所以 floatLayout 自身调整高度的时机是:

1.添加到父视图时

2.调用 ​​

​[floatLayout layoutIfNeeded];​

​ 重新布局

经过 ​

​Demo​

​ 调试,正确方式之一如下:

// 先添加
[self.scrollView addSubview:floatLayout];
// 重新布局(加上这句,UIScrollView 才能滚动)
[floatLayout layoutIfNeeded];      

当然,还有 ​

​其他​

​​ 的处理方式:

考虑到 ​​

​floatLayout​

​​ 内部有对 ​

​UIScrollView​

​​ 进行处理(当 ​

​UIScrollView​

​​ 是其父视图时)

就加一个 ​​

​中间层​

[_itemView addSubview:floatLayout];
[floatLayout layoutIfNeeded];
_itemView.frame = floatLayout.bounds;

[self.scrollView addSubview:self.itemView];      

当然,如果 ​

​仅仅​

​​ 是要处理 ​

​UIScrollView​

​​ 的 ​

​滚动​

​​ 问题

只要设置布局视图的一个属性就好了(当然,这是一位熟练掌握了​​

​MyLayout​

​ 的大佬告诉我的 😂),不需要多加一个中间层了

floatLayout.adjustScrollViewContentSizeMode = MyAdjustScrollViewContentSizeModeNo;      

这个属性默认的动作是:

​​

​任何加入到 UIScrollView 中的布局视图,在尺寸变化时,都会调整和设置 contentSize 的值​

针对具体情况,这里由于​

​floatLayout​

​​ 不是最后一个视图,它后面的视图位置对它有依赖

所以,还是需要调用

[floatLayout layoutIfNeeded];      

强制布局,计算自身高度

总结一下:

要么

// 先添加
[self.scrollView addSubview:floatLayout];
// 重新布局(加上这句,UIScrollView 才能滚动)
[floatLayout layoutIfNeeded];      

要么

// 设置属性
floatLayout.adjustScrollViewContentSizeMode = MyAdjustScrollViewContentSizeModeNo;

// 再计算高度
[floatLayout layoutIfNeeded];  // 这种可以
// CGSize size = [floatLayout sizeThatFits:(CGSizeMake(self.scrollView.frame.size.width, 0))]; // 这样也行      

最后,附上 ​​Demo​​

  • 4.添加顺序
// 约束有效
MyLinearLayout *subView1 = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
subView1.myWidth = 150;
[self addSubview:subView1];

// 约束无效
MyLinearLayout *subView2 = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
[self addSubview:subView2];
subView2.myWidth = 150;      
  • 3.分隔线颜色
// 设置最后一行底部颜色为 红色
MyLinearLayout *lastRow = [self.tableLayout addRow:60 colSize:MyLayoutSize.fill];
lastRow.notUseIntelligentBorderline = YES;
lastRow.topBorderline = [[MyBorderline alloc] initWithColor:[UIColor redColor]];      
// 设置某个 cell 右边的颜色
MyFrameLayout *cell = [MyFrameLayout new];
cell.notUseIntelligentBorderline = YES;
cell.rightBorderline = [[MyBorderline alloc] initWithColor:[UIColor blueColor]];
[tableLayout addSubview:cell];      
  • 2.整体居中时,个别居中偏移量
// 整体居中
_rootLayout.gravity = MyGravity_Horz_Center;

// 个别居中向左偏移10
_subView.centerXPos.equalTo(_rootLayout).offset(-10);      
  • 1.抖动现象

使用动态布局时,如果你的根界面是一个​

​UIScrollView​

​​视图,又同时设置了滚动视图的

​​

​scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;​

​ 那么就有可能在滚动到底部时出现抖动的现象。

总有一款能满足你的验证码输入框