【问题标题】:Hidden UIView Orientation Change / Layout problems隐藏的 UIView 方向更改/布局问题
【发布时间】:2011-01-12 22:57:19
【问题描述】:

问题: 我有两个视图控制器加载到根视图控制器中。两种子视图布局都响应方向变化。我使用 [UIView transformationFromView:...] 在两个视图之间切换。两个子视图都可以自己正常工作,但是如果...

  1. 视图已交换
  2. 方向变化
  3. 视图再次交换

之前隐藏的视图存在严重的布局问题。我重复这些步骤越多,问题就越严重。

实现细节

我有三个视图控制器。

  1. MyAppViewController
  2. A_ViewController
  3. 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


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-26
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 2020-08-27
      • 1970-01-01
      相关资源
      最近更新 更多