UIButton Layout #54
Comments
There is currently no layout specialized for buttons. Doesn't sound difficult to do, we would just need to figure out how button padding works. I imagine the implementation of ButonLayout could just reuse a InsetLayout and a LabelLayout. |
Quick question But would it make sense to have a layout for self-sizing view such as label, button, switch something like this import UIKit
/// measurement references
fileprivate var references = [String: UIView]()
/// create or get an existing reference of type V
func getReference<V: UIView>() -> V {
let key = String(describing: V.self)
if let reference = references[key] as? V { return reference }
let reference = V.self()
references[key] = reference
return reference
}
class SelfSizingViewLayout<V: UIView>: Layout {
/// block that configure View size
let configureSize: (V) -> Void
init(configureSize: @escaping (V) -> Void) {
self.configureSize = configureSize
}
open func measurement(within maxSize: CGSize) -> LayoutMeasurement {
let reference: V = getReference()
configureSize(reference)
let fittedSize = reference.sizeThatFits(maxSize)
return LayoutMeasurement(layout: self, size: fittedSize.decreasedToSize(maxSize), maxSize: maxSize, sublayouts: [])
}
/// rest of protocol ...
}
let size1 = CGSize(width: 100, height: 100)
SelfSizingViewLayout<UIButton>(
configureSize: { $0.setTitle("Bim Foo Bar", for: .normal) }
).measurement(within: size1) // {w 98 h 34}
SelfSizingViewLayout<UILabel>(
configureSize: { $0.text = "Bim Foo Bar" }
).measurement(within: size1) // {w 92.5 h 20.5} |
I think you can copy ComponentKit's button layout implementation here. |
Thks @esam091 for the link. |
The implementation of From the UIView documentation
Instantiating a UIView on a background thread may be fine, but in order to get a proper measurement we would need to set data on the UIView (e.g. button title). Per the above documentation, this type of manipulation should not be done from a background thread (even if it does seem to work now because it may break in the future). |
Make sense to me now, thks for the explanations |
This is in preparation for ButtonLayout (#54) which will also want to use this type.
This is in preparation for ButtonLayout (#54) which will also want to use this type.
This is in preparation for ButtonLayout (#54) which will also want to use this type.
This is in preparation for ButtonLayout (#54) which will also want to use this type.
This is in preparation for ButtonLayout (#54) which will also want to use this type.
This is in preparation for ButtonLayout (#54) which will also want to use this type.
This is in preparation for ButtonLayout (#54) which will also want to use this type.
This is in preparation for ButtonLayout (#54) which will also want to use this type.
Hey there
Is there something equivalent to LabelLayout for views such as UIButton (or any other view that define an intrinsicContentSize)?
Right now I am using a SizeLayout but I am forced to define the size of my button
The text was updated successfully, but these errors were encountered: