繁体   English   中英

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

[英]Subview visible but frame outside superview frame

我有一个父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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM