简体   繁体   中英

iOS part of viewcontroller goes black when orientation changes

When I change viewcontroller orientation while it's loading on particular orientation, part of the screen goes blank at the bottom.

Steps to reproduce:

  1. Have tableview(Not necessary I think. could be any view) in a view controller
  2. Push a new viewcontroller which also has a tableview(Not necessary I think. could be any view) during an action
  3. Rotate the screen from one orientation to Another(Portrait to landscape or landscape to portrait).now you will be able to see the dark part.
  4. Press back button to pop the current viewcontroller
  5. Now rotate from one orientation to Another(Portrait to landscape or landscape to portrait).now you will be able to see the dark part here as well.

The bottom part of viewcontroller goes black.

I am able to reproduce 7 out of 10 times.

FYI:

My Viewcontroller has only tableview that's all and all cells has autolayout constraints. Trying to understand why it happened and I would like to fix it.

More details on reproducing the issue:

Basically you can reproduce this issue only if you hold your device in slanting position and either push or pop a view controller and while the page is trying to load, you have to change the orientation then the issue happens.

(Pun intended.... lol ) In other words,you have to tilt you device as if you are steering the wheel:).

This issue can only happen if you use your device like a steering wheel:)

In your viewWillTransition method call layoutIfNeeded()

Try to reset the layout of TableView after rotation by using this method,

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        tableView.layoutIfNeeded()
    }

or

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tableView.layoutIfNeeded()
    }

I also experienced the same issue.

My view controller had just one tableview and when we tilt, I mean change the orientation during a navigational push or pop this used to happen.

Try setting the frame in viewDidLayoutSubviews

   -(void)viewDidLayoutSubviews{
    CGRect screen = [[UIScreen mainScreen] bounds];
    CGFloat width = CGRectGetWidth(screen);
    CGFloat height = CGRectGetHeight(screen);
    CGRect frame = self.tableView.frame;
    // To check if there's any mismatch between the sizes of screen and tableview then set the tableview frame same as screen frame.
    if (CGRectGetHeight(frame) != height && CGRectGetWidth(frame) != width) {
        self.tableView.frame = CGRectMake(0, 0, width, height);
    }
}

or try setting the frame in viewWillTransitionToSize

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    CGRect screen = [[UIScreen mainScreen] bounds];
    CGFloat width = CGRectGetWidth(screen);
    CGFloat height = CGRectGetHeight(screen);
    CGRect frame = self.tableView.frame;
    if (CGRectGetHeight(frame) != height && CGRectGetWidth(frame) != width) {
        self.tableView.frame = CGRectMake(0, 0, width, height);
    }
}

FYI:

if setting the frame self.tableView.frame = CGRectMake(0, 0, width, height); doesn't work then you could try [self.tableView layoutIfNeeded]

Set frame again in

viewDidLayoutSubviews

In one view controller(Parent the one that pushes new view controller) setting the frame at viewDidLayoutSubviews solves the issue in another view controller(The child view controller, one that's getting pushed) setting the frame at viewWillTransition fixes the problem:) But still I don't know why there's no universal solution to this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM