Skip to content

Add wrapContent() methods - #141

Merged
lucdion merged 5 commits into
masterfrom
wrap_content
Jun 6, 2018
Merged

lucdion merged 5 commits into
masterfrom
wrap_content

Conversation

@lucdion

@lucdion lucdion commented Jun 5, 2018 •

Copy link
Copy Markdown
Member

Add wrapContent() methods that adjust view's width & height to wrap all its subviews

The following methods are useful to adjust view's width and/or height to wrap all its subviews. These methods also adjust subviews position to create a tight wrap.

Methods:

  • wrapContent()
    wrapContent(padding: CGFloat)
    wrapContent(padding: UIEdgeInsets)
    Adjust the view's width and height to wrap all its subviews. The method also adjusts subviews position to create a tight wrap. It is also possible to specify an optional padding around all subviews.
  • wrapContent(:WrapType)
    wrapContent(:WrapType, padding: CGFloat)
    wrapContent(:WrapType, padding: UIEdgeInsets)
    Adjust the view's width AND/OR height to wrap all its subviews. WrapType values are .horizontally/.vertically/.all It is also possible to specify an optional padding around all subviews.
Usage examples:
	view.pin.wrapContent().center()   // wrap all subviews and centered the view inside its parent.
	view.pin.wrapContent(padding: 20) // wrap all subviews with a padding of 20 pixels all around
	view.pin.wrapContent(.horizontally)

if frame.maxY > maxY {
maxY = frame.maxY
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using CGRect.union() to find the bounding rect instead of computing each rect component individually? All this loop and the local min/max variables could probably be replaced by the statement let boundingRect = subviews.reduce(CGRect.zero, { $0.union($1.frame) }). The min/max values are all available on a CGRect directly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea, I'll do that 👍

@antoinelamy antoinelamy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appart from the Android inspired name, 👍 😉

@lucdion
lucdion merged commit e5b0ae4 into master Jun 6, 2018
@lucdion
lucdion deleted the wrap_content branch June 6, 2018 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants