简体   繁体   English

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

[英]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.

相关问题 触摸其父视图框架之外的子视图 - Touches on a subview outside the frame of its superview 捕获超视图框架外的子视图(多层) - Capturing touches on a subview outside the frame of its superview (over multiple layers) 使用 hitTest:withEvent 捕获对其超视图框架外的子视图的触摸: - Capturing touches on a subview outside the frame of its superview using hitTest:withEvent: 通过Superview的框架更改调整子视图的大小 - Resizing subview with superview's frame change 如何添加具有约束的子视图以使子视图具有与其父视图相同的框架? - How to add a subview with constraints to make the subview with the same frame as its superview? 如何以编程方式设置子视图的框架以使其适合其父视图? - How to programmatically set a subview's frame to fit it inside its superview? 子视图显示在两个具有相同框架和父视图的视图的不同位置 - A subview is displayed in different places on two views with the same frame and superview 我想要一个子视图的框架 w.r.t 到 window 和同一个子视图的超级视图被转换(缩放,旋转) - I want frame of a subview w.r.t to window and superview of the same subview is transformed ( scaled, rotated) Objective C:如何在当前可见帧中设置子视图显示? (UITableView) - Objective C: How to set the subview display in the current visible frame? (UITableView) 子视图在父视图中接收其框架外的触摸 - Subview receiving touches outside its frame in parent view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM