【发布时间】:2011-01-12 22:57:19
【问题描述】:
问题: 我有两个视图控制器加载到根视图控制器中。两种子视图布局都响应方向变化。我使用 [UIView transformationFromView:...] 在两个视图之间切换。两个子视图都可以自己正常工作,但是如果...
- 视图已交换
- 方向变化
- 视图再次交换
之前隐藏的视图存在严重的布局问题。我重复这些步骤越多,问题就越严重。
实现细节
我有三个视图控制器。
- MyAppViewController
- A_ViewController
- B_ViewController
A & B ViewController 各有一个背景图片,以及一个 UIWebView 和一个 AQGridView。为了给你一个我如何设置它的例子,这里是 A_ViewController 的 loadView 方法......
- (void)loadView {
[super loadView];
// background image
// Should fill the screen and resize on orientation changes
UIImageView *bg = [[UIImageView alloc] initWithFrame:self.view.bounds];
bg.contentMode = UIViewContentModeCenter;
bg.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
bg.image = [UIImage imageNamed:@"fuzzyhalo.png"];
[self.view addSubview:bg];
// frame for webView
// Should have a margin of 34 on all sides and resize on orientation changes
CGRect webFrame = self.view.bounds;
webFrame.origin.x = 34;
webFrame.origin.y = 34;
webFrame.size.width = webFrame.size.width - 68;
webFrame.size.height = webFrame.size.height - 68;
projectView = [[UIWebView alloc] initWithFrame:webFrame];
projectView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:projectView];
}
为简洁起见,B_ViewController 中的 AQGridView 的设置方式几乎相同。
现在这两种视图都可以单独运行。但是,我像这样在 AppViewController 中使用它们...
- (void)loadView {
[super loadView];
self.view.autoresizesSubviews = YES;
[self setWantsFullScreenLayout:YES];
webView = [[WebProjectViewController alloc] init];
[self.view addSubview:webView.view];
mainMenu = [[GridViewController alloc] init];
[self.view addSubview:mainMenu.view];
activeView = mainMenu;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchViews:) name:SWAPVIEWS object:nil];
}
我使用我自己的 switchView 方法在两个视图之间切换,就像这样
- (void) switchViews:(NSNotification*)aNotification;
{
NSString *type = [aNotification object];
if ([type isEqualToString:MAINMENU]){
[UIView transitionFromView:activeView.view toView:mainMenu.view duration:0.75 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
activeView = mainMenu;
}
if ([type isEqualToString:WEBVIEW]) {
[UIView transitionFromView:activeView.view toView:webView.view duration:0.75 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
activeView = webView;
}
// These don't seem to do anything
//[mainMenu.view setNeedsLayout];
//[webView.view setNeedsLayout];
}
我正在摸索,我怀疑我所做的很多事情都没有正确实施,所以请随时指出应该以不同方式做的任何事情,我需要输入。
但我主要关心的是了解导致布局问题的原因。这里有两张图片说明了布局问题的本质......
更新: 我刚刚注意到,当方向是横向时,过渡会垂直翻转,而我希望它是水平的。我不知道这是否是可能出现问题的线索。
切换到另一个视图...改变方向....切换回来....
【问题讨论】:
标签: cocoa-touch ios uiviewcontroller orientation