我正在构建一个复杂的项目,除其他外,我需要将UIPageViewController设置为主视图的子视图。我正在使用autolayout,并使用约束来对主视图上的各种元素进行排序。
问题是,当我尝试运行应用程序时,由于NSAutoresizingMaskLayoutConstraints冲突而崩溃。
2013-10-28 16:22:18.419 Red Event App[1658:60b] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x145c1990 V:|-(20)-[UINavigationBar:0x145bf6b0] (Names: '|':UIView:0x145bf620 )>",
"<NSLayoutConstraint:0x145bf510 V:[UINavigationBar:0x145bf6b0]-(0)-[UIView:0x145bef70]>",
"<NSLayoutConstraint:0x145d0550 UIView:0x145a8c10.top == UIView:0x145bf620.top>",
"<NSAutoresizingMaskLayoutConstraint:0x145b3a40 h=-&- v=-&- UIView:0x145a8c10.midY == UIView:0x145bef70.midY + 56.5>",
"<NSAutoresizingMaskLayoutConstraint:0x145b3a70 h=-&- v=-&- UIView:0x145a8c10.height == UIView:0x145bef70.height + 113>"
)
通常的解决方法是将TranslatesAutoresizingMaskIntoConstraints设置为no。
但是,当我这样做时,UIPageViewController的子视图开始忽略PageViewController的边界,并以(0,0)的原点结束(据我所知)。
我试图通过在设置数据源(_itemViewControllers)时手动设置框架来修复位置:
- (void)setupLeafs{
_itemViewControllers = [[NSMutableArray alloc]init];
for(NSString *pagename in _datasource){
RWNode *page = [_xml getPage:pagename];
UIViewController *viewController = [RWNavigationController getViewControllerFromDictionary:[page getDictionaryFromNode]];
viewController.view.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,self.view.frame.size.width, self.view.frame.size.height);
[_itemViewControllers addObject:viewController];
}
}
并在获取页面时
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
UIViewController *nextViewController = [self getPreviousLeaf:viewController];
nextViewController.view.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,self.view.frame.size.width, self.view.frame.size.height);
return nextViewController;
}
但都没有任何影响。
我需要对我正在做的事情进行约束,因此坚持使用Masks不是一种选择。我认为我正在寻找的是一种在UIPageViewController子项添加后对其进行约束的方法(通过任何进程调用) viewControllerBeforeViewController
)。但我真的很想知道这个问题可以解决的方法。
编辑: 我找到了解决这个问题的黑客。我不太确定我在这里列出的是整个解决方案,但是经过一个多月的修改代码之后,我现在才注意到这一点。
首先,在设置了pageviewcontroller的视图控制器中,在我初始化了pageviewcontroller之后,我有以下两行
UIView *pageView = self.pageViewController.view;
pageView.frame = self.view.frame;
不是我设定的 [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
在这个视图控制器中。
其次,在子控制器上,我检查视图是在页面视图控制器的内部还是外部使用。只有视图是 不 在pageviewcontroller中使用是 [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
设置在子视图上。
现在,这对我来说是有用的(对我来说)。但我真的想要一个不那么黑客的解决方案。