Hidden orientation problems with UIView / Layout orientation

Problem: I have two view controllers loaded into the root view controller. Both layouts are displayed according to orientation changes. I switch between the two views using [UIView transformFromView: ...]. Both sub-choices work fine on their own, but if ...

  • Views replaced
  • Orientation changes
  • Views replaced again

A View that was previously hidden has serious layout issues. The more I repeat these steps, the worse the problem.

Implementation Details

I have three viewControllers.

  • Myappppcontroller
  • A_ViewController
  • B_ViewController

A B ViewControllers , UIWebView AQGridView . , , loadView A_ViewController...

- (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];

}

AQGridView B_ViewController .

. , 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];

}

, , , , , , , , -, .

- , . , ...

UPDATE: , , , , . , , .

Normal layout

... .... ....

Problem Layout

+3
1

, . , - , , .

(, , MyAppViewController), float:

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;
}
+1

Source: https://habr.com/ru/post/1785083/


All Articles