iphone 5 simulator doesn't load images into UIImage? - iphone

The following code works as expected in a iPhone 4s simulator running either OS7.1 or OS8.1 and displays a non-zero frame size for the image I am trying to load. The same code in a iPhone 5 or 6 simulator doesn't load the image and displays a zero-size frame for it. What's the difference?
UIView *_webBorder;
UIImageView _messagesIcon;
_webBorder = [[UIView alloc] init];
_webBorder.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_webBorder.layer.borderColor = [UIColor blackColor].CGColor;
_webBorder.layer.borderWidth = 1.0;
_webBorder.backgroundColor = [UIColor colorWithRed:0.5 green:0.2 blue:0.9 alpha:0.5];
[self.view addSubview: _webBorder];
_messagesIcon = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"mIcon"]];
[_webBorder addSubview: _messagesIcon];
NSLog(#"message icon's frame is %#", NSStringFromCGRect(_messagesIcon.frame ));
XCode version 6.1.1.

The images were missing from the bundle resources. The mystery is how the iPhone 4 simulator worked without these missing images in the bundle.

Related

Three20 TTTableItem elements background colour changed to white in IOS5

I have a TTTableViewController with a background image in which I insert items of type TTTableLink. Running the app if I drag the cells out of the screen by scrolling up and down for example, when they enter the screen again their background colour is changed to white. I have noticed this problem only in iOS5.
I don't really get when the background is changed, any ideas of how to preserve clear background for the table cells
Here is my code:
-(void) loadView {
[super loadView];
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png"]];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
backgroundView.frame = CGRectMake(0, 0, 320, 416);
[self.view addSubview:backgroundView];
[self.view sendSubviewToBack:backgroundView];
[backgroundView release];
self.tableView.backgroundColor = [UIColor clearColor];
}
-(void) createModel {
TTListDataSource* listDataSource= [[[TTListDataSource alloc] init] autorelease];
TTTableLink *item = [TTTableLink itemWithText:itemName URL:targetURL];
[listDataSource.items addObject:item];
}
There's a pending pull request on three20 which fixes this issue. see https://github.com/facebook/three20/pull/689. post a comment on it, so the repo owner will merge it. I think he forgot about this project :-)
or you can either fix it by changing that line in your source code

Custom UITabBar background image not working in iOS 5 and later

I have a simple piece of code that places a background image on the tabBar.
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabBG.png"]];
[self.tabBarController.tabBar insertSubview:imageView atIndex:0];
[imageView release];
This works fine in iOS 4 but when testing in iOS 5, it doesn't work.
I'm trying to do the following:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabBG.png"]];
NSString *reqSysVer = #"4.3";
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
if ([iOSVersion compare:reqSysVer options:NSNumericSearch] !=NSOrderedDescending) {
// code for iOS 4.3 or below
[self.tabBarController.tabBar insertSubView:imageView atIndex:0];
}
else {
// code for iOS 5
[self.tabBarController.tabBar insertSubView:imageView atIndex:1];
}
[imageView release];
Alas, this isn't working... Can anyone offer a solution?
iOS5 offers the UIAppearance Proxy.
Also, it's best practice to switch your code based on the capability (in this case it's respondsToSelector) instead of iOS version - that's a fragile assumption (who's to say it doesn't change in the future).
You can set it for just that instance or globally for all tab bars:
// not supported on iOS4
UITabBar *tabBar = [tabController tabBar];
if ([tabBar respondsToSelector:#selector(setBackgroundImage:)])
{
// set it just for this instance
[tabBar setBackgroundImage:[UIImage imageNamed:#"tabbar_brn.jpg"]];
// set for all
// [[UITabBar appearance] setBackgroundImage: ...
}
else
{
// ios 4 code here
}
//---- For providing background image to tabbar
UITabBar *tabBar = [tabBarController tabBar];
if ([tabBar respondsToSelector:#selector(setBackgroundImage:)])
{
// ios 5 code here
[tabBar setBackgroundImage:[UIImage imageNamed:#"PB_MD_footer_navBg_v2.png"]];
}
else
{
// ios 4 code here
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *tabbg_view = [[UIView alloc] initWithFrame:frame];
UIImage *tabbag_image = [UIImage imageNamed:#"PB_MD_footer_navBg_v2.png"];
UIColor *tabbg_color = [[UIColor alloc] initWithPatternImage:tabbag_image];
tabbg_view.backgroundColor = tabbg_color;
[tabBar insertSubview:tabbg_view atIndex:0];
}
After reviewing various articles, I found the answer for anyone that's having the same problem:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabBG.png"]];
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
//iOS 5
[self.tabBarController.tabBar insertSubview:imageView atIndex:1];
}
else {
//iOS 4.whatever and below
[self.tabBarController.tabBar insertSubview:imageView atIndex:0];
}
[imageView release];
Works like a charm! Enjoy.
I realize this has been solved, I'm posting this for others who had the same issue as me. I wanted to add a background image to the selected tab in a tabbar. Here is the solution:
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"tabbar.png"]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar-item.png"]];
The second line here adds a background image to the selected tab in a tabbar.
There is something new in iOS 5
forBarMetrics:UIBarMetricsDefault
Here is the apple doc on that
[[UINavigationBar appearance] setBackgroundImage:toolBarIMG forBarMetrics:UIBarMetricsDefault];

UIView Background Image Transparency Problem

I added a subview with an PNG image that should display with transparency, but I am getting all black where the transparency should be.
The code is:
- (void)viewDidLoad
{
[super viewDidLoad];
toolbar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BannerBackground.png"]];
logoImage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 56)];
logoImage.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Logo.png"]];
[self.view addSubview:logoImage];
}
This is code for a iPad, iOS version 4.3. I have the same problem with both the simulator and the iPad.
Since this is the last view added, I expect it to have a z index higher than all of the other views, so the other views should show through the transparent areas. This is not working.
I noticed this behaves differently on iOS 4.3.x than 5.0.x. In 4.3.x I had to set opaque to YES, then set the background image, then set it back to NO.
Set your view's property opaque to no. like this [view setOpaque:NO];
Failing setOpaque:NO... check to see if your image is correctly exported and not corrupted etc.. that happened to me once
Create a UIImageView with transparent backgroundColor and your logo as its image, and add that:
UIImageView *logoView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"Logo.png"]];
logoView.backgroundColor = [UIColor clearColor];
[self.view addSubview:logoView];
This should work.
#import <QuartzCore/QuartzCore.h>
logoImage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 56)];
logoImage.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Logo.png"]];
[logoImage.layer setOpaque:NO];
logoImage.opaque = NO;
[self.view addSubview:logoImage];
Old question, but: any chance that your transparency is, for instance, black and not actually transparent? Not that I just did that or anything.
(When drawing the toolbar icon, iOS fills in any pixels, including black ones, with white. Make sure there's actually nothing where you expect your transparent area to be.)

What is the correct way of adding a background image to a UIView?

Did read on couple of forums that adding a background image with "colorWithPatternImage" will consume more memory than usual.
The bad way:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
Better solution:
UIImageView* iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]]; iv.userInteractionEnabled = YES;
self.view = iv;
[iv release];
I have two question regarding this! which solution is better ? And why ?
I am also trying to figure out how to place my labels on the top of the imageView.
UIColor* tmpColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:#"background.png"]];
self.view.backgroundColor=tmpColor;
[tmpColor release];

UITableViewController background image stretched

I'm trying to set the background image for a UITableViewController. I've got the following code which does set the background fine, but the image itself is being stretched to about twice the usual size.
[[self view] setAutoresizesSubviews:NO];
UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Default.png"]];
self.tableView.backgroundView = backgroundView;
self.tableView.backgroundColor = [UIColor clearColor];
[backgroundView release];
The image is only 640x960 pixels.
I've tried manually setting the frame of the backgroundView but it doesn't change the size of the background image.
Can anyone explain where I'm going wrong?
Many thanks.
In my expirience, you should avoid using
[UIColor colorWithPatternImage: ... ];
as it leads to some problems with picking the right image for Retina/Low-Res devices.
Try something more like this:
UIImageView* bgView = [[[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"Default.png"]] autorelease];
[tableView.backgroundView addSubview:bgView];
Also make sure you have two PNGs:
Default.png - 320 x 480
Default#2x.png = 640 x 960