首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AutoLayout约束不影响childViewController

AutoLayout约束不影响childViewController
EN

Stack Overflow用户
提问于 2015-05-21 11:13:15
回答 2查看 51关注 0票数 0

在我的viewController应用程序中,当我将一个childViewController添加到另一个iOS应用程序时,autolayout约束并不影响视图。

有任何方法可以获得ChildViewController约束并将它们添加到supperView中吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-21 12:43:58

我找到了一个解决办法:

代码语言:javascript
复制
view.addSubview(svc.view)

let vd: [NSObject:AnyObject] = ["svc": svc.view]
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-0-[svc]-0-|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: vd))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[svc]-0-|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: vd))

svc.didMoveToParentViewController(self)

来自:https://gist.github.com/radianttap/4455176

票数 0
EN

Stack Overflow用户

发布于 2015-05-21 11:44:40

我相信您需要像这样手动添加约束到您的childViewController;

代码语言:javascript
复制
UIViewController *newVC = [UIViewController new];
[self addChildViewController:newVC];
[self.view addSubview:newVC.view];

newVC.view.translatesAutoresizingMaskIntoConstraints = NO;

NSLayoutConstraint *bottomContstraint =[NSLayoutConstraint
                                        constraintWithItem:newVC.view
                                        attribute:NSLayoutAttributeBottom
                                        relatedBy:NSLayoutRelationEqual
                                        toItem:self.view
                                        attribute:NSLayoutAttributeBottom
                                        multiplier:1.0
                                        constant:0];

NSLayoutConstraint *topConstraint =[NSLayoutConstraint
                                    constraintWithItem:newVC.view
                                    attribute:NSLayoutAttributeTop
                                    relatedBy:NSLayoutRelationEqual
                                    toItem:self.view
                                    attribute:NSLayoutAttributeTop
                                    multiplier:1.0
                                    constant:0];

NSLayoutConstraint *leftConstraint =[NSLayoutConstraint
                                     constraintWithItem:newVC.view
                                     attribute:NSLayoutAttributeLeft
                                     relatedBy:NSLayoutRelationEqual
                                     toItem:self.view
                                     attribute:NSLayoutAttributeLeft
                                     multiplier:1.0
                                     constant:0];

NSLayoutConstraint *rightConstraint =[NSLayoutConstraint
                                      constraintWithItem:newVC.view
                                      attribute:NSLayoutAttributeRight
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      attribute:NSLayoutAttributeRight
                                      multiplier:1.0
                                      constant:0];

[self.view addConstraint:bottomContstraint];
[self.view addConstraint:topConstraint];
[self.view addConstraint:leftConstraint];
[self.view addConstraint:rightConstraint];

我自己也用过这个,它看起来很管用,但是一个更有经验的开发人员可能会给你一些不同的东西,但我希望这会有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30372071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档