使子视图适合容器内部并正确调整大小

时间:2021-02-23 16:47:27

I'm trying to load dynamic nibs as subviews of containers. I almost got it to work, except that the subviews have an offset I can't seem to get rid off (cf pink view in pictures below).

我正在尝试将动态nib加载为容器的子视图。我几乎得到它的工作,除了子视图有一个我似乎无法摆脱的偏移(参见下面图片中的粉红色视图)。

使子视图适合容器内部并正确调整大小

From the View Hierarchy debugging:

从View Hierarchy调试:

使子视图适合容器内部并正确调整大小

As you can see in 2nd picture, the container frame is correctly positioned, whereas the subview isn't, for some reason.

正如您在第2张图片中看到的那样,容器框架正确定位,而子视图由于某种原因不正确定位。

I don't really know what is going with autolayout.

我真的不知道autolayout会发生什么。

Here's the code that deals with loading the nib and assigning it as subview:

这是处理加载笔尖并将其指定为子视图的代码:

使子视图适合容器内部并正确调整大小

The commented-out code is all the things I've tried to make it work, with no success. I thought autolayout would work on its own without me having to do anything, but by default it loads the nib without resizing it.

注释掉的代码是我试图让它工作的所有东西,没有成功。我认为autolayout可以独立工作而不需要做任何事情,但默认情况下它会加载nib而不需要调整它。

That means the leading and top anchors are correct, however the nib then uses its full size... (cf picture below)

这意味着前导和顶部锚是正确的,但是笔尖然后使用它的全尺寸......(参见下图)

使子视图适合容器内部并正确调整大小

So the question is, what is needed for me to do in order to load the nib and make it fit to the container view ?

所以问题是,为了加载笔尖并使其适合容器视图,我需要做些什么?

1 个解决方案

#1


4  

You should add constraints to your NibView instead of setting the bounds and the frame of the NibView.

您应该为NibView添加约束,而不是设置NibView的边界和框架。

Try to call the following function (addFullScreenConstraint) on the NibView after adding the NibView as a subview of the content view:

在将NibView添加为内容视图的子视图后,尝试在NibView上调用以下函数(addFullScreenConstraint):

  extension UIView {

    /// Adds constraints to this `UIView` instances `superview` object
    /// to make sure this always has the same size as the superview.
    /// Please note that this has no effect if its `superview` is `nil`
    /// – add this `UIView` instance as a subview before calling this.
    func addFullScreenConstraints() {
        guard let superview = self.superview else {
            return
        }

        self.translatesAutoresizingMaskIntoConstraints = false
        superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[subview]-0-|",
                                                                options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self]))
        superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[subview]-0-|",
                                                                options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self]))
    }
}

#1


4  

You should add constraints to your NibView instead of setting the bounds and the frame of the NibView.

您应该为NibView添加约束,而不是设置NibView的边界和框架。

Try to call the following function (addFullScreenConstraint) on the NibView after adding the NibView as a subview of the content view:

在将NibView添加为内容视图的子视图后,尝试在NibView上调用以下函数(addFullScreenConstraint):

  extension UIView {

    /// Adds constraints to this `UIView` instances `superview` object
    /// to make sure this always has the same size as the superview.
    /// Please note that this has no effect if its `superview` is `nil`
    /// – add this `UIView` instance as a subview before calling this.
    func addFullScreenConstraints() {
        guard let superview = self.superview else {
            return
        }

        self.translatesAutoresizingMaskIntoConstraints = false
        superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[subview]-0-|",
                                                                options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self]))
        superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[subview]-0-|",
                                                                options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self]))
    }
}