Skip to content

Support sizing image views with aspect ratio preservation #135

@NickEntin

Description

@NickEntin

It's very common to size image views preserving their image's aspect ratio. We currently support this with the AspectRatio utility, but its call semantics are a bit awkward for this use case. In a simplified example, we end up with something like this:

imageView.bounds.size = imageView.image?.size.aspectRatio.size(toFit: bounds, in: self) ?? .zero

We should add methods calculate the sizeThatFits and to sizeToFit an image view. Additionally these should specify whether upscaling is allowed, since in many cases (e.g. when using a raster image) you want to cap the image at its native size.

func sizeThatFitsPreservingAspectRatio(_ sizeToFit: CGSize, allowingUpscaling: Bool) -> CGSize
func sizeToFitPreservingAspectRatio(_ sizeToFit: CGSize, allowingUpscaling: Bool)

Metadata

Metadata

Assignees

Labels

enhancementRequest for a new feature or improvement to an existing feature

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions