[英]Subview visible but frame outside superview frame
I have a parent VC that loads a child VC inside it. 我有一个父VC,它在其中加载了一个子VC。 Code in parent VC is as follows:
父VC中的代码如下:
self.draggerViewController = [[DraggerViewController alloc] initWithSuperView:self.view];
And the code of the child VC is as follows: 子VC的代码如下:
- (instancetype)initWithSuperView:(UIView*)superView {
self = [super init];
if (self) {
self.superView = superView;
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[superView addSubview:self.view];
[superView addConstraint:[NSLayoutConstraint constraintWithItem:superView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];
[superView addConstraint:[NSLayoutConstraint constraintWithItem:superView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
self.constraintDraggerAttributeBottom = [NSLayoutConstraint constraintWithItem:superView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
[superView addConstraint:self.constraintDraggerAttributeBottom];
}
return self;
}
With this code, the subview is visible on its parent view and constraints are applied correctly so that it is placed at the bottom of the parent view with leading and trailing being 0. BUT, the real frame of the view is outside the parent view. 使用此代码,子视图在其父视图上可见,并且正确应用了约束,因此子视图放置在父视图的底部,且前导和尾随均为0。但是,该视图的实际框架位于父视图之外。
That is, I can see the subview at the bottom of the parent view but the frame of this subview is frame = (0 -315; 768 35). 也就是说,我可以在父视图的底部看到该子视图,但是该子视图的框架为frame =(0 -315; 768 35)。
If I set 'clipToBounds' to 'YES', the view is placed on the real frame but now constraints are not applied correctly. 如果将'clipToBounds'设置为'YES',则视图将放置在真实框架上,但现在约束没有正确应用。
How can I place this child VC inside the parent VC's view at the position I want using constraints? 如何将该子VC放置在父VC的视图中要使用约束的位置?
Thank you! 谢谢!
OK, got it... 好的,我知道了...
I was forgetting the height constraint of the view... What a shame... 我忘记了视图的高度限制...真可惜...
[superView addConstraint:[NSLayoutConstraint constraintWithItem:superView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];
Thank you all! 谢谢你们!
尝试这个 :
superView.insertSubview(self.view, at: self. superView.subviews.count)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.