简体   繁体   中英

Subview that clips to parent view in Swift

I'm trying to add a subview that would always have the same size as its parent, so that it would always resize if the parent size changes.

I was thinking about adding (top, bottom, leading, trailing) constraints from the code.

When I add the subview, this code is executed:

subview.frame = parent.frame
subview.translatesAutoresizingMaskIntoConstraints = false
subview.leadingAnchor.constraint(equalTo: parent.leadingAnchor).isActive = true
subview.trailingAnchor.constraint(equalTo: parent.trailingAnchor).isActive = true
subview.topAnchor.constraint(equalTo: parent.topAnchor).isActive = true
subview.bottomAnchor.constraint(equalTo: parent.bottomAnchor).isActive = true

But the subview size stays at 0, and is displayed at the top left of the screen. I don't understand why it's size is zero since I set it's frame, and also because it has all the constraints to set its size.

Inspector view:

Note: The parent is on the left, the subview ( LoaderView ) is on the right

父视图 子视图

Your parent view seems to be a scroll view, and it is missing the constraints that tell it what its scrollable content size is (note the warning in your first screenshot). Since nothing is defining the content size, the scrollable area size is therefore just zero.

I'm not sure what constraints you need exactly as that depends on the details of your situation, but in order to at least see your subview you could add (for example) equal-width and equal-height constraints to your subview and its parent view (the scroll view):

NSLayoutConstraint.activate([
    subview.widthAnchor.constraint(equalTo: parent.widthAnchor),
    subview.heightAnchor.constraint(equalTo: parent.heightAnchor)
])

This defines the scrollable area as being equal in width and height to the scroll view's frame. However, if you do this then there won't really be any kind of scrolling other than the bouncing behavior when you try to scroll beyond the scrollable area (if that's turned on for this scroll view).

You could instead change the height anchor to be equal to some constant, just to allow for some actual scrolling:

NSLayoutConstraint.activate([
    subview.widthAnchor.constraint(equalTo: parent.widthAnchor),
    subview.heightAnchor.constraint(equalToConstant: 2000) // just some arbitrary amount for demonstration purposes
])

This makes the scrollable height equal to 2000 points, regardless of the height of the scroll view's frame.

Or, if you want the scrollable area to be some multiple of the scroll view's frame height, you could set a multiplier on the height anchor:

NSLayoutConstraint.activate([
    subview.widthAnchor.constraint(equalTo: parent.widthAnchor),
    subview.heightAnchor.constraint(equalTo: parent.heightAnchor, multiplier: 3)
])

This makes the scrollable height equal to three times the height of the scroll view's frame.

And if you want a horizontally-scrolling scroll view, just change the widthAnchor instead of the heightAnchor like I've done above. Or if you want a 2D-scrollable area that lets you pan around both vertically and horizontally, then change both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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