简体   繁体   English

iOS 部分视图控制器在方向改变时变黑

[英]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在视图 controller 中有 tableview(我认为没有必要。可以是任何视图)
  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.我能够重现 10 次中的 7 次。

FYI:供参考:

My Viewcontroller has only tableview that's all and all cells has autolayout constraints.我的 Viewcontroller 只有表视图,所有单元格都有自动布局约束。 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.基本上,只有当您将设备倾斜 position 并推送或弹出视图 controller 时,您才能重现此问题,并且当页面尝试加载时,您必须更改方向然后问题发生。

(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()在您的 viewWillTransition 方法中调用 layoutIfNeeded()

Try to reset the layout of TableView after rotation by using this method,尝试使用此方法重置 TableView 旋转后的布局,

    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.我的观点 controller 只有一个表格视图,当我们倾斜时,我的意思是在导航推送或弹出期间改变方向,这曾经发生过。

Try setting the frame in viewDidLayoutSubviews尝试在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或尝试在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);如果设置框架self.tableView.frame = CGRectMake(0, 0, width, height); doesn't work then you could try [self.tableView layoutIfNeeded]不起作用,那么您可以尝试[self.tableView layoutIfNeeded]

Set frame again in再次设置框架

viewDidLayoutSubviews 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.在一个视图控制器(推送新视图控制器的父级)中,在viewDidLayoutSubviews设置框架解决了另一个视图控制器中的问题(子视图 controller,一个正在被推送的)在viewWillTransition设置框架解决了问题:) 但我仍然不知道为什么这个问题没有通用的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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