Custom button not appearing on iOS device - ios

So I'm trying to implement a custom button in my iOS app to replace the default rounded rectangle button. Heres a code snippet:
UIImage *normalImage = [[UIImage imageNamed:#"Images/whitebutton.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
UIImage *pressedImage = [[UIImage imageNamed:#"Images/bluebutton.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[self.theButton setBackgroundImage:normalImage forState:UIControlStateNormal];
[self.theButton setBackgroundImage:pressedImage forState:UIControlStateHighlighted];
When I run it in the simulator everything works fine, my custom button shows up. However, when I run it on my actual device the button just appears as the default one with no customisation at all. Anybody got any idea where I'm going wrong?

The iOS file system is case sensitive, unlike the sinulator's. I'd start by checking that "Images" directory.
(Quick test if I'm barking up the wrong tree: does the +imageNamed: call return nil?)

You should add whitebutton.png and bluebutton.png to your project. And call imageNamed:#"filename" not imageNamed:#"file path" . Also , when running on the simulator there is no problem with case sensitivity. On the device , you have to type the correct name of the file because it IS case sensitive.
Hope this helps.
Cheers,
George

Your resources are going to get flattened in your app bundle. Also, the filesystem on iOS is case sensitive while the filesystem on Mac OS X is case preserving, so make sure the actual filenames of your images are case-correct. If they are, just remove the Images/ prefix and it should work:
[UIImage imageNamed:#"bluebutton"];
Also, if the image is a png type, you don't need to specify the file extension with imageNamed:.

Use
[UIImage imageNamed:#"whitebutton.png"]
and
[UIImage imageNamed:#"bluebutton.png"]
Regardless of how you have it visually organized in Xcode, it all gets flattened in the bundle

Related

How to change image with device language? Xcode

I have a background image that has some texts too. So I want to change it with the device language. But the background changes with if statement in my code. So I need to change the image itself to optimize it for another language. How can I make this happen?
You can do this by creating a UIImage with the "imageNamed" constructor, but use an NSLocalizedString for the image name:
UIImage* localizeImg = [UIImage imageNamed:NSLocalizedString(#"imageNameKey", nil)];
Then in your localized .strings files you just put the name of the image that you want tied to that language.
"imageNameKey" = "englishBackground.png";
Apply the newly created UIImage to the background and voila! Hope that helps!

Xcode 6: Images Not Showing Up On Device

I am creating UIImageView(s) and adding these views to a UIScrollView. The images show up when I am testing on a simulator, but when I test on an iphone 5s the images don't show up. Below is a list of things I have tried:
Clean Build
Reinstall App
Then I thought maybe it was a problem with retina images so I added retina images (the images are white pngs):
I am using the images as such:
UIButton *doneBttn = [UIButton buttonWithType:UIButtonTypeCustom];
[doneBttn setImage:[UIImage imageNamed:#"addButton.png"] forState:UIControlStateNormal];
[doneBttn setImage:[UIImage imageNamed:#"addButtonSelected.png"] forState:UIControlStateHighlighted];
[doneBttn setFrame:CGRectMake(0, 0, 50 , 30)];
[doneBttn addTarget:self action:#selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[uiNavView addMenuBarButtonRight:doneBttn];
I know the code above works, because the image is displayed in the simulator.
What do I need to change to get the image to display on my device? Also, I have checked to make sure the files are in the xcode project directory, and not just linked.
I also faced this issue once, the cause of this issue was that I am not taking care of image name by assigning it to UIImageView, make sure that you are assigning UIImageView the exact name of the image as it is in the main bundle.
The simulator can read the image name if there is any case-sensitive issue or some extension issue like i.e. .png , .jpg, but device never reads the image name if there are issues in the naming conventions, device always treat the image name as exact.

Picture doesn't show on iPhone when running app

I have set the backgroundColor to a picture which is located in the project folder. When I run the app in the simulator the pic is shown. But when running on my real iPhone it's just white. What can be causing this? Could it be because I have <0.5gb free on both the computer and iPhone?
I had similar situation.
It seems the simulator - Xcode is cacheing the image somewhere, but at build time is loosing.
Try: uninstall the app from simulator, device.
Clean the project.
Force close XCode, not just the project, close it.
Open, clean project, build
At this step should match the device and simulator behaviour, hopefully it is there the desired image.
Please check spelling of the image name you used. In actual devices it's case sensitive.
That should be plenty of free memory. When adding the file to your project, and make sure you choose the options for the image to be copied into the project and to be part of the target.
EDIT: my above suggestion is only useful if the UIImageView you are trying to load points to a picture, and I guess in this case you are just using a blank UIImageView and setting background color.
Also, you might want to look at the arrangement (ie. send to back, send to front, etc. Try sending to front in case there was something in front of it blocking the view) I think these options are in the menu bar under Design --> Arrangement
Could you post some code?
You'll want to add the image not as a backgroundColor, but as a UIImageView subview.
// load the image
UIImage *image = [UIImage imageNamed:#"image.png"];
// create the image view using the image
UIImageView *imageView = [UIImageView alloc] initWithImage:image];
// add the image view as a subview
[yourview addSubview:imageView];
First try to Uninstall the app Clean the project then Install and then check if not then.
Second there may some issue with picture spelling Case senstive or Not As The iPhone is case sensitive, but the simulator is not because iPhone file system is case sensitive and Mac OS is not
Third
Add a break point where you add UIImage
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
imgView.image = [UIImage imageNamed:#"a_image.png"];
Then on the console "po [imgView image]"
If it is nil then either the resource is not getting copied correctly into the bundle.

iOS UIImage returns null

Why is this not working?
//set up check mark image
imgCheckMark = [UIImage imageNamed:#"checkmark.png"];
vImgCheckMark = [[UIImageView alloc] initWithImage:imgCheckMark];
NSLog(#"%#", imgCheckMark);
I keep getting null in the console. The image is in the bundle, I can view it in editor.
Remove the image Checkmark.png from the project
add it back and try it
Make sure that the image is in the project folder[while adding check
copy items into destination group (if needed)]
Make sure the image name is exactly the same as in the file
Check the filename for case sensitivity. If your file is Checkmark.png then it will not work properly on the device. This questions has a few more details:
Works on iPhone Simulator but not on device

UIImage imageNamed not work on ios 5.0

I use this code:
[image setImage:[UIImage imageNamed:#"some#2x.png"]];
and it work on ios 4.3, but on ios 5.0 I don't see my image.
Anybody know which kind this problem?
You normally don't need to include the #2x in the name - iOS should automatically pick the right filename (some.png or some#2x.png) depending on the scale of the screen. Have you tried this:
[image setImage:[UIImage imageNamed:#"some.png"]];
instead?
As Martin (the other :) ) said, you should'nt add #2x if you have both yourImage.png and yourImage#2x.png.
I also listed many reasons of problems that could occurs with imageNamed here

Resources