Make the subview fit inside the container and resize correctly

I am trying to load dynamic tips in the form of container views. I almost got it to work, except that there is an offset in the subtitles that I can't seem to get rid of (cf the pink look in the pictures below).

enter image description here

From debug view hierarchy:

enter image description here

As you can see in the 2nd picture, the container frame is correctly positioned, while the subtitle for some reason does not work.

I do not know what happens with autorun.

Here is the code related to loading nib and assigning it as a subview:

enter image description here

The commented code is all that I tried to make it work, without success. I thought that autostart would work on its own if I didn't do anything, but by default it loads the thread without changing its size.

This means that the lead and top anchors are correct, however the thread then uses the full size ... (cf figure below)

enter image description here

So the question is, what do I need to do to load the tip and make it suitable for viewing the container?

+5
source share
1 answer

You should add constraints to your NibView, not set the borders and frame of the NibView.

Try calling the following function (addFullScreenConstraint) in NibView after adding NibView as a subview of the content view:

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])) } } 
+4
source

Source: https://habr.com/ru/post/1269666/


All Articles