iphone - 旋转 iPhone 并改变 View

标签 iphone ios uiview uiviewcontroller rotation

我从来没有玩过横向 iPhone,但我有一个功能完备的 iPhone 应用程序,它假设用户正在直立查看 iPhone。我想玩转手机,为此,我做了一些非常简单的事情:

我将以下代码添加到我的 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);


}

我收到 BAD_ACCESS 错误。我根本看不到 View 负载。那么,问题是什么?我该如何正确实现 willRotate 方法,以便在手机旋转时调整所有组件的大小?

最佳答案

崩溃是由这一行引起的:

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

orientation 是一个 enum 值,因此格式说明符应该是 %i 而不是 %@

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

为您希望支持的方向返回 YES。不要在此方法中进行任何调整大小。使用 View struts 和 springs 来确保它们为交替旋转正确调整大小和重新定位。

关于iphone - 旋转 iPhone 并改变 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4796121/

相关文章:

php - jQuery Ajax 表单不在 iPhone 上提交

android - 首页英雄背景图像在移动设备上缩放不当

ios - Swift - 在 KeyWindow 的 subview 中无法识别点击手势

ios - 如何滚动另一个 UICollectionView 下方的 UICollectionView?

ios - 设置框架未正确调整 UIView 的大小

iphone - 在 MKMapView 中使用 UIActivityIndi​​cator

iphone - 在哪里调用方法以重新加载应用程序从后台返回时的 View

iphone - 移除标签栏后, Storyboard 仍显示 'ghost' 标签栏项目

iphone - 获取 UIView 相对于其父 View 的父 View 的位置

swift - UIView 调整大小以适应其中的标签