我正在向视图控制器添加子视图,其尺寸为1024(宽)x 768(高)。我的应用程序设置为仅支持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打印出两个视图都具有纵向方向尺寸,甚至是黄色视图,实际上看起来都是横向方向。
有什么想法吗?
修改以获取更多详细信息:
如果子视图的宽度只有一个像素,那么视图就会像它应该的那样横向(请参见下面的薄黄色条带)。
根据我的观察,只有完全全屏导致此子视图旋转。较小和较大的框架保留了笔尖中定义的尺寸。
答案 0 :(得分: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}]];
}