在我的viewController应用程序中,当我将一个childViewController添加到另一个iOS应用程序时,autolayout约束并不影响视图。
有任何方法可以获得ChildViewController约束并将它们添加到supperView中吗?
发布于 2015-05-21 12:43:58
我找到了一个解决办法:
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)发布于 2015-05-21 11:44:40
我相信您需要像这样手动添加约束到您的childViewController;
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];我自己也用过这个,它看起来很管用,但是一个更有经验的开发人员可能会给你一些不同的东西,但我希望这会有所帮助!
https://stackoverflow.com/questions/30372071
复制相似问题