iOS part of viewcontroller goes black when orientation changes

偶尔善良 提交于 2020-01-24 16:40:54

问题


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 :)


回答1:


In your viewWillTransition method call layoutIfNeeded()




回答2:


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()
    }



回答3:


Set frame again in

viewDidLayoutSubviews




回答4:


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]




回答5:


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.



来源:https://stackoverflow.com/questions/58745651/ios-part-of-viewcontroller-goes-black-when-orientation-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!