iphone - 横向模式下 View 的位置和大小错误

标签 iphone objective-c cocoa-touch ipad uikit

我正在为 iPad 开发“仅横向模式”应用程序。 问题是我的 View 在屏幕上的位置错误,而且我找不到原因...

我已经在 Info.plist 中设置了必要的属性:

    <key>UIInterfaceOrientation</key>
    <string>UIInterfaceOrientationLandscapeRight</string>

    <key>UISupportedInterfaceOrientations</key>
    <array>
       <string>UIInterfaceOrientationLandscapeLeft</string>
       <string>UIInterfaceOrientationLandscapeRight</string>

我有这样的层次结构:

UIWindow --> MainMenuViewController.view --> MyTableviewController.view

在 AppDelegate 的 application:didFinishLaunchingWithOptions: 方法中,我有以下代码: CGRect 矩形 = [[UIScreen 主屏幕] 边界]; [窗口设置边界:矩形];

mainMenuController = [[MainMenuViewController alloc] init];

[window addSubview:mainMenuController.view];
[window makeKeyAndVisible];

return YES;

在 MyMenuController 的 viewDidload: 中,我有:

[super viewDidLoad];
[[UIApplication sharedApplication] setStatusBarHidden:YES];

SelectPictureTableViewController *selectPictureViewController = [[SelectPictureTableViewController alloc] initWithStyle:UITableViewStylePlain];
[selectPictureViewController.view setBounds:CGRectMake(0.0, 128.0, 1024.0, 640.0)];
[self.view addSubview:selectPictureViewController.view]; 

在 SelectPictureTableViewController 的 initWithStyle: 中我写道:

if ((self = [super initWithStyle:style])) {
    templatesArray = [[NSMutableArray alloc] init];
    tumbnailsInRow = 3;

    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.allowsSelection = NO;
}
return self;

在两个 ViewController 中我都有这个实现:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortrait && 
 interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

对我来说,它看起来应该显示 1024x640 表格 View 。 但它显示高度约为 350px 的 tableview,并移动到屏幕中间(距顶部约 250px)。 您知道为什么它可以如此转变吗? 也许我必须在某个地方设置界限却忘了这样做? 被这个问题困扰了几个小时。

PS:没有 MainMenuViewController,tableview 的位置很好(当我将 tableview 添加为 UIWindow 的 subview 时)。但我需要将其他 UI 元素放置到屏幕上,并且我需要这个 MainMenuViewController...

干杯!

最佳答案

我找到了答案:

我已将其添加到我的 MainMenuViewController 的方法 viewDidLoad 中:

    UIViewAutoresizing mask = (1 & !UIViewAutoresizingFlexibleTopMargin);
    selectPictureViewController.view.autoresizingMask = mask;

问题是默认的 autoresizingMask。

关于iphone - 横向模式下 View 的位置和大小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3286298/

相关文章:

ios - Swift:以编程方式附加自定义 UIView

ios - ld : SDK. 使用旧版本的 Swift 语言 (XX) 编译的框架比以前的 ObjC 框架文件(未知 ABI 版本 YYY)

objective-c - 使用 Storyboard 通过滑动更改 View

objective-c - NSScrollView滚动不滚动?

objective-c - FetchedResultsController 看不到数据导入后 managedObjectContext 的变化

objective-c - 简单的字符串模式匹配

ios - Xcode - 如何修复 'NSUnknownKeyException' ,原因 : … this class is not key value coding-compliant for the key X"error?

iphone - 将存储在 IBAction 中的值传递给下一个 Nib UITextField。

ios - iOS 11 中的 NSFoundationVersionNumber - 如何以编程方式检测 iOS 版本

iphone - UITableViewCell 显示 UIImageView 或 UIProgressView