微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 将UIPageViewController上的“将AutoAresizeMaskIntoConstraints设置为”否“

我正在构建一个复杂的项目,其中除了别的以外,我需要设置一个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.size.height);
    return nextViewController;
}

但也没有任何效果.

我需要限制我在做什么,所以坚持使用Masks不是一个选择.我想我正在寻找的是一种在UIPageViewController子项添加约束之后(通过任何进程调用viewControllerBeforeViewController)的方法.但我真的很想听到有关这个问题可以解决的任何方式.

编辑:
我发现了一个黑客来解决这个问题.我不太清楚我在这里列出的是整个解决方案,但是现在我注意到,经过一个多月修补代码.

首先,在设置pageviewcontroller的视图控制器中,我已经初始化了pageviewcontroller之后,我有以下两行

UIView *pageView = self.pageViewController.view; 
pageView.frame = self.view.frame;

不是我设置了[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];在这个视图控制器.

其次,在子控制器上,我检查视图是否在一个pageviewcontroller内部或外部使用.只有在viewviewcontroller中没有使用视图的时候,[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];设置在子视图上.

现在,这个工作(对我来说)在这一刻.但我真的希望一个不那么麻烦的解决方案.

解决方法

使用AutoLayout时,不应直接设置视图的框架.限制用于为您这样做.通常,如果要在视图中设置自己的约束,则覆盖UIViews的updateConstraints方法.确保页面控制器的内容视图允许其边缘调整大小,因为它们的大小适合页面视图的框架.您的约束和视图设置将需要解决此问题,否则您将收到不满意的约束错误.

原文地址:https://www.jb51.cc/iOS/329645.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐