How to support both portrait and landscape orientations using xib files?

I am interested in having a UIViewController that supports all orientations and purely uses two xib files to configure its subzones.

One possibility is to use the NSBundle method loadNibNamed:to load a new hierarchy of views in each rotation, but this seems inefficient and can have unpleasant side effects. For example, after rebooting from the nib file, all my views will lose their previous state, for example, adding text to a text field, etc.

Is there a better way to do this?

+3
source share
2 answers

. xib , (.. xib).

:

// This is for an iPad root-level view controller.
- (void)setupForOrientation:(UIInterfaceOrientation)orientation {
  if (UIInterfaceOrientationIsPortrait(orientation)) {
    CGRect bounds = CGRectMake(0, 0, 768, 1004);
    bkgImageView.frame = bounds;
    // Other view positioning for portrait.
  } else {
    CGRect bounds = CGRectMake(0, 0, 1024, 748);
    bkgImageView.frame = bounds;
    // Other view positioning for landscape.
  }
  [self drawBackgroundForOrientation:orientation];
}

shouldAutorotateToInterfaceOrientation: .

+3

, shouldAutorotateToInterfaceOrientation, "" "", , willRotateToInterfaceOrientation, , - , .

+1

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


All Articles