Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule Request: SwiftUI: Prefer scaledToFit()/scaledToFill() over aspectRatio(contentMode:) #5713

Open
2 tasks done
CraigSiemens opened this issue Jul 29, 2024 · 2 comments
Open
2 tasks done
Assignees
Labels
good first issue Issue to be taken up by new contributors yet unfamiliar with the project. rule-request Requests for a new rules.

Comments

@CraigSiemens
Copy link
Contributor

New Issue Checklist

New rule request

SwiftUI has scaledToFit() scaledToFill() which are convenience methods for calling aspectRatio with only a content mode.

This method is equivalent to calling aspectRatio(_:contentMode:) with a nil aspectRatio and a content mode of ContentMode.fit.
https://developer.apple.com/documentation/swiftui/view/scaledtofit()#

Since Apple considered it a common enough use case to have dedicated view modifiers for it, there should be a rule to prefer using them over the more general purpose view modifier.

Triggering cases

It just needs to be the versions where they're passed a constant.

view.aspectRatio(contentMode: .fit)
view.aspectRatio(contentMode: .fill)

aspectRatio(contentMode: .fit)
aspectRatio(contentMode: .fill)

Non-triggering cases

If a ratio is being passed or the content mode is not a constant, it should not trigger

let ratio = CGSize(width: 1, height: 1)
view.aspectRatio(ratio, contentMode: .fit)
view.aspectRatio(ratio, contentMode: .fill)

let contentMode = ContentMode.fit
view.aspectRatio(contentMode: contentMode)

let shouldFit = true
view.aspectRatio(contentMode: shouldFit ? .fit : .fill)

Configuration

I don't think this needs any configuration.

It's always possible someone else would prefer the aspectRatio method over the convenience method. If that occurs the rule could be modified to controlling the preferred method, though it could also be a separate rule as well since it would be checking for the opposite case.

Opt-in?

Based on the criteria in the readme, this could be enabled by default. Though it depends on what the general consensus is.

@SimplyDanny SimplyDanny added rule-request Requests for a new rules. good first issue Issue to be taken up by new contributors yet unfamiliar with the project. labels Jul 29, 2024
@pranav1160
Copy link

@CraigSiemens please assign this issue to me.

@SimplyDanny
Copy link
Collaborator

SimplyDanny commented Oct 15, 2024

@CraigSiemens please assign this issue to me.

Done. Please go ahead. I'm happy to review your pull request once you're done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issue to be taken up by new contributors yet unfamiliar with the project. rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests

3 participants