change uitabbar background color - iphone

I am write this code but the warning is show "insertSubview" may not respond
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *i = [UIImage imageNamed:#"GO-21-TabBarColorx49.png"];
UIColor *c = [[UIColor alloc] initWithPatternImage:i];
v.backgroundColor = c;
[c release];
[[self tabBar] addSubview:v];
[v release];
}

Did you try
[[self tabBar] insertSubview:v atIndex:0];
Instead of addSubview:
Source: Changing Tint / Background color of UITabBar

Related

iPhone - UITableView background

I want to give a background image to my UITableView, so I tried this method:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *v = [[UIImageView alloc] initWithFrame:self.view.bounds];
[v setImage:[UIImage imageNamed:#"Background.png"]];
[self.view addSubview:v];
[self.view sendSubviewToBack: v];
...
}
This works fine. However, after pushViewController, this method doesn't work anymore (why?). So I changed a method:
- (void)viewDidLoad
{
[super loadView];
// Do any additional setup after loading the view from its nib.
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:#"Background.png"]];
...
}
This can work, but the result is not what I expected. Somehow the background picture changes its size (like a partial enlargement), and the background of cells is darker than the previous one.
Anyone knows the reason? I've been stuck with this problem for a long time.
Thanks a lot!
Use this:
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"TableBackground.jpg"]];
self.tableView.backgroundColor = background;
[background release];
Edit:
UIImage *myImage = [[UIImage alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
myImage.image = [UIImage imageNamed:#"TableBackground.jpg"];
UIColor *background = [[UIColor alloc] initWithPatternImage:myImage];
self.tableView.backgroundColor = background;
[background release];
Try This Code i am sure it'll work
UIImage * targetImage = [UIImage imageNamed:#"coloured_flower.jpg"];
UIGraphicsBeginImageContextWithOptions(tableView.frame.size, NO, 0.f);
[targetImage drawInRect:CGRectMake(0.f, 0.f, tableView.frame.size.width, tableView.frame.size.height)];
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
tableView.backgroundColor = [UIColor colorWithPatternImage:resultImage];
UITableView *tblView=[[UITableView alloc] init];
[tblView setFrame:CGRectMake(0, 0, 320, 300)];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:tblView.frame];
[imgView setImage:[UIImage imageNamed:#"cinemax.jpg"]];
tblView.backgroundView=imgView;
[self.view addSubview:tblView];

how to increase the imageview size insde the picker?

I have an application in which a pickerview is ther with images.I had done that with this.`
picker1=[[UIPickerView alloc] init];
picker1.frame= CGRectMake(30,90,155,316);
picker1.delegate = self;
picker1.dataSource = self;
picker1.showsSelectionIndicator = YES;
picker1.tag = 1;
[self.view addsubview:picker1];
and i am using viewForRow delegate methode to load the images.i need to increase the images frame there inside the pickerview.`
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%#.png",[self.array objectAtIndex:row]]];
UIImageView *icon = [[UIImageView alloc] initWithImage:img];
//temp.frame = CGRectMake(170, 0, 30, 30);
UIView *tmpView = [[[UIView alloc] initWithFrame:CGRectMake(155,75,95,35)] autorelease];
[tmpView insertSubview:icon atIndex:0];
picker1.backgroundColor = [UIColor clearColor];
[(UIView*)[[picker1 subviews] objectAtIndex:2] setAlpha:0.0f];
[(UIView*)[[picker1 subviews] objectAtIndex:0] setAlpha:0.0f];
[(UIView*)[[picker1 subviews] objectAtIndex:1] setAlpha:0.0f];
[(UIView*)[[picker1 subviews] objectAtIndex:3] setAlpha:0.0f];
[tmpView setUserInteractionEnabled:NO];
[tmpView setBackgroundColor:[UIColor clearColor]];
[tmpView setTag:row];
return tmpView;
but when i try to increase the frame of the view its position only changing,not the size.can anybody help me to achieve this?
implement the following pickerview delegate method..
- (CGSize)rowSizeForComponent:(NSInteger)component{
CGSize size = CGSizeMake(320, 60); //input ur custom size
return size;
}

Preload circle for UITableView

I search for a lot forums but nothing found, my problem is that
I need preload some data from the web to load to the UITableView.
I saw some apps solved this problem, when show UITableView they shows "waiting circle" as modal view and after data is loaded just hide this circle.
Is there simple solution to do this?
What I need to do?
What kind of Request do I need: synchronius or asynchronius?
How to show this circle, do I need to show animatedGif or there is some internal/external control for this?
You need to make a NSThread for your "waiting circle"-(Activity Indicator).
1)Threads How do I create an NSThread that isn't the main thread.
2)Activity Indicator How to use activity indicator view.
- (void)showWaitingView {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGRect frame = CGRectMake(90, 190, 32, 32);
UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[progressInd startAnimating];
progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
frame = CGRectMake(130, 193, 140, 30);
UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];
waitingLable.text = #"Processing...";
waitingLable.textColor = [UIColor whiteColor];
waitingLable.font = [UIFont systemFontOfSize:20];;
waitingLable.backgroundColor = [UIColor clearColor];
frame = [[UIScreen mainScreen] applicationFrame];
UIView *theView = [[UIView alloc] initWithFrame:frame];
theView.backgroundColor = [UIColor blackColor];
theView.alpha = 0.7;
theView.tag = 999;
[theView addSubview:progressInd];
[theView addSubview:waitingLable];
[progressInd release];
[waitingLable release];
[window addSubview:[theView autorelease]];
[window bringSubviewToFront:theView];
[pool drain];
}
- (void)removeWaitingView {
UIView *v = [window viewWithTag:999];
if(v) [v removeFromSuperview];
}

Cannot set background image for tabbar

I'm having trouble setting the background image on my tabbar. I've upgraded to iOS5 and Xcode 4.2, and the tabbar background image just stopped working.
The tabbar is only present on my RootViewController.
I managed to use the Interface Builder and applied a background colour which is not exactly what I wanted but will do for now. This only works for iOS5 for some reason. Ideally I would like to do it programmatically. I'm trying to get the following code working:
RootViewController.h
#interface RootViewController : UIViewController {
IBOutlet UITabBar *mainTabBar;
IBOutlet UITabBarItem *settingsBarItem;
IBOutlet UITabBarItem *infoBarItem;
IBOutlet UITabBarItem *aboutBarItem;
}
#property (retain,nonatomic) UITabBar *mainTabBar;
#end
RootViewController.m
-(void)viewDidLoad {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"smallMenuBackground.png"]];
if ([mainTabBar respondsToSelect:#selector(setBackgroundImage:)]){
mainTabBar.backgroundImage = imageView;
}
else{
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *i = [UIImage imageNamed:#"smallMenuBackground.png"];
UIColor *c = [[UIColor alloc] initWithPatternImage:i];
v.backgroundColor = c;
[c release];
[mainTabBar insertSubview:v atIndex:0];
[v release];
}
}
This code is just not working for me, I must be doing something wrong, so any help appreciated.
I'm getting a couple of warning messages aswell:
1) Instance method '-respondToSelect:' not found (return type defaults to 'id')
2) Incompatible pointer types assigning to 'UIImage *' from 'UIImageView *'
EDIT
The entire code is too large to paste here, the RootViewController.h is http://pastie.org/3249124 and the RootViewController.m is http://pastie.org/3249137
this line, shouldn't it be
if ([mainTabBar respondsToSelector:#selector(setBackgroundImage:)])
respondsToSelector: not respondsToSelect:
also, just as it said exactly,
mainTabBar.backgroundImage = imageView;
should be
mainTabBar.backgroundImage = [UIImage imageNamed:#"smallMenuBackground.png"];
because backgroundImage is UIImage while your imageView is UIImageView
Edit
also, I find that the code in your else statement is kind of weird, try if this is working.
else
{
UIImage *i = [UIImage imageNamed:#"smallMenuBackground.png"];
UIImageView* imageView = [UIImageView alloc] initWithImage:i];
[mainTabBar insertSubview:imageView atIndex:0];
[imageView release];
}
EDIT 2:
if ([mainTabBar respondsToSelect:#selector(setTintColor:)])
{
UIImage *i = [UIImage imageNamed:#"smallMenuBackground.png"];
UIColor* c = [[UIColor alloc] initWithPatternImage:i];
mainTabBar.tintColor = c;
}
else
{
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *i = [UIImage imageNamed:#"smallMenuBackground.png"];
UIColor *c = [[UIColor alloc] initWithPatternImage:i];
v.backgroundColor = c;
[c release];
[mainTabBar insertSubview:v atIndex:0];
[v release];
}
you have minor problem you pass imageview in mainTabBar.backgroundImage it's worng , you will pass UIImage ... please check this code
-(void)viewDidLoad {
if ([mainTabBar respondsToSelect:#selector(setBackgroundImage:)]){
mainTabBar.backgroundImage = [UIImage imageNamed:#"smallMenuBackground.png"] ;
}
else{
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *i = [UIImage imageNamed:#"smallMenuBackground.png"];
UIColor *c = [[UIColor alloc] initWithPatternImage:i];
v.backgroundColor = c;
[c release];
[mainTabBar insertSubview:v atIndex:0];
[v release];
}
}

iphone memory deallocation issue with scrollview with image inside

I'm having trouble deallocating a UISCrollView that contains images.
There's something wrong with my code, but I cannot understand where.
this is a snippet of my code. It basically create a loops adding a uiscrollview with an image and removing.
If you run the code with instruments, no memory will be freed.
I also add some check for the retainCount, with no luck at all..
here's the code
- (void)loadView {
[super loadView];
CGRect theRect = CGRectMake(0, 0, 320, 480);
UIView *view = [[UIView alloc] initWithFrame:theRect];
[view setBackgroundColor:[UIColor purpleColor] ];
self.view = view;
[view release];
UIView *pippo = [[UIView alloc] initWithFrame:theRect];
[pippo setBackgroundColor:[UIColor redColor]];
[self.view addSubview:pippo];
[pippo release];
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
int dd = [img retainCount];
UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect];
[v2 setImage:img];
[scroll addSubview:v2];
dd = [v2 retainCount];
[v2 release];
dd = [v2 retainCount];
dd = [img retainCount];
[self.view addSubview:scroll];
[img release];
dd = [img retainCount];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
-(void)scrollRemove:(id)o {
int dd = [scroll retainCount];
UIImageView *theV = [[scroll subviews] objectAtIndex:0];
dd = [theV retainCount];
[scroll removeFromSuperview];
dd = [theV retainCount];
[theV release];
dd = [theV retainCount];
dd = [scroll retainCount];
scroll = nil;
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
The issue is that you are releasing img when you shouldn't (which is probably being masked by the view hierarchy never being released), and you aren't releasing scroll when you should.
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
int dd = [img retainCount];
UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect];
[v2 setImage:img];
[scroll addSubview:v2];
[v2 release];
[self.view addSubview:scroll];
[img release];
dd = [img retainCount];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
This should be:
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect];
[v2 setImage:img];
[scroll addSubview:v2];
[v2 release];
[self.view addSubview:scroll];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
Of course if you do this you need to also make a slight change in your view removal path:
-(void)scrollRemove:(id)o {
[scroll removeFromSuperview];
[scroll release];
scroll = nil;
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
I've basically reached the same conclusions as #Louis, but also made some comments within the code as to what I removed and why.
- (void)loadView {
[super loadView];
CGRect theRect = CGRectMake(0, 0, 320, 480);
UIView *view = [[UIView alloc] initWithFrame:theRect];
[view setBackgroundColor:[UIColor purpleColor] ];
self.view = view;
[view release];
UIView *pippo = [[UIView alloc] initWithFrame:theRect];
[pippo setBackgroundColor:[UIColor redColor]];
[self.view addSubview:pippo];
[pippo release];
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
// img is already autoreleased, you're releasing again down below
// --- UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
UIImageView *v2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"koala1b.jpg"]];
v2.frame = theRect;
//--- [v2 setImage:img];
[scroll addSubview:v2];
[v2 release];
[self.view addSubview:scroll];
// NO !; img is autoreleased from imageWithContentsOfFile
//--- [img release];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
-(void)scrollRemove:(id)o {
//--- UIImageView *theV = [[scroll subviews] objectAtIndex:0];
[scroll removeFromSuperview];
// you don't own theV because you didn't create it here, or send it a retain. So NO release
// it also appears that you think you're responsible for releasing or cleaning up
// a view's subviews. NO. Just call [scroll removeFromSuperview] and let scroll view
// clean it's own resources.
//--- [theV release];
[scroll release];
scroll = nil;
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}