ios开发UIView在iphone5下自适应问题

本文详细解析了在iOS开发中,UIViewController视图加载过程中的自适应布局问题。介绍了视图从加载到显示过程中不同阶段视图尺寸的变化规律,强调了在viewWillAppear方法中进行尺寸依赖的布局调整是最为合适的时机。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的UIViewController的Xib里面的View是 3.5寸的即(320*480)的。

在设置自适应自动伸长以后,经常会在ViewDidLoad方法里面初始化一些界面,这时候使用主self.view.frame时,发现frame和xib里面的尺寸是一样的,并没有自适应伸长,结果很多subview错位了,上网查了下,在stackoverflow上发现一段说明:

 

The frame is not guaranteed to be the same in viewDidLoad as it will be when the view is eventually displayed. UIKit adjusts the frame of your view controller's view prior to displaying it, based on the context in which will appear. The size is determined based on interface orientation and the dimensions of any visible navigation bar, tab bar, toolbar, or status bar (which itself has a height that can change, e.g. when you're on a phone call).

It helps to understand what happens when a view controller's view is loaded and displayed:

  1. Something accesses your view controller's view property for the first time. This may occur in your own code, or in UIKit in response to a user action like selecting a tab.

  2. UIKit lazy-loads your view controller's view by calling loadView if it's defined, or by loading the view from the NIB that was specified in initWithNibName:bundle:. If neither exists, UIKit just loads an empty view.

  3. UIKit calls viewDidLoad once the view and its subviews have been fully loaded. At this point the view's frame will be whatever it was set to in the NIB, or in loadView.

  4. Something calls for UIKit to display your view controller's view. Again, this may be a user action like tapping on a tab, or an explicit method call in your code like pushViewController:animated: orpresentModalViewController:animated:.

  5. UIKit resizes the view based on the context in which it will be presented, as described above.

  6. UIKit calls viewWillAppear:The frame should now be the size that will be displayed.

  7. UIKit displays the view, with or without animations.

  8. UIKit calls viewDidAppear:.

As you can see, if you need to know the size of your view's frame before it gets presented,viewWillAppear: is your one and only opportunity. Just remember that this size may change after the view appears for various reasons, including rotation events or changes in status bar height. For this reason, it's important to give every subview an appropriate autoresizingMask, to ensure that the layout can adjust itself properly for any change in bounds.

If you wish to build your view hierarchy manually, the recommended place to do so is in loadView. Since you construct the view yourself in this method, you can initialize its frame to whatever you'd like. The size you choose doesn't matter much, since UIKit is likely to change it on you anyway. Just make sure you set your autoresizingMasks appropriately.

其实代码是没错的,不管是在xib里面设置自适应,还是在ViewDidLoad方法里卖弄设置,得到的self.view.frame始终是和xib里面的尺寸是一样的,这时你就郁闷了,因为很多subView的尺寸要根据self.view的尺寸来设置。

所以对subView的一些适应性设置,不能在Viewdidload里面,而是需要在viewWillAppear和viewDidAppear里面完成,在这两个方法里面self.view.frame是自适应之后的值。

自己认为最好的方式,还是对subview设置好合适autoresizingMasks。

内容概要:本文档详细介绍了基于弹性架构搜索(Elastic Architecture Search, EAS)结合Transformer编码器进行多变量时间序列预测的项目实例。项目旨在自动化优化多变量时间序列预测模型结构,提升预测精度与鲁棒性,降低计算资源消耗,实现模型轻量化。通过MATLAB实现,项目采用Transformer编码器的多头自注意力机制,结合EAS的弹性权重共享和分阶段搜索策略,解决了高维多变量时间序列的复杂依赖建模、架构搜索计算资源需求高、模型过拟合、多步预测误差积累、数据异构性与缺失值处理、复杂模型训练收敛等挑战。最终,项目构建了一个高度模块化和可扩展的系统设计,适用于智能制造、能源管理、智慧交通等多个工业场景。 适合人群:具备一定编程基础,对时间序列预测、深度学习及MATLAB有一定了解的研发人员和研究人员。 使用场景及目标:①自动化优化多变量时间序列预测模型结构,提升预测精度与鲁棒性;②降低计算资源消耗,实现模型轻量化;③实现高度模块化与可扩展的系统设计,促进人工智能在工业领域的深度应用;④提供科研与教学的典范案例与工具,探索深度学习架构搜索在时序预测的前沿技术;⑤促进多变量时序数据融合与异质信息处理能力,推动MATLAB深度学习工具箱的应用与扩展。 其他说明:项目不仅聚焦于模型性能提升,更注重计算资源节约和应用落地的可行性。借助弹性架构搜索自动化调参,减少人工经验依赖,加快模型迭代速度,降低开发门槛。结合Transformer编码器的表达能力,显著改善多变量时间序列预测中的长期依赖捕捉和异质数据融合问题,为各类时间序列分析任务提供一种全新的解决方案。项目通过详细的代码实现和注释,帮助用户理解Transformer机制与弹性架构搜索如何协同工作,实现多变量时间序列预测。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值