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

Custom image/icon #680

Open
Bilalwarind opened this issue Jan 27, 2024 · 7 comments
Open

Custom image/icon #680

Bilalwarind opened this issue Jan 27, 2024 · 7 comments

Comments

@Bilalwarind
Copy link

Bilalwarind commented Jan 27, 2024

How we can use custom image or icon from assets folder other than SF symbols?

@Gregoirevda
Copy link
Contributor

patch the library and use UIImage(named: image)

@Gregoirevda
Copy link
Contributor

Gregoirevda commented Mar 27, 2024

@Bilalwarind Seems like this was merged: https://github.com/react-native-menu/menu/pull/731/files
but not depoyed to npmjs yet

@ptahdunbar
Copy link

@Gregoirevda @Gregoirevda its deployed in 1.0.0 — but how do you use it?

@honcon
Copy link
Contributor

honcon commented Apr 26, 2024

You can pass the image name from an image you have put in xcassets

@alaughlin
Copy link

@honcon are you able to elaborate a little more for us react native devs who never really have to touch the native code? in my case I'm using EAS so the ios dir isn't even part of the repo - it's generated at build time

@honcon
Copy link
Contributor

honcon commented May 2, 2024

I think something like this https://github.com/EvanBacon/expo-apple-targets could be a solution

@hesselbom
Copy link

Here's how I solved adding custom icons to Expo project using CNG/prebuild.

It involves creating an asset and writing a custom Expo config plugin to copy it into XCode on clean prebuilds.

  1. In XCode, create a new asset (New File... > Asset Catalog) and add all your images there, let's say you call it MyImages.
  2. Right click on that asset, press Show in Finder, then move the file to your project root so it's not part of XCode anymore.
  3. Create a file called plugins/withXcodeBundleResource.js with the following content (inspired by this comment):
/* eslint @typescript-eslint/no-var-requires: 0 */
const path = require('path')
const { withXcodeProject, IOSConfig } = require('expo/config-plugins')

function withXcodeBundleResource(config, fileArray) {
  return withXcodeProject(config, async (config) => {
    for (const file of fileArray) {
      config.modResults = await setFileAsync({
        file,
        projectName: config.modRequest.projectName,
        project: config.modResults,
      })
    }
    return config
  })
}

async function setFileAsync({ file, projectName, project }) {
  const thisFilePath = path.join('../', file)
  if (!project.hasFile(thisFilePath)) {
    console.log(`Adding ${thisFilePath} to Xcode project`)
    IOSConfig.XcodeUtils.addResourceFileToGroup({
      filepath: thisFilePath,
      groupName: projectName,
      project,
      isBuildFile: true,
    })
  }

  return project
}

module.exports = withXcodeBundleResource
  1. Then in your app.json, add it as a plugin with an array of files to copy, in this case your newly created MyImages.xcasset:
[...]
    "plugins": [
      [
        "./plugins/withXcodeBundleResource.js",
        ["./MyImages.xcassets"]
      ],
[...]
  1. Now I can use any image from MyImages.xcassets as the image value for an item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants