I have standard iOS app, with a standard app icon contained in Assets.
I'd like to display the app icon within the app (using SwiftUI). Note that I am not asking how to set the app icon, or change the icon dynamically. I just want to show the app icon within the app's own Settings view.
It would appear the App Icon asset should just be like any other, and I could include it using the following (note there is no space between App and Icon in the default icon naming),
Image("AppIcon")
I've also tried experimenting with,
Image("[email protected]") // Pick out a specific icon by filename
Image("icon_60pt@3x") // Maybe it assumes it's a .png
Image("icon_60pt") // Maybe it auto picks most appropriate resolution, like UIKit
...but none of these work.
How do I include the apps own icon within the app, without duplicating it as a separate Image Set (which I have tried, and does work.)
Thanks.
Tap Photo icon, then tap Add new. Set the size for the icon, then tap OK. Select the image you want to use for the app. Crop the image (choose Crop photo or Crop picture, then Always or Just once), then tap OK.
Apple products are copyrighted and can't be reproduced in your app icons.
Tap Add to Home Screen and select the icon next to your shortcut under Home Screen Name and Icon. In the pop-up window that appears, select File, Photo, or Take Photo. You can crop an image to customize what will show, but happily, the image needn't be square or a specific size. Tap Add > Done.
The following works if app icon is correctly set for used device (ie. iPhone icons for iPhone, etc.)
Note: sizes of app icons must match exactly!
Tested with Xcode 11.4
Image(uiImage: UIImage(named: "AppIcon") ?? UIImage())
This works:
extension Bundle {
var iconFileName: String? {
guard let icons = infoDictionary?["CFBundleIcons"] as? [String: Any],
let primaryIcon = icons["CFBundlePrimaryIcon"] as? [String: Any],
let iconFiles = primaryIcon["CFBundleIconFiles"] as? [String],
let iconFileName = iconFiles.last
else { return nil }
return iconFileName
}
}
struct AppIcon: View {
var body: some View {
Bundle.main.iconFileName
.flatMap { UIImage(named: $0) }
.map { Image(uiImage: $0) }
}
}
You can then use this in any view as just:
AppIcon()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With