![](/img/trans.png)
[英]Orientation changes when pop back to the initial viewcontroller using UINavigationController iOS 6
[英]iOS part of viewcontroller goes black when orientation changes
當我在特定方向加載時更改視圖控制器方向時,屏幕的一部分在底部變為空白。
重現步驟:
視圖控制器的底部變黑。
我能夠重現 10 次中的 7 次。
供參考:
我的 Viewcontroller 只有表視圖,所有單元格都有自動布局約束。 試圖了解它發生的原因,我想修復它。
有關重現問題的更多詳細信息:
基本上,只有當您將設備傾斜 position 並推送或彈出視圖 controller 時,您才能重現此問題,並且當頁面嘗試加載時,您必須更改方向然后問題發生。
(雙關語……大聲笑)換句話說,您必須像在方向盤一樣傾斜設備:)。
僅當您將設備用作方向盤時才會發生此問題:)
在您的 viewWillTransition 方法中調用 layoutIfNeeded()
嘗試使用此方法重置 TableView 旋轉后的布局,
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
tableView.layoutIfNeeded()
}
或者
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.layoutIfNeeded()
}
我也遇到了同樣的問題。
我的觀點 controller 只有一個表格視圖,當我們傾斜時,我的意思是在導航推送或彈出期間改變方向,這曾經發生過。
嘗試在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);
}
}
或嘗試在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);
}
}
供參考:
如果設置框架self.tableView.frame = CGRectMake(0, 0, width, height);
不起作用,那么您可以嘗試[self.tableView layoutIfNeeded]
再次設置框架
viewDidLayoutSubviews
在一個視圖控制器(推送新視圖控制器的父級)中,在viewDidLayoutSubviews設置框架解決了另一個視圖控制器中的問題(子視圖 controller,一個正在被推送的)在viewWillTransition設置框架解決了問題:) 但我仍然不知道為什么這個問題沒有通用的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.