旋转 iPhone 并更改视图

发布于 2024-10-14 09:01:02 字数 668 浏览 0 评论 0原文

我从来没有玩过 iPhone 横屏的情况,但我有一个完全运行的 iPhone 应用程序,它全部假设用户直立查看 iPhone。我想尝试旋转手机,为了做到这一点,我做了一些非常简单的事情:

我将以下代码添加到我的视图控制器中:

-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
    NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);

    if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
        tableView.frame = CGRectMake(0, 0, 480, 320);
    else
        tableView.frame = CGRectMake(0,73,320,390);


}

并且我收到了 BAD_ACCESS 错误。我根本没有看到视图负载。那么,问题出在哪里,如何正确实现 willRotate 方法,以便当手机旋转时,我可以调整所有组件的大小?

I've never played around with the iPhone being landscape, but I have a fully working iPhone app that is all assuming the user is viewing the iPhone upright. I'd like to play around with rotating the phone, and in order to do that, I did something very simple:

I added the following code to my View Controller:

-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
    NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);

    if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
        tableView.frame = CGRectMake(0, 0, 480, 320);
    else
        tableView.frame = CGRectMake(0,73,320,390);


}

and I'm getting a BAD_ACCESS error. I don't see the view load at all. So, what's the problem, and how do I properly implement the willRotate method so that when the phone rotates, I can resize all my components?

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

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

发布评论

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

评论(2

情绪少女 2024-10-21 09:01:02

崩溃是由这一行引起的:

NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);

orientation 是一个 enum 值,因此格式说明符应该是 %i 而不是 %@< /代码>。

支持旋转的最简单方法是实现:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

对于您希望支持的方向返回 YES。不要在此方法中进行任何大小调整。使用视图支柱和弹簧确保它们能够正确调整大小并重新定位以进行交替旋转。

The crash is caused by this line:

NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);

orientation is an enum value, therefore the format specifier should be %i not %@.

The simplest way to support rotating is to implement:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

Return YES for the orientations you wish to support. Do not doing any resizing in this method. Use the views struts and springs to ensure that they resize and reposition themselves correctly for the alternate rotations.

我很坚强 2024-10-21 09:01:02

这是您的日志记录代码的问题:

NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);

日志记录字符串中的格式说明符 %@NSString 指针的占位符。当应用程序到达此点时,它会查看方向变量,尝试取消引用它以获取 NSString 实例,然后崩溃,因为它根本不是指针,而是枚举值。

It's a problem with your logging code:

NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);

The formatting specifier %@ in your logging string is a placeholder for an NSString pointer. When the app reaches this point, it looks at the orientation variable, attempts to dereference it to get to an NSString instance, and crashes, because it's not a pointer at all, it's an enum value.

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