UINavigationController 中的 UITableView 旋转后位于导航栏下方

发布于 2024-08-13 18:08:29 字数 495 浏览 6 评论 0原文

这是肖像:

“替代文本”

这是风景

“替代文本”

我已经尝试过旋转但没有成功:

self.tableView.autoresizesSubviews = YES;
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

This is Portrait:

alt text

This is Landscape

alt text

I've tried this on rotation with no success:

self.tableView.autoresizesSubviews = YES;
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

十二 2024-08-20 18:08:29

我最近遇到了这个问题。就我而言,这是因为我正在执行导航控制器的旋转,该导航控制器不是应用程序窗口的根控制器。因此,我使用旋转通知侦听器/仿射变换/边界调整方法来更改导航控制器的布局。这工作正常,但产生了此处描述的结果。我花了一段时间才注意到,当我旋转到横向时,导航栏的高度没有正确调整大小(即,从 44 像素到 32 像素,框架在横向模式下降低了高度)。然而,我的应用程序的根视图控制器正在响应willRotateToInterfaceOrientationdidRotateFromInterfaceOrientation。该视图控制器也已经知道关联的导航控制器。我通过将以下代码添加到 willRotateToInterfaceOrientation 方法来解决这个问题:

CGRect frame = self.navViewController.navigationBar.frame;
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    frame.size.height = 44;
} else {
    frame.size.height = 32;
}
self.navViewController.navigationBar.frame = frame;

希望这可以为遇到类似情况的人节省一些时间。

I ran into this problem recently. In my case, it was because I was performing rotations of a navigation controller that was not the root controller of the application window. I was therefore using the rotate notification listener/affine transform/bounds adjustment approach to changing the layout of the navigation controller. This worked all right but produced the results described here. It took me a while to notice that when I rotated to landscape the nav bar's height was not being correctly resized (i.e., from 44 to 32 pixels, the framework's reduced height for landscape mode). The root view controller of my app was responding to willRotateToInterfaceOrientation and didRotateFromInterfaceOrientation, however. That view controller also already knew about the associated navigation controller. I remedied the problem by adding the following code to the willRotateToInterfaceOrientation method:

CGRect frame = self.navViewController.navigationBar.frame;
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    frame.size.height = 44;
} else {
    frame.size.height = 32;
}
self.navViewController.navigationBar.frame = frame;

Hope this saves somebody some time in a similar situation.

层林尽染 2024-08-20 18:08:29

当使用自定义视图控制器作为根(它管理其他视图控制器本身)时,它负责将所有旋转事件转发到当前活动的子控制器(如果我可以这样称呼它)。这些事件是:

– willRotateToInterfaceOrientation:duration:

– willAnimateRotationToInterfaceOrientation:duration:

– didRotateFromInterfaceOrientation:

– willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:

– didAnimateFirstHalfOfRotationToInterfaceOrientation:

– willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:

因此,对于由自定义根控制器管理的导航控制器,将 – willAnimateRotationToInterfaceOrientation:duration: 转发到导航控制器可以修复旋转时导航栏高度错误的问题。

When using a custom view controller as root, which manages other view controllers itself, then it is responsible to forward all rotation events to the currently active sub-controller (if I may call it like that). These events are:

– willRotateToInterfaceOrientation:duration:

– willAnimateRotationToInterfaceOrientation:duration:

– didRotateFromInterfaceOrientation:

– willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:

– didAnimateFirstHalfOfRotationToInterfaceOrientation:

– willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:

So in the case of a navigation controller, managed by your custom root controller, forwarding – willAnimateRotationToInterfaceOrientation:duration: to the navigation controller fixes the issue with the wrong navigation bar height when rotating.

怎言笑 2024-08-20 18:08:29

在 didRotateFromInterfaceOrientation 方法中添加以下行。

[self.navigationController.viewlayoutSubviews];

我希望这能行。

Add the following line in the didRotateFromInterfaceOrientation method.

[self.navigationController.view layoutSubviews];

I hope this will do.

给我一枪 2024-08-20 18:08:29

我有完全相同的问题,@jstevenco 让我找到了一个非常奇怪的解决方案。
我的视图也会在旋转时自动调整大小,但 tableview 似乎不明白那里有一个导航控制器:

在 tableviewcontroller 中,添加以下内容:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
 CGRect frame = navBar.frame;
if (fromInterfaceOrientation == UIInterfaceOrientationPortrait || fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    frame.size.height = 31.9;
} else {
    frame.size.height = 44.1;
}
navBar.frame = frame;  

}

31.9 和 44.1 非常重要!剩下的就是 32 和 44,没有任何调整大小的事情要做,所以它似乎忽略了它。为我工作,希望对其他人有帮助。

I have exactly the same issue, and @jstevenco led me to a really strange solution.
My views also auto resize on rotation, but the tableview doesn't seem to understand there is a navigationcontroller sitting there:

Within the tableviewcontroller, add the following:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
 CGRect frame = navBar.frame;
if (fromInterfaceOrientation == UIInterfaceOrientationPortrait || fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    frame.size.height = 31.9;
} else {
    frame.size.height = 44.1;
}
navBar.frame = frame;  

}

The 31.9 and 44.1 are very important strangly! Leaving these are 32 and 44, there is no resizing to do, so it seems to ignore it. Worked for me, hope it helps someone else.

葬心 2024-08-20 18:08:29

我看起来表格视图在方向改变后向下滚动了一点。

您是否尝试过致电 scrollToRowAtIndexPath:atScrollPosition:animated: 并滚动到最上面一行?

I looks like the table view is scrolled down a little after the orientation change.

Have you tried calling scrollToRowAtIndexPath:atScrollPosition:animated: and scrolling to the top-most row?

好听的两个字的网名 2024-08-20 18:08:29

如果您在导航控制器中调用了“willAnimateRotationToInterfaceOrientation:”,但忘记从方法内调用 super,则可能会出现此问题。

你应该有这样的东西:

        - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation];
// Your code here

    }

This problem can occur if you've called "willAnimateRotationToInterfaceOrientation:" in your navigation controller, but forgotten to call super from within the method.

You should have something like this:

        - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation];
// Your code here

    }
淡淡的优雅 2024-08-20 18:08:29

在这些情况下需要注意的一件事是状态栏的方向(如果您手动更改状态栏)。如果状态栏未隐藏并且您在状态栏方向错误时更改了视图的框架(在您的代码中,也许您在框架更改后更新了状态栏的方向),它将自动错误地调整导航栏的大小,即使您没有打开自动调整大小。

One thing to look out for in these situations is the orientation of the Status Bar if you are changing it manually. If the status bar is not hidden and you change the frame of your view while the status bar is in the wrong orientation (in your code, maybe you're updating the orientation of the status bar just after the frame change), it will auto resize the navigation bar wrongly, even if you haven't got auto resize on.

半岛未凉 2024-08-20 18:08:29

当设备方向发生变化时,您的 UINavigationController 需要收到通知。如果您仅将 UINavigationControllerview 添加到另一个 UIView,则不会发生这种情况。

从 iOS 5 开始,您只需在父 UIViewController 上调用 addChildViewController: 即可,这将确保方向更改传播到此子视图控制器。

Your UINavigationController needs to get notified when the device orientation changes. This doesn't happen if you only add the view of a UINavigationController to another UIView.

Starting with iOS 5, you can simply call addChildViewController: on the parent UIViewController, which will ensure that orientation changes are propagated to this child view controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文