UIView 和 AutoresizingMask 被忽略
第一次在堆栈溢出上发帖。
我花了几个小时搜索了许多 Google 搜索,此时,我几乎记住了 UIView 和 UIViewController 类参考文档。无论我做什么,我的应用程序都会忽略我调整方向变化视图大小的努力(或与此相关的任何其他框架大小修改)。
不涉及 nib 或 xib;我正在以编程方式构建所有视图和视图控制器,它们(视图控制器)都是我创建的三个自定义 UIViewController 子类之一的子类。这些超级类在其 loadView: 方法中具有以下几行...
[self setView:[[[UIView alloc] initWithFrame:[self viewFrame]] autorelease]];
[[self view] setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[[self view] setAutoresizesSubviews:TRUE];
在所有适当的 setter 方法中(换句话说,无论我在何处添加子视图),我都会重复应用 autoresizingMask 的步骤。似乎无论我做什么,结果总是相同的——我单击“构建”,单击“调试”,等待应用程序启动,旋转设备,然后就这样!视图会旋转,但根本不会调整大小。
我在这里缺少什么?这可能是我没有看到的显而易见的事情。非常感谢任何帮助,提前致谢。
First time posting on stack overflow.
I've spent hours scouring over many Google searches and have, at this point, practically memorized the UIView and UIViewController class reference docs. No matter what I do, my app is ignoring my efforts to resize my views on orientation change (or any other frame size modifications for that matter.)
There are no nibs or xibs involved; I am building all of my views and viewcontrollers programmatically, and they (the viewcontrollers) are all subclasses of one of three custom UIViewController subclasses that I have created. These super classes have the following lines in their loadView: method...
[self setView:[[[UIView alloc] initWithFrame:[self viewFrame]] autorelease]];
[[self view] setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[[self view] setAutoresizesSubviews:TRUE];
In all of the appropriate setter methods (wherever I add a subView in other words) I am repeating the steps of applying the autoresizingMask. It seems that no matter what I do, the outcome is always the same -- I click build, click debug, wait for the app to launch, rotate the device, and presto! The view rotates but does not resize at all.
What am I missing here? It's probably something obvious that I am just not seeing. Any help is truly appreciated, thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,果然,这是我自己的错。
第一课: 熬夜尝试修复损坏的代码对自己没有任何好处。休息一下吧!多喝水!如果你一直试图强迫你的大脑在就寝时间之后执行复杂的算法策略和逻辑,你只会让事情变得更糟。
事实证明,我在视图层次结构的底部有一个流氓 UIView,它没有autoresize 属性完全设置。我以为我已经把所有的东西都看完了,但结果发现我漏掉了一件。 (只是一个小视图,一整天的镜头!)
我可以对后来遇到类似挫折的任何人说,
Autoresizing
确实按照记录的那样工作。如果您认为“某些内容没有被调用”,那么您可能找错地方了。此外,UIViewAutoresizingMask
枚举常量的使用方式与 Interface Builder 中的使用方式并不完全相同。在IB中,您“锁定”边距,而在以编程方式设置边距时,默认情况下假定锁定边距,您可以通过将其设置为“灵活”来“解锁”它们。例如,仅设置FlexibleWidth和FlexibleHeight位相当于启用IB中的所有自动调整大小选项。通过添加任何边距掩码(即UIViewAutoresizingFlexibleLeftMargin
),您将“取消选择”IB 中相应的边距自动调整大小选项。 (我见过许多其他帖子,这似乎是一些人的主要困惑点。)然而,在我的研究过程中,我确实注意到似乎没有任何类型的事件、消息、通知,或者当 UIView 调整大小时,无论是否自动调整。虽然调整子视图的大小不是必需的,但如果您希望在框架大小发生变化时导致滚动视图或表视图滚动,那么最好知道它何时发生。我知道当键盘弹出时,人们可以轻松地做到这一点,因为周围有一个通知系统以及
TextField
delegate
方法。所以也许是另一篇文章的主题...无论如何,感谢所有参与 StackOverflow 的人!
Well, sure enough, it was my own fault.
Lesson number one: You are not doing yourself any favors by staying up all night trying to fix broken code. Get some rest! Drink more water! You will only probably make things worse if you keep trying to force your brain to perform complicated algorithmic strategy and logic past its bed-time.
Turns out I had a rogue UIView toward the bottom of the view hierarchy that did not have the autoresize property set at all. I thought I had looked through everything, but turns out that I missed one. (Just one little view, and an entire day shot!)
I can say for anyone who comes along later with similar frustration, that
Autoresizing
does indeed work as documented. If you think that "something is not being called" then you are probably looking in the wrong place. Also, theUIViewAutoresizingMask
enum constants are not used exactly like they are in Interface Builder. In IB, you "lock" margins, whereas in setting them programmatically, the locked margins are assumed by default and you "unlock" them by setting it as "Flexible". So for example, setting only the FlexibleWidth and FlexibleHeight bits is equivalent to enabling all autoresizing options in IB. By throwing in any of the margin masks (i.e.UIViewAutoresizingFlexibleLeftMargin
) you are "deselecting" the corresponding margin autoresizing option in IB. (I've seen many other posts floating around where this seemed to be a major point of confusion for some folks.)During my research, however, I did notice that there does not seem to be any sort of event, message, notification, or otherwise when a UIView gets resized, whether automatically or not. While not necessary for resizing subViews, it would be nice to know when it's happening in the situation where you would like to cause a scrollView or tableView to scroll should it's frame size ever change. I know one can easily do this when the keyboard pops up, because there is a notification system around that as well as the
TextField
delegate
methods. So maybe a topic for another post...Anyway, thank you everyone who participates on StackOverflow!