问题: 我有两个视图控制器加载到根视图控制器。两个子视图布局都响应方向更改。我使用[UIView transformationFromView:...]在两个视图之间切换。两个子视图都可以自行工作,但是如果......
以前隐藏的视图存在严重的布局问题。我重复这些步骤越多,问题就越严重。
实施细则
我有三个viewsControllers。
A& B ViewControllers各有一个背景图像,分别有一个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];
}
我正在摸索着这一点,我怀疑我所做的很多事情都是错误的,所以请随意指出应该做的事情,我需要输入。
但我主要关心的是了解导致布局问题的原因。这里有两张图片说明了布局问题的性质......
更新: 我刚刚注意到,当方向是横向时,过渡会垂直翻转,而我希望它是水平的。我不知道这是什么可能出错的线索。
切换到另一个视图...更改方向....切换回....
答案 0 :(得分:1)
我偶然发现了这一点并且有一种感觉,你已经想到了这一点。但是为了以防万一,或者如果其他人正在寻找相同的答案,这就是我在这种情况下采取的方法。
在每个视图控制器的头文件(你切换的文件,而不是根MyAppViewController)中,添加一个浮点数:
float boundWidth;
在viewDidLoad中,将其初始化为0
boundWidth = 0;
然后创建一个如下所示的检查:
- (void)viewDidAppear:(BOOL)animated {
// check to see what orientation the device is in;
if ((boundWidth != 0) && (boundWidth != self.view.bounds.size.width)) {
// the orientation has changed, so insert code to deal with this;
}
离开视图时设置宽度来跟踪宽度:
- (void)viewDidDisappear:(BOOL)animated {
boundWidth = self.view.bounds.size.width;
}