Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance imporvement in UIView fillToSuperview() #540

Merged
merged 1 commit into from Aug 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -29,6 +29,8 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S
- `removeFirst(where:)` array extension now is more generic `RangeReplaceableCollection` extensions. [#516](https://github.com/SwifterSwift/SwifterSwift/pull/516) by [LucianoPAlmeida](https://github.com/LucianoPAlmeida).
- **RandomAccessCollection**:
- `indices(of:)` array extension now is more generic `RandomAccessCollection` extensions. [#516](https://github.com/SwifterSwift/SwifterSwift/pull/516) by [LucianoPAlmeida](https://github.com/LucianoPAlmeida).
- **UIView**:
- `fillToSuperview()` should be more performant.
Copy link
Member

Choose a reason for hiding this comment

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

Ah, post merge regret. We should clean up this CHANGELOG entry in another PR 😕 My bad guys

Copy link
Member

Choose a reason for hiding this comment

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

Ooops, since I also approve my bad too :((
But it's simple I can fix it right :))


### Fixed
- **UIImage**:
Expand Down
9 changes: 5 additions & 4 deletions Sources/Extensions/UIKit/UIViewExtensions.swift
Expand Up @@ -457,10 +457,11 @@ public extension UIView {
// https://videos.letsbuildthatapp.com/
translatesAutoresizingMaskIntoConstraints = false
if let superview = superview {
leftAnchor.constraint(equalTo: superview.leftAnchor).isActive = true
rightAnchor.constraint(equalTo: superview.rightAnchor).isActive = true
topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
let left = leftAnchor.constraint(equalTo: superview.leftAnchor)
Copy link
Member

Choose a reason for hiding this comment

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

Hey @viktart :))
Can you explain how this implementation is more performant that the previous?
I believe the code is more clear this way, but can't see the performance gain here

Copy link
Member

Choose a reason for hiding this comment

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

The performance gain is because you're not supposed to activate constraints individually. It's better to activate them all at once using the NSLayoutConstraint.activate API

let right = rightAnchor.constraint(equalTo: superview.rightAnchor)
let top = topAnchor.constraint(equalTo: superview.topAnchor)
let bottom = bottomAnchor.constraint(equalTo: superview.bottomAnchor)
NSLayoutConstraint.activate([left, right, top, bottom])
}
}

Expand Down