It’s important to optimize any UIView layoutSubviews method you create, as it can be frequently called, and has the potential for creating recursion (triggering a setNeedsLayout from layoutSubviews can create a loop that will grossly affect your apps performance). Layout subviews is called once per run loop on any view that has had setNeedsLayout or setNeedsDisplayWithRect: called on it. So in addition to any time you manually call these methods, it can be useful to know when the UI framework calls setNeedsLayout/setNeedsDisplay as this will trigger layoutSubviews.
For this purpose, I will define a few view relationships:
- View1 – UIView class, root view for examples
- View1.1 – UIScrollView class, subview of View1
- View1.1.1 – UIView class, subview of View1.1 (No autoresize mask)
- View1.1.2 – UIView class, another subview of View1.1 (Autoresize mask – flexible width)
I then ran the following tests. An X means the view was layed out
From this I surmise the following:
- init does not cause layoutSubviews to be called (duh)
- addSubview causes layoutSubviews to be called on the view being added, the view it’s being added to (target view), and all the subviews of the target view
- setFrame intelligently calls layoutSubviews on the view having it’s frame set only if the size parameter of the frame is different
- scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and it’s superview
- rotating a device only calls layoutSubview on the parent view (the responding viewControllers primary view)
- removeFromSuperview – layoutSubviews is called on superview only (not show in table)
Hopefully this is helpful information for you as well.
分享到:
相关推荐
As of iOS 6.0, when constraints-based layout is used the base implementation applies the constraints-based layout, otherwise it does nothing. layoutSubviews方法的具体作用 layoutSubv
How to subclass UIView and make friends with layoutSubviews How to build programmatic layouts without storyboards What is Auto Layout? What is a constraint? Who owns a constraint? How many ...
您可以在Interface Builder中使用“自动布局”,可以在代码中使用“自动布局”并维护对这些约束的引用,或者可以使用layoutSubviews实现布局功能。 这些方法中的每一种都有优点和缺点。 如果您使用layoutSubviews...
索引条部分使用UIView 的 layoutSubviews绘制字母和边框线 setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到UIGraphicsGetCurrentContext,就可以画画了。而setNeedsLayout会默认调用layoutSubViews,就...
自定义UITabBar,layoutSubviews重写UITabBarButton位置,重写则hitTest方法并监听按钮的点击 自定义的UITabBarController和UINavigationController
需求: 中间按钮 需要个性化 思路 : 利用KVC 对 TabBarController 的 tabBar 赋值 然后 子类化 UITabBar 重写 layoutSubviews 找到 UITabBarButton 重新布局
自定义警告框、弹出框AlertView 自定义AlertView 实现自定义UIAlertView。继承UIAlertView,改写其中的layoutSubviews方法,用于自定义UIAlertView的外观
IOS购物车UIButton版本备忘笔录UIButton代码://别忘了调用父类的layoutSubviews方法//设置图片及文字的位置大小//获取数据进按钮
需求 之前已经实现了自定义TabBar,如图所示: 自定义TabBar.jpeg 现在需要实现一个类似今日头条TabBar的功能 —— 如果继续点击当前...1. 自定义TabBar的layoutSubviews中绑定事件 - (void)layoutSubviews { [supe
适用于iOS的故事书 适用于iOS的Storybook是一个可提高UI开发速度的库。 它使我们能够独立预览UI可以显示的每个状态的组件。... public override func layoutSubviews () { super . layoutSubviews () backgroundColo
自定义tabBar,简单,易用 使用说明: 1.修改初始化数据 #pragma mark 初始化数据 在 -(instancetype)init 方法里面 2.修改底部图片文字布局 在TabBarButton.m 文件的 layoutSubviews 方法里面
2018.7.7更新 1.更改masonry约束布局为frame布局 2.支持设置角标 3.支持自定义角标颜色和背景颜色 4.支持清除角标 自定义类似淘宝TabBar 公司的项目需求是tabbar和...- (void)layoutSubviews { [super layoutSubviews];
7.自定义的tabbar类的的UITabbarItem的layoutSubViews方法中布局所有的字视图; 8.记录按钮的tag,设置当前选中按钮,同时在按钮事件中设置选中和取消选中状态; 9.在tabbar类设置代理,回传选中按钮的tag,用来...
项目中用到带有带有UITableView的UIAlertView,刚开始的做法 是 UIAlertView 的基础上addsubview上UITableView,...重写了 layoutSubviews方法,可以用于横竖屏切换 ,UITableView的位置大小 在 UIAlertView内。
在使用这份代码时遇到一点问题,提出了他自己的解决方案:这个代码中reloaddata后,调用layoutsubviews应移除掉之前的subviews,然后加上[[_scrollView subviews] makeObjectsPerformSelector:@selector...
Semi Chircle仪表板示例: let dashCircle = DYDashboardCircle()dashCircle.value(to: 0.9, animated: true)dashCircle.layoutSubviews()view.addSubview(dashCircle)其他配置: var circleColors: [UIColor]任何...
@flycent_stars 在使用这份代码时遇到一点问题,提出了他自己的解决方案:这个代码中reloaddata后,调用layoutsubviews应移除掉之前的subviews,然后加上[[_scrollView subviews] makeObjectsPerformSelector:@...
代码是通过继承UIButton,然后再重写layoutSubviews方法,对自带的图片和titleLabel进行重新的layout,代码如下: // // ZZZUpDownButton.h // // Copyright :copyright: 2016年 George. All rights reserved. // /...
LayoutLoopHunter 该库通过在实时代码中复制UIViewLayoutFeedbackLoopDebuggingThreshold的行为来帮助捕获由自动布局反馈循环引起的OOM。 这是上的运行时教程的最终... 当在单个运行循环中多次调用layoutSubviews