旋转 iPhone 并更改视图
我从来没有玩过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
崩溃是由这一行引起的:
orientation
是一个enum
值,因此格式说明符应该是%i
而不是%@< /代码>。
支持旋转的最简单方法是实现:
对于您希望支持的方向返回
YES
。不要在此方法中进行任何大小调整。使用视图支柱和弹簧确保它们能够正确调整大小并重新定位以进行交替旋转。The crash is caused by this line:
orientation
is anenum
value, therefore the format specifier should be%i
not%@
.The simplest way to support rotating is to implement:
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.这是您的日志记录代码的问题:
日志记录字符串中的格式说明符
%@
是NSString
指针的占位符。当应用程序到达此点时,它会查看方向变量,尝试取消引用它以获取 NSString 实例,然后崩溃,因为它根本不是指针,而是枚举值。It's a problem with your logging code:
The formatting specifier
%@
in your logging string is a placeholder for anNSString
pointer. When the app reaches this point, it looks at the orientation variable, attempts to dereference it to get to anNSString
instance, and crashes, because it's not a pointer at all, it's an enum value.