簡體   English   中英

UIPageViewController +自動布局旋轉問題

[英]UIPageViewController + Auto Layout rotation issue

我有一個UIPageViewController,里面有一個簡單的UIViewController。

如果UIViewController沒有子視圖,則其視圖在旋轉時可以正確調整大小。 (純綠色背景)。

肖像景觀

如果UIViewController具有帶固定幀的子視圖,則其視圖在旋轉時可以正確調整大小。 (與角落的黃色正方形的堅實綠色背景)。

肖像景觀

如果UIViewController有一個子視圖,其自動布局約束設置為填充其超級視圖,則其視圖在旋轉時不再正確調整大小。 (純黃色背景,UIPageViewController的紅色背景可見)。 我使用的自動布局代碼是:

UIView *v = [[UIView alloc] init];
[v setTranslatesAutoresizingMaskIntoConstraints:NO];
[v setBackgroundColor:[UIColor yellowColor]];
[[self view] addSubview:v];

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(v);

NSArray *cs = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views:viewsDictionary];
[[self view] addConstraints:cs];

cs = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v]|" options:0 metrics:nil views:viewsDictionary];
[[self view] addConstraints:cs];

肖像景觀

為什么子視圖的自動布局會影響其超級視圖? 只有當它包含在UIPageViewController中時才會發生這種情況。

我也有這個問題,以前的答案似乎沒有多大幫助。 我需要為與PageViewController一起使用的scrollview添加約束。

// Add Paging View Controller
self.addChildViewController(self.pageViewController)
self.pageViewController.didMoveToParentViewController(self)
self.containerView.addSubview(self.pageViewController.view)

// Add Constraints
var verticalConstraints:Array = NSLayoutConstraint.constraintsWithVisualFormat("V:|[View]|", options: nil, metrics: nil, views: ["View":self.pageViewController.view])
var horizontalConstraints:Array = NSLayoutConstraint.constraintsWithVisualFormat("|[View]|", options: nil, metrics: nil, views: ["View":self.pageViewController.view])

self.pageViewController.view.setTranslatesAutoresizingMaskIntoConstraints(false)
self.containerView.addConstraints(verticalConstraints)
self.containerView.addConstraints(horizontalConstraints)

// Add Constraints for the Scrollview
//This line is a bit of a hack.  If Apple ever changes the structure of the UIPageViewController, this could break.
let scrollView = self.pageViewController.view.subviews.first! as UIView  
scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[View]|", options: nil, metrics: nil, views: ["View":scrollView])
horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("|[View]|", options: nil, metrics: nil, views: ["View":scrollView])
self.pageViewController.view.addConstraints(verticalConstraints)
self.pageViewController.view.addConstraints(horizontalConstraints)

頁面視圖控制器的視圖不是表示頁面的視圖的超級視圖。 頁面視圖控制器的視圖具有由自定義滾動視圖子類組成的視圖層次結構,該滾動視圖又具有三個通用視圖子類,每個子類用於索引0到2中包含的視圖控制器的視圖。這些視圖用作超視圖你包含的觀點。 您必須將自動布局約束定位到這些視圖。 但是使用自動調整遮罩並讓它們轉換為自動布局約束要容易得多。

我想到了:

我曾調用[[_pageViewController view] setTranslatesAutoresizingMaskIntoConstraints:NO]; 在容器控制器類中,並沒有在其視圖(或視圖,如Bored Astronaut提到的視圖)上顯式設置自動布局約束。

@Bored宇航員的回答幾乎對我有用。 除了將自動調整掩碼轉換為自動布局約束之外,我唯一做的就是設置UIPageViewController的視圖幀,然后再將其添加到父視圖中。

[self addChildViewController:pageViewController];
pageViewController.view.frame = self.view.frame;
[self.view addSubview:pageViewController.view];
[pageViewController didMoveToParentViewController:self];

在不設置框架的情況下,子視圖控制器將以任何方向正確顯示和定位,但其子視圖將無法正確布局。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM