how to set a imageview at center dynamically on tableview cell - iphone

I'm wondering how to set an imageview to the center of a tableview cell dynamically.
Here is my current code.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 20, 20)];
imgView.image = [UIImage imageNamed:#"friendsCelluserpic.png"];
cell.imageView.image = imgView.image;

Try this code :
This will align your image horizontally centre in TableViewCell
float imgwidth=40;
float imgheight=40;
imgView = [[[UIImageView alloc] initWithFrame:CGRectMake((cell.contentView.bounds.size.width/2)-(imgwidth/2),15, imgwidth, imgheight)] autorelease];
[cell.contentView addSubview:imgView];

try this, :)
UIImageView * imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"friendsCelluserpic.png"]];
imgView.frame=CGRectMake(100, 100, 20, 20);
[cell.contentView addSubview:imgView];
return cell;

you can code like this [cell.contentView addSubview:imgView];

Related

Size of Image on UINavigationBar

I want to put Custom image on my UINavigationBar,
When i tried this two code snippets,
[self.navigationItem setTitleView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]]];
And
UIButton *logoView = [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,30)];
[logoView setBackgroundImage:[UIImage imageNamed:#"logo.png"] forState:UIControlStateNormal];
[logoView setUserInteractionEnabled:NO];
self.navigationItem.titleView = logoView;
image which i can see on my Device is getting blur or it is getting streched.
I made image with Resolution of 180*40(Width*Height)
I want to know the exact size of Image. what, it would be ?
Thanks in advance.
use this
UIImageView *navigationImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 98, 34)];
navigationImage.image=[UIImage imageNamed:#"topNav-logo.png"];
UIImageView *workaroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 98, 34)];
[workaroundImageView addSubview:navigationImage];
self.navigationItem.titleView=workaroundImageView;
hope this helps.
Create Image with size (320 x 44) and set as a Title like bellow..
self.navigationItem.titleView = yourImageView;
See whole example like bellow...
- (void)viewDidLoad{
UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
self.navigationItem.titleView = imgView;
[imgView release];
}
You need to first initialize the titleView. and then set it the imageView as a subView:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
self.navigationItem.titleView = [[UIView alloc] initWithFrame:imageView.bounds];
[self.navigationItem.titleView addSubview:imageView];

UIScrollView in iPhone?

I am working in iPhone application, Using UIScrollView to set in Horizontal, add 5 images in UIScrollView,and its working fine.
(I want to show 2nd image in opening time) Then i tried, when the screen shows, at the time 2nd image show. But i tried my level best, but i did not do that, please any one help me
Thanks in advance
I tried this:
UIScrollView *scroll1 = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 45, 300, 360)];
scroll1.contentSize=CGSizeMake(2700, 360);
[scroll1 setShowsHorizontalScrollIndicator:NO];
scroll1.pagingEnabled=YES;
[self.view addSubview:scroll1];
UIImageView *SlideImage = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 300, 360)];
SlideImage.image=[UIImage imageNamed:#"img1.png"];
SlideImage.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage];
UIImageView *SlideImage1 = [[UIImageView alloc]initWithFrame:CGRectMake(300,0, 300, 360)];
SlideImage1.image=[UIImage imageNamed:#"img2.png"];
SlideImage1.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage1];
UIImageView *SlideImage2 = [[UIImageView alloc]initWithFrame:CGRectMake(600,0, 300, 360)];
SlideImage2.image=[UIImage imageNamed:#"img3.png"];
SlideImage2.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage2];
UIImageView *SlideImage3 = [[UIImageView alloc]initWithFrame:CGRectMake(900,0, 300, 360)];
SlideImage3.image=[UIImage imageNamed:#"img4.png"];
SlideImage3.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage3];
UIImageView *SlideImage4 = [[UIImageView alloc]initWithFrame:CGRectMake(1200,0, 300, 360)];
SlideImage4.image=[UIImage imageNamed:#"img5.png"];
SlideImage4.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage4];
UIImageView *SlideImage5 = [[UIImageView alloc]initWithFrame:CGRectMake(1500,0, 300, 360)];
SlideImage5.image=[UIImage imageNamed:#"img6.png"];
SlideImage5.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage5];
Try this
[scroll1 setContentOffset:CGPointMake(300, 0) animated:FALSE];

combining 2 images For accessory View

I have 2 images which I want to join side by side and show in accessory view.
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(20.0, 20.0, 20, 20);
[[UIImage imageNamed:#"homesmall.png"] drawInRect:imageRect];
CGRect imageRect1 = CGRectMake(0.0, 0.0, 20, 20);
[[UIImage imageNamed:#"update.png"] drawInRect:imageRect1];
UIImageView *statusImageView = (UIImageView *) cell.accessoryView;
statusImageView.image= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I am trying to put both the images side by side but somehow nothing in coming in accessory view.
[cell accessoryView] is UIView, not UIImageView. And I don't get why do you draw your images since you have them in the bundle.
Why not:
UIImageView * image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"homesmall.png"]];
UIImageView * image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"update.png"]];
[image1 setFrame:CGRectMake(0,0,20,20)];
[image2 setFrame:CGRectMake(20,0,20,20)];
UIView * cellAcc = [[UIView alloc] initWithFrame:CGRectMake(0,0,40,40)];
[cellAcc addSubview:image1];
[cellAcc addSubview:image2];
[cell setAccessoryView:cellAcc];
[image1 release];
[image2 release];
[cellAcc release];

Add title and image to navigationbar

I need so set the name of the UIViewController and an image to the navigationBar. So far I am able to display the image, but the title is of course missing.
// show image
UIImage *image = [UIImage imageNamed: #"bar_icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(0, 0, 40, 40);
self.navigationItem.titleView = imageView;
[imageView release];
Thanks for your help,
BR, doonot
// show image
UIImage *image = [UIImage imageNamed: #"bar_icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(70, 0, 40, 40);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 40)];
tmpTitleLabel.text = #"Mannschaft";
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
[imageView release];
[tmpTitleLabel release];
Create a UIView with 2 subview: a UIImageView and a UILabel. Set the titleView to the UIView.

How to set UIImage frame

How i can set the frame size of UIImage.
You can set the frame of the UIImageView:
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imgView.image = [UIImage imageNamed:#"image.png"];
(and don't forget to [imgView release]; somewhere)
UIImage has no frame. It has only width and height. If you want to put it on your view you should create UIImageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,12,123,123)];
imageView.image = image; // image - your UIImage