iphone - 未调用 UIView layoutSubviews

标签 iphone objective-c ios

使用 Xcode 4.2/iOS5.0

我有一个 viewController 和一个关联的 viewController.xib。 viewController 被设置为 .xib 文件的所有者。它是从 UINavigationController 调用的。

当设备纵向/横向旋转时,我正在尝试一些手动布局

据我了解,要覆盖的 UIView 方法是

- (void)layoutSubviews;

这是你做布局的地方

但是 - 与 drawRect 一样 - 你不能直接调用它,它会在适当的时候由 iOS 调用

相反,您调用 setNeedsLayout...

所以我在 ViewController 中有这个

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)
         fromInterfaceOrientation {
   [[self view] setNeedsLayout];
 }

在同一个 ViewController 中我有这个

- (void)layoutSubviews {
    //.manual override of automatic layout on rotation
NSLog(@"layoutSubviews");
 }

但是 layoutSubviews 没有被调用,我不明白为什么。

这是一个线索... 如果我直接调用 layoutSubviews 作为 [self layoutSubviews]

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)
         fromInterfaceOrientation {
   [self layoutSubviews];
 }

它确实触发了

但是如果我将它称为[[self view] layoutSubviews] 它不会

看来 View 及其 Controller 没有正确连接?

最佳答案

你的问题是你在你的 View Controller 上实现了 -layoutSubviews,而不是在你的 View 上。永远不会这样调用它。 -layoutSubviews 是一种机制,通过它自定义 View 可以布置自己的 subview (例如,一个 UIButton 有一个用于图像的 UIImageView 和一个UILabel 用于标签,并使用 -layoutSubviews 确保 imageview 和标签都正确定位)。它不是 View Controller 可以用来控制其 View 布局的机制。

如果您想在旋转时更改布局,那么您应该继续在 -didRotateFromInterfaceOrientation: 中设置新布局。

关于iphone - 未调用 UIView layoutSubviews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8192072/

相关文章:

javascript - iPhone OS 3.0 中使用 webkit-transform translate() 的慢速动画

iphone - iOS:UIImageView 示例代码

iphone - iPhone 4 每秒可以发送多少条 Objective-C 消息?

objective-c - 如何停止 enumerateObjectsUsingBlock Swift

ios - MPRemoteCommandCenter 暂停/播放按钮不切换?

iphone - iOS 在另一个 uiview 中加载 uiview

iphone - 更改 Tab Bar 应用程序中 UITableViewController 的样式

iphone - 替换 mkannotationview 中的 calloutaccessoryview

ios - 使用 SpriteKit 和内置物理引擎移动和跳跃 Sprite

ios - 使用 UIImage RenderingMode AlwaysTemplate 在 iPhone 6 上显示不同的颜色