LinkedInAttic / LayoutKit

LayoutKit is a fast view layout library for iOS, macOS, and tvOS.
http://layoutkit.org
Apache License 2.0
3.16k stars 267 forks source link

A non-nil viewReuseId does not force BaseLayout to have a view #70

Closed nicksnyder closed 8 years ago

nicksnyder commented 8 years ago

fixes #56

nicksnyder commented 8 years ago

Certain layouts don't require a view (e.g. StackLayout, InsetLayout) because they merely position other layouts. The exception to this is if the developer provides a config block to a StackLayout or an InsetLayout. This config block may set properties that require a view (e.g. background color), so the layout will always produce a view.

Prior to this change, setting a viewReuseId would also force a layout to produce a view. So:

StackLayout(axis: .vertical, sublayouts: [LabelLayout(text: "Hi")]) // produces one UILabel
StackLayout(axis: .vertical, viewReuseId: "stack", sublayouts: [LabelLayout(text: "Hi")]) // produces UILabel nested in UIView

After this change, these would both produce only a single UILabel. There is really no point in setting a viewReuseId on a StackLayout if it doesn't have a doesn't need a UIView so there are a few options:

  1. Create a UIView anyway (this is the current behavior)
  2. Don't create a UIView (this is the PR)
  3. Assert/log that this doesn't really make any sense.

At least one person was surprised by (1) because they didn't realize that they didn't need to set a viewReuseId on things like StackLayout. (2) Allows developers to assign viewReuseIds without having to worry about performance or whether the layout currently needs a view. This is a bit kinder than (3) because it allows a developer to assign a viewReuseId before it may be needed.