iOS MyLayout使用笔记
原创
©著作权归作者所有:来自51CTO博客作者haocold的原创作品,请联系作者获取转载授权,否则将追究法律责任
前言
记录一些使用过程中需要注意的地方。
目录
- 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
无法滚动 - 由于
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
// 约束有效
MyLinearLayout *subView1 = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
subView1.myWidth = 150;
[self addSubview:subView1];
// 约束无效
MyLinearLayout *subView2 = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
[self addSubview:subView2];
subView2.myWidth = 150;
// 设置最后一行底部颜色为 红色
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];
// 整体居中
_rootLayout.gravity = MyGravity_Horz_Center;
// 个别居中向左偏移10
_subView.centerXPos.equalTo(_rootLayout).offset(-10);
使用动态布局时,如果你的根界面是一个UIScrollView
视图,又同时设置了滚动视图的
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
那么就有可能在滚动到底部时出现抖动的现象。
这时候的解决方法是:
rootLayout.insetsPaddingFromSafeArea = ~UIRectEdgeBottom;
为了防止拉到底部时iPhoneX
设备的抖动发生,不能将底部安全区叠加到padding
中去。
也就是将滚动视图的子布局视图的这个底部叠加区域去掉即可。
总有一款能满足你的验证码输入框
https://github.com/xjh093/JHVerificationCodeView