添加到窗口后自动调整 UIView 的大小
注意:这可能与子视图在添加到时不自动调整大小重复根视图控制器
我有一个 iPad 应用程序,可以在其主窗口中的不同视图之间切换。视图切换代码如下所示:
- (void)switchToViewController:(UIViewController*)viewController {
if (currentViewController != viewController) {
[currentViewController.view removeFromSuperview];
currentViewController = viewController;
[window addSubview:viewController.view];
}
}
问题是,当新视图(UISplitView)以横向显示时,它的大小无法填充整个窗口。右侧有一大片空白的黑色空间。看起来视图只有 768 像素宽,而不是横向窗口的 1024 像素宽。
如果我将设备旋转到纵向然后再返回到横向,视图会自动调整大小。
如果设备处于纵向,则一切正常。如果 UISplitView 是我显示的第一个视图,它的大小也会正确调整。仅当我在横向显示另一个视图后切换到它时,才会出现此问题。
那么,有什么方法可以强制 iPhone OS 在将视图添加到窗口后调整视图大小吗?
我尝试调用 sizeToFit
和 setNeedsLayout
。我还尝试将视图的边界设置为窗口的边界,并且尝试设置框架以匹配前一个视图的框架。
Note: This may be a duplicate of Subview Doesnt AutoSize When Added to Root View Controller
I have an iPad app that switches between different views in its main window. The view-switching code looks like this:
- (void)switchToViewController:(UIViewController*)viewController {
if (currentViewController != viewController) {
[currentViewController.view removeFromSuperview];
currentViewController = viewController;
[window addSubview:viewController.view];
}
}
The problem is that when the new view (a UISplitView) appears in landscape orientation, it is not sized to fill the entire window. There is a large empty black space on the right. It looks like the view is only 768 pixels wide, rather than the 1024-pixel width of the landscape window.
If I rotate the device to portrait and then back to landscape, the view sizes itself properly.
If the device is in portrait orientation, everything works fine. The UISplitView also gets sized properly if it is the first view I show. The problem only occurs if I switch to it after another view has been shown, in landscape.
So, is there some way to force iPhone OS to resize the view after it has been added to the window?
I've tried calling sizeToFit
, and setNeedsLayout
. I've also tried setting the view's bounds
to the window's bounds
, and I've tried setting the frame
to match the previous view's frame.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这绝对是可能的! :-)
你可以在这里查看我的存储库:
https://github.com/hfossli/AGWindowView
它会自动处理任何旋转和帧更改,这样你就赢了不必担心这个。
如果您想担心这一点,那么您可以剪切并粘贴最重要的部分
# 1 将视图添加到窗口
# 2 添加侦听器并更新视图
请记住删除通知侦听
# 3 做数学题
祝你好运!
This is absolutely possible! :-)
You can check out my repo here:
https://github.com/hfossli/AGWindowView
It will automatically deal with any rotation and framechanges so you won't have to worry about that.
If you like to worry about that then you can just cut and paste the most important parts
# 1 Add view to window
# 2 Add listener and update view
Remember to remove notification listening
# 3 Do the math
Good luck!
这可行,但看起来有点老套:
This works, but it seems a little hacky:
除了您的视图之外,窗口可能还包含其他 UI 元素。您的示例中的 20 像素差异是状态栏的高度。
窗口和屏幕都不旋转。仅当您切换了高度和宽度时,获取它们的框架并将它们用于旋转视图才有效。
如果您使用的是 UIViewController,请尝试从此方法返回 YES:
The window may include other UI elements besides your view. The 20 pixel difference in your example is the height of the status bar.
Neither the window nor screen rotate. Getting their frames and using them for a rotated view will only work if you have switched the height and width.
If you are using a UIViewController, try returning YES from this method:
我遇到了同样的问题,但我用这行代码修复了它:
}
您必须添加新视图,然后删除旧视图和新视图,然后添加新视图。我不知道为什么,但这确实有效。
I got the same problem, but i fixed it with this lines of code:
}
You must add the new view, then remove the old and the new and then add the new view. I don't know why, but that works.
Fossli 的答案对于 iPad 来说是正确的。但是,我有一个需要支持的通用应用程序。因此,一些调整是必要的。
将以下内容添加到 AppDelegate.h
将以下内容添加到 AppDelegate.m
将以下内容添加到 Utility.h
将以下内容添加到 Utility.m
Fossli's answer is correct for iPad. However, I have a universal app that I needed to support. Therefore some adjustments are necessary.
Add the following to AppDelegate.h
Add the following to AppDelegate.m
Add the following to Utility.h
Add the following to Utility.m
iOS7 的 Windows 与 iOS8/9 的 Windows 具有不同的行为。
iOS7 的键盘窗口和 iOS8/9 的所有窗口始终具有正确的方向和方向。尺寸。因此您可以观察尺寸变化事件并更新视图的框架。
但iOS7的其他窗口始终保持纵向方向和尺寸。旋转后您需要更新视图的变换。
您需要观察 UIApplicationWillChangeStatusBarOrientationNotification 并更新 UIView 的大小,如下所示:
Windows of iOS7 have different behaviors with windows of iOS8/9.
Keyboard window of iOS7 and all windows of iOS8/9 always have correct orientation & size. So you can observer the size change events and update the frame of your view.
But other windows of iOS7 always keep the portrait orientation and size. You need update transform of your view after rotation.
You need to observer UIApplicationWillChangeStatusBarOrientationNotification and update size of your UIView like this: