UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Which Modifier to use?

Forums > SwiftUI

I a bit confused with which modifiler to use as there seam to be two. eg

.scaledToFit()
.aspectRatio(contentMode: .fit)

.scaledToFill()
.aspectRatio(contentMode: .fill)

also with

.accessibilityLabel(Text)
.accessibility(label: Text)

.accessibilityHint(Text)
.accessibility(hint: Text)

as most of the accessibility have both

Does anyone know why and which one to use as I do not want to use one then find that it depreciated!

2      

From the docs, re: scaledToFit: "This method is equivalent to calling aspectRatio(_:contentMode:) with a nil aspectRatio and a content mode of fit."

And scaleToFill: "This method is equivalent to calling aspectRatio(_:contentMode:) with a nil aspectRatio and a content mode of fill."

Docs:

[scaledToFit()](https://developer.apple.com/documentation/swiftui/view/scaledtofit())

[scaledToFill()](https://developer.apple.com/documentation/swiftui/view/scaledtofill())

(Sorry about the links; if you click on one of these, you'll need to add the () manually in order to get the correct page.)

So the scale modifiers are just shortcuts for the default aspectRatio modifiers.

accessibility(label:) and accessibility(hint:) are deprecated in favor of accessibilityLabel(_:) and accessibilityHint(_:).

Docs:

accessibility(label:)

accessibility(hint:)

2      

Thanks @roosterboy

I was using accessibilityLabel(_:) etc in my code so that good, and regarding the aspectRatio(_:contentMode:) I tend to use scaleToFill etc.

Cheers again

Nigel

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.