设备方向更改时,我在旋转子视图时遇到问题。我的情况是:
我有在
中声明的根视图控制器//in AppDelegate.h
@property MainViewController * myMainViewController;
//in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.myMainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
CGRect initFrame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
self.window = [[UIWindow alloc] initWithFrame:initFrame];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
self.window.rootViewController = self.myMainViewController;
return YES;
}
在根视图控制器中,我以编程方式添加子视图;初始设备方向为纵向,我的子视图的大小设置为帧属性值(0,0,300,600)。
//in MainViewController.h
@property ContentViewController * mySubViewController;
//in MainViewController.m
-(void)viewDidAppear:(BOOL)animated
{
self.mySubViewController = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
[self.mySubViewController.view setFrame:CGRectMake(0, 0, 300, 600)];
[self.view addSubview:self.mySubViewController.view];
}
当设备方向发生变化时,我想根据新的设备方向调整我的子视图的大小。如果这是横向,那么我想将我的子视图大小设置为(0,0,600,300)。
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(fromInterfaceOrientation==UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation==UIInterfaceOrientationLandscapeRight)
{
self.mySubViewController.view.frame = CGRectMake(0, 0, 300, 600);
}
else
{
self.mySubViewController.view.frame = CGRectMake(0, 0, 600, 300);
}
}
但是,结果不是我所期望的。我的子视图的大小是一个大小约为(0,0,300,300)的矩形。有什么问题?
答案 0 :(得分:-1)
再次查询您的界面方向,而不是预测基于fromOrientation
的当前方向,如下所示,
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation))
{
//Your portrait
}
else
{
//Your Landscape.
}
}