Skip to content

Instantly share code, notes, and snippets.

@eldaroid
Last active August 10, 2024 22:40
Show Gist options
  • Save eldaroid/7d88be42781e6d0106986c9055004454 to your computer and use it in GitHub Desktop.
Save eldaroid/7d88be42781e6d0106986c9055004454 to your computer and use it in GitHub Desktop.
Инструмент для отладки UI в SwiftUI, позволяя более детально контролировать и понимать процесс вычисления размеров и размещения представлений
struct LogSizes: Layout {
var label: String
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
assert(subviews.count == 1)
print(String(format: "предлагают _%@_: →%.2f x ↓%.2f", label, proposal.width ?? 0, proposal.height ?? 0))
let result = subviews[0].sizeThatFits(proposal)
print(String(format: "ответ от _%@_: →%.2f x ↓%.2f", label, result.width, result.height))
return result
}
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
subviews[0].place(at: bounds.origin, proposal: proposal)
}
}
extension View {
func logSizes(_ label: String) -> some View {
LogSizes(label: label) { self }
}
}
@eldaroid
Copy link
Author

Пример использования:

Image("image")
     .resizable()
     .logSizes("resize")
     .aspectRatio(contentMode: .fill)
     .logSizes("aspectratio")
    .frame(width: 250, height: 250)
    .logSizes("frame")
    .border(Color.black)

Ответ:

предлагают _frame_: 393.00 x 759.00
ответ от _frame_: 250.00 x 250.00
предлагают _aspectratio_: 250.00 x 250.00
ответ от _aspectratio_: 482.51 x 250.00

P.S.: моя картинка размеров 393.00 x 759.00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment