Xamarin.Forms ContentPage.BackgroundImage - ios

Xamarin.Forms
VS2013 IDE
I know there are other topics on this, but they don't answer my problem.
For Android, the background image works fine.
BackgroundImage = Device.OnPlatform("Resources/HTbg#2x.png",
"Drawable/HTMobile.png",
"Images/HTMobile.png");
For iOS, I can't get it to work.
I've tried the normal and retina versions of the image, which are of
the correct dimensions for those screens.
I've tried placing them in the root folder of the project
I've tried images with transparent or solid colored image backgrounds
In iOS, the background image, in any of the scenarios above, just shows a white screen ontop of all the CustomContentPage controls. I comment out the background line of code, and my page shows normally, with all controls and navigations links...
With the following code (no 'Resouces' folder), the page shows normally, but no image...
BackgroundImage = Device.OnPlatform("HTbg#2x.png",
"Drawable/HTMobile.png",
"Images/HTMobile.png");
How do you set the background image in iOS?
For this answer, I need to know the following:
Where do you put the image in the project so it's picked up properly?
What type of image do you use? Size? Resolution? Color depth? Transparency?
How do you let iOS 'know' what image to use if it's a 4s, 5s or 6/6+ iPhone?
EDIT
ContentPage.BackgroundImage is of type FileImageSource - meaning the correct resolution is loaded automatically; therefore the call should be...
BackgroundImage = Device.OnPlatform("HTbg.png",
"Drawable/HTMobile.png",
"Images/HTMobile.png");
But this still doesn't work. I'll add in the 4-inch image version and see if that helps...

This problem on IOS is solved by adding the required image to an IOS asset catalog. Xamarin.Forms WILL NOT load an image into the BackgroundImage property when it is located in the Resources folder as for versions of IOS pre 7. This might save someone some time.

Related

I can't create a splash screen for a Flutter app, using Xcode for iOS

I read lots of articles about the Splash Screen and how to create it in Flutter but almost all of them are deprecated now. Neither one worked for me, and I did try by myself, but the Splash Screen is not displaying, instead is displaying a white background.
I found out a package which could make my life easier, but I think it's better to create the Splash Screen as it is created from the official documentation.
As I understood, I opened the Runner.xcworkspace folder in Xcode. After that I uploaded 1x, 2x, 3x images which were the logo with transparent background. (I used the same size)
I didn't find the real sizes for 1x, 2x, 3x images anywhere for 2021/2022. A few people put there full images, while the others put only the logo.
I decided to change the background color from the View property as in the image below and I've fixed the image with the drag option and I am pretty sure that it's not a good approach.
So, even if I created somehow the Splash Screen, it is not rendered on the devices. I restarted the emulator, deleted the app and didn't work for me.
The question is, how to create Splash Screen for iOS in a correct way and why it is still a white background?
Edit: I managed to display the Splash Screen selecting this property in the image above, but as I expected, it is not rendered well. So the question remains: how to create Splash Screen for iOS in a correct way?
Edit 2: The logo is now centered by autoresizing arrows from Size Inspector, but after 1 second it is resizing between 1x, 2x, 3x. Also, I used 3 constraints.
Chose your logo and click here:
image
Use alignment constants and set your logo 0 horizontally and 0 vertically. (Be sure you have chosen your logo file). Click "Add 2 Constants" It will show your logo in a centre at every Apple device.

Launch Screen not working on iOS 14 with Xcode 12

I am very frustrated now. I upgraded Xcode to version 12 and tested my app on iOS 14. Now the problem is, that my launch screen is just showing in black. I tested it with an iOS 13.5 device and it is still working as expected. I tried to remove the launchscreen.storyboard approach and added the Launch Screen key in the info.plist, but then the image is scaled to full size.
Now I tested a little bit with the launchscreen.storyboard and I found a few things.
If I remove the Image view, the launch screen is showing as expected. I added just a label and that would work.
If I use an image from the system in the image view, it is working as well. It is just not working when I am using an image from the project.
Did you experience issues with iOS 14 and the storyboard approach?
If yes, how did you fix it?
A work around that seems to be working for me:
For a strange reason if I am downsizing the image then it is working fine. I had a full size image of dimension 2732x2732 and it didn't work. Reducing the dimension of the same image to 2400x2400 is working fine. I have tried with 2500x2500 and 2600x2600 but none worked.
Most importantly, every time I change the image I had to restart the simulator to reflect the updated image.
It didn't work on the real device yet. Like the simulator, I had to restart the device and then only it worked.
Not sure if it is something with the dimension/size of the image or with the reboot.
Further updates:
I uploaded the same build (with smaller image) to TestFlight and installed it on my device (basically updated from the previous version having large size image) and unfortunately the issue persisted :(
Now what I did is uninstalled the app and reinstalled it again from TestFlight. Issue still persisted.
Finally I had to delete the app, reboot my device and install the app again. This is when it worked fine.
So here are a lot of good ideas, but I was able to finally solve the issue - it is more like a workaround. I needed to store the picture outside the Images.xcassets folder and then it started to work again. This is a very weird issue.
A workaround is to use a button with an image instead of an imageview in the launch storyboard. It works like a charm!!
One reason might be the size of the images. Since iOS 14 images for launch screen are limited to 25 MB as stated in Apples Human Interface Guidelines.
An image with 2.400x2.400 pixel will result in a memory size about 22 MB. With 2.500x2.500 we reach the 25 MB.
We had the same problem where the full screen image in LaunchScreen.storyboard was black on all #3x devices like iPhone 12. Reducing the size of only the #3x launch image from 3.072x3.072 to 2.400x2.400 solved this problem.
A square image is the easy way to support landscape and portrait orientation. But it should also work to have different images for each orientation, where one image in total is less than 25 MB.
Had the same problem. The issue was the .png image used in the launch screen storyboard. This lead to the whole launch screen just being black.
What fixed it in our case was:
Choose a smaller image
Delete App from device/simulator
Restart device/simulator
I found that the image was the source of error by replacing the image with an other one in our app. It immediately worked with a different image (no restarting or else required).
The only workaround that works for me was to add the images outside Images.xcassets folder. After modified this and rebuild the app, it worked expected.
Some issue it's happening on the iOS 14 or XCode 12.
I encountered this problem
when using LaunchScreen.storyboard and used the image on Assets.xcassets, not working on iOS 14 with Xcode 12.
I solved it by try put change 3x image 2436×1125,and set 2x image 828×1792.
Finally I had to delete the app, reboot my device and install the app again. This is when it worked fine.
I had this, too. Pretty infuriating. My launch screen is merely a background image (2048x2048 PNG) and a graphic (2048x2048 PNG) in the middle.
The fix for me was to have a smaller middle graphic (720x720 PNG), remove the images from any assets catalogs, and add them into the project.
The images didn't show on the Simulator until I restarted it. When built and installed to my iPhone, I didn't have to restart it.
In my case, the ViewController property, "Is Initial View Controller" was not checked. Also, be sure to set the color of the view to an actual color and not a system color, otherwise you can run into issues with the device theme settings. That is, if you like to show a specific color.
Also, to access that setting make sure you click on the actual View Controller object in the drop down panel.
The only thing that worked for me was:
Delete the existing LaunchScreen.storyboard & create a new one
Check " Use as Launch Screen "
Check " Is Initial View Controller "
Make sure the image your using is NOT located under Assets.xcassets folder. I put my image in the root of the project
Delete the app, restart the simulator, and run the application
BINGO!!!
I have a similar issue with XCode12 / iOS14 and React project:
The launchscreen.storyboard was working fine in previous versions. Since update to XCode12 it shows as following:
Lauching into a black screen (2-3 seconds)
Then showing the Launchscreen correctly
If I try to use the new "Launch Screen" dictionary in Info.plist I have the follwing issues (same in another Xamarin App):
Image is zoomed in / not showing completely
Background color setting is completely ignored (tried RGB and Color.xcasset)
This is just completely bugged since Xcode 12 !
So I've had this bugged with Apple via their feedback system since June 26th 2020 - FB7795777.
I've provided my project and many diagnostic reports and they've never come back with a fix.
However, today, I fixed it.
Basically, there has been some change in the iOS/iPadOS SDK about how it's support .PNG and .JPG files (size or dimensions are the problems I think) in the Launch screen process.
The solution:
Open your .PNG file in Photoshop and re-export it with the 'Smaller File - 8-bit' option checked. It shrunk it down to less than half the size, and it worked first time.
In my case following did the job:-
Remove Launch screen file from Project -> General -> App Icons and Launch Imagees -> Launch screen file.
Go to info.plist file and open as source code.
Add following code there and change the image name:-
<key>UILaunchScreen</key> <dict> <key>UIImageName</key> <string>splash(This is your image name)</string> </dict>
In iOS 14 this new key was released (UILaunchScreen). So take care from next time. If you get error then check info.plist file.
Happy coding :)
That worked for me was:
Put xxx.pdf image into Assets.xcassets and set "scale" to Single Scale
In Launch Screen.storyboard, set the imageView's image is xxx, then set imageView's width, height, centerX and Bottom to View
Then worked for me
Xcode 12.5.1, iPhone 7 Plus, iOS 14.6
In my own case , I solved the problem in these few steps:
Image must be 1024px, then
Add to assets
-> A workaround in case you have a much bigger image, use these simple steps:
Generate your app icon
Go to the app icon folder
Copy the 1024 px of that image andappicon image
Then add to assets.
Both methods works perfectly.
This may help others so I am adding it here.
I use Cordova for my hybrid app, and then I load my image assets that are generated by Asset Catalog Creator (which is a great little product).
This was working perfectly until I updated to Xcode 12 and tried to run it under iOS 14.
What I discovered is that Cordova was adding a "CDVLaunchScreen.storyboard", and they had a "LaunchStoryboard.imageset" (directory) in their asset catalog, which contained just a contents.json file.
I simply copied this into my asset catalog, and when I drag and drop this into my updated project (newly generated each time by Cordova), my Launch Image displays again.
Don't ask my how or why Xcode magically wires this stuff together, but it worked for me.
I can pass along the contents.json that worked with my asset catalog if it will help.
BTW, my General settings screen does NOT have a Launch Screen File selected, but Xcode seems to find the required files.
I've got yet another potential solution to this, as none of the existing solutions worked for me.
I use PDF vectors in my Asset file, and this normally works great. But it turns out that it doesn't work on the launch screen - it just shows skips the launch screen entirely if it contains a PDF image.
Swapped it for a large PNG, problem solved.
What worked for me:
Use smaller images like #Darkpaw suggested. Launch storyboards are, after all, intended to be indicative of the coming UI, not artwork based.
Use PNG image (not PDF) and don't put it in an assets catalog like suggested by #Sylber
Just noting separately as I had black/broken launch mages until I did BOTH steps.
You might be seeing this issue if your #1x, #2x, #3x images are blank in your image xcassets folder.
Make sure you have images for each.
You can also select your 1 image, and set Scales as "Single Scale" in the Attributes inspector (on the right hand side)
In Xcode 13 launch screen was not displaying rather a black screen was coming.
But this worked for me Project -> General -> App Icons and Launch Images -> Launch screen file -> change Main to Launchscreen.
Most answers mentioned here are workarounds, the issue i noticed is that when you modify the launch screen assets, do check the item file name too.
Assets -> Show in folder
Then check the launchscreen items folder and make sure that the item inside the folder has the same name as folder.
To expand on Thorsten Stark's answer above...
Since iOS 14 images for launch screen are limited to 25 MB
For me it appeared to be an accumulated size of assets on my Launch Screen storyboard file. When I removed a number of them I no longer saw the black screen.
I ended up just using one image 2778 x 1284 which was 600kb in size. This solved the problem.
To further test, I duplicated the image to make 3 copies on my Launch Screen storyboard file, and sure enough, black screen again.
The best solution to me in 2022...
reduce sizes of your images, in my case:
splash.png (256x256)
splash#x2.png (512x512)
splash#x3.png (768x768)
only doing this and put them in Resources folder then reference in
ImageView work like charm....
alternative:
use Button in place of ImageView works too

image view only shows background color in launch screen

I'm having some problems with my app's launch screen storyboard.
When I run the app on an iPhone 5S, the image is displayed. However, when I run the app on an iPhone 8, the background color of the UIImageView is shown.
In the project settings, at App Icons and Launch Images, I am not using an image source (although trying do so doesn't seem to work either) and the Launch Screen File is set to LaunchScreen.
The View Controller has Is Initial View Controller checked and the file has Use as Launch Screen checked. The image set was brought in using Import... and is universal.
I've read through several of the solutions for black screen here, but switching the background color revealed that was where my black screen was coming from.
In the assets file, the image set has 1x # 640x1136, 2x # 1242x2208 & 3x # 1125x2436. I experimented with other combinations, but this is the first set that let me see the actual image on the 5S. I'm pretty sure the issue is not having the right configuration of dimensions in the image set file, but can't be certain.
Any help would be much appreciated. Thanks in advance.
Had the same thing. Working on the simulator but didn't work on the device (or any other possible option). It's just a bug.
Try these until it works:
Turn simulator/device off and then on again.
Hard clean simulator. Turn it off.
Clean/Hard Clean/Delete Derived data
Reboot Xcode
Hope one of these helps.
If not, please include the screenshots of working / non-working app screens.

Template Image doesn't get tinted on Launch Screen Storyboard

I am setting up my launch screen with storyboard, and I'm using an image that is set to be a PDF template image. My goal is to put this image in a UIImageView and set tint color to it via Interface Builder.
This approach works fine if I have such image view in my Main.storyboard, but somehow, on LaunchScreen.storyboard, the image doesn't get tinted and takes on the actual color rendered on the PDF.
I looked around for information online but haven't found much info on proper use of template image on Launch Screen.
Is using tint color supported on Launch Screens? If it is, what's the appropriate way to set it up?
Launch screen storyboards work differently than a general storyboard. They are basically substitutes for launch screen images.
There are some restrictions for them: they cannot run code, nor they can use custom fonts etc. These restrictions include not being able to use tint colors.
Tamás Sengel's answer is partially correct. But for now, you can use tintColor in LaunchStoroboard.
Follow these steps:
Go to Assets.xcassets and set Render parameter of your image to Template Image.
Then add your image and set it tintColor.
Notice that, if it doesn't work, this is not a reason to be upset. Try to reset your emulator by the following way: run Simulator and click Device -> Erase all Content and Settings. Аnd it should work.
In my case, my image wasn't showing at all. So I read about the following trouble shooting hints somewhere:
uncheck Clears Graphics Context in the attributes inspector of the UIImageView in your LaunchScreen.storyboard
delete app
restarting the iPhone/iPad
reinstall the app
try if it worked and repeat all the steps a couple of times until it works :D
After that I had no issues tinting a PDF image asset marked as Render as Template Image in the asset catalog. Tested on iOS 11 and 12.
Unfortunately those caching issues and all the Launchscreen caveats are poorly documented by Apple.

How to force universal app on iPad into scale mode?

I created an universal iOS app. On all iPhone variants I want native resolution without scaling, so I created splash screen images for all available sizes in images.xcassets/LaunchImage.launchimage and set them all in Xcode:
Now, this works very well for all iPhone versions up to the biggest iPhone 6 Plus.
On iPad (with high pixel density) though I don't want native resolution. Instead I want it to scale the app (even if it looks a bit blurry then, but that's ok).
How would I do that?
In image assets you have to provide proper size for each launch screen, You can not use the same image for two devices in launch-images ( unless you have two copies of the same image ).
If you want to make app-size smaller, implement launch screen, set its background color to your desired background color and put your logo in the center of it. You can now remove iPhone 6,6Plus images from image assets, but you still have to provide launch images for iOS 7 devices (old iPhones / iPad ).
Don't use the Asset Catalogue. Create a LaunchScreen.xib , add a UIImage inside the view. Open the attribute inspector in the right hand panel and set the view mode to Aspect Fill.
The solution was quite easy, I simply changed project type from "universal" to "iphone" and that did the trick!

Resources