0

我正在向视图控制器添加一个子视图,其尺寸为 1024(W) x 768(H)。我的应用程序设置为仅支持 plist 中的横向,但我的子视图将始终以纵向显示,其底部从屏幕上消失。

我的监督:

#import "TSTViewController.h"
#import "TSTContentViewController.h"

@interface TSTViewController ()
@property (nonatomic, strong) TSTContentViewController *tcvc;
@end

@implementation TSTViewController

- (void)loadView
{
    UIView *rootView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f)];
    [rootView setBackgroundColor:[UIColor yellowColor]];
    self.view = rootView;
    _tcvc = [[TSTContentViewController alloc] initWithNibName:@"TSTContentViewController" bundle:nil];
    [rootView addSubview:_tcvc.view];
}

- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"H:%f W:%f; H:%f W:%f", self.view.frame.size.height, self.view.frame.size.width, _tcvc.view.frame.size.height, _tcvc.view.frame.size.width);
}

@end

我的应用委托:

#import "TSTAppDelegate.h"

#import "TSTViewController.h"

@implementation TSTAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[TSTViewController alloc] init];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

@end

我的子视图 (TSTContentViewController) 没有自定义代码,并且笔尖尺寸为 (1024W X 768H),背景为灰色。

结果似乎无条件地看起来像:

(黄色超视图,灰色子视图) 在此处输入图像描述

viewDidAppear 中的 NSLog 打印出两个视图都有纵向尺寸,即使是黄色的,实际上看起来就像是横向的。

有任何想法吗?

编辑以获取更多详细信息:

如果子视图的宽度只小了一个像素,视图就会像它应该的那样变成横向的(见下面的黄色细条)。

在此处输入图像描述

根据我的观察,只有完全全屏才会导致这种子视图旋转。越来越小的框架保留了笔尖中定义的尺寸。

4

1 回答 1

0

如果您使用自动布局,那么您只需要为您的子视图添加几个约束:

- (void)loadView
{
    UIView *rootView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f)];
    [rootView setBackgroundColor:[UIColor yellowColor]];
    self.view = rootView;
    TSTContentViewController* _tcvc = [[TSTContentViewController alloc] initWithNibName:@"TSTContentViewController" bundle:nil];
    [self.view addSubview:_tcvc.view];
    [_tcvc.view setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[view]|" options:0 metrics:nil views:@{@"view":_tcvc.view}]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:@{@"view":_tcvc.view}]];
}
于 2013-03-19T19:24:45.813 回答