子视图可见,但框架在超级视图框架之外



我有一个父 VC,在其中加载了一个子 VC。父 VC 中的代码如下所示:

self.draggerViewController = [[DraggerViewController alloc] initWithSuperView:self.view];

子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;
}

使用此代码,子视图在其父视图上可见,并且正确应用约束,以便将其放置在父视图的底部,前导和尾随为 0。但是,视图的实际框架在父视图之外。

也就是说,我可以在父视图的底部看到子视图,但此子视图的框架是 frame = (0 -315; 768 35)。

如果我将"clipToBounds"设置为"YES",则视图将放置在实际帧上,但现在未正确应用约束。

如何使用约束将此子 VC 放置在父 VC 视图中我想要的位置?

谢谢!

好的,知道了...

我忘记了视图的高度限制...真可惜...

[superView addConstraint:[NSLayoutConstraint constraintWithItem:superView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];

谢谢大家!

试试这个:

superView.insertSubview(self.view, at: self. superView.subviews.count)

最新更新