How to add image bottom of the tableview in iOS - ios

I want to add image on bottom of the tableview. But it should not scroll along with tableview
Please anybody have idea please share.
I am trying as follows
UIImage *img = [UIImage imageNamed:#"image.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
imgView.frame = CGRectMake(0, 0, img.size.width, img.size.height);
CGRect frame = imgView.frame;
frame.origin.x = 0;
frame.origin.y = self.tableView.frame.size.height - img.size.height;
imgView.frame = frame;
imgView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:imgView];
But it is scrolling along with tableview

You can use setTableFooterView method of `UITableview, to set a view at the bottom of the table view.
Swift:
self.tableView.tableFooterView = UIImageView(image: myImageView)
Objective-C:
[[self tableView] setTableFooterView:[self myImageview]];
Hope this helps

Just add an image ON TOP of the table, not within the table (or as a "footer" view)
You can do it like this in your storyboard or XIB file:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIImage *myImage = [UIImage imageNamed:#"bluebar.png"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease];
imageView.frame = CGRectMake(10,10,300,100);
return imageView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 10;
}
U can add your image to the footer.
OR
U can create a cell and add image image to that custom cell and attach that cell to footer

change in your code
frame.origin.y = self.tableView.frame.size.height + self.tableView.frame.origin.y;

Related

How to create slideshow of images within scrollview with autolayout?

I have a collectionview that loads a series of cells. Some of the cells will have static images (which works well), but some might have multiple images that will need to be swipeable.
The UICollectionViewCell is the base view, the UIScrollView is within it (pinned to the top, left, bottom, and right of its superview).
Adding a UIImageView to the scrollview, I want to pin it to the top and left of the scrollview, but have its width and height be the same as the collection view cell, not the content size of the scrollview.
Then I'd like two additional images to be pinned to the top of the collection view cell, and the left closest view (in this case, the prior UIImageView).
The Storyboard would look something like this:
(with the other images off-screen and not displayed).
I decided to add the images via code:
There's a parent view, and a scrollview (attached to an IBOutlet).
The CollectionViewCell subclass contains:
#synthesize lbl, sv, pageControl;
- (void) awakeFromNib {
sv.userInteractionEnabled = NO;
sv.delegate = self;
pageControl.userInteractionEnabled = NO;
[self.contentView addGestureRecognizer:sv.panGestureRecognizer];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = sv.frame.size.width;
float fractionalPage = sv.contentOffset.x / pageWidth;
NSInteger page = lround(fractionalPage);
self.pageControl.currentPage = page;
}
Then, within the View Controller's collectionView:cellForItemAtIndexPath method, I manually add images like so:
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[myArray objectAtIndex:indexPath.row]]];
img.frame = (CGRect) { 0, 0, cell.frame.size.width, cell.frame.size.height};
[cell.sv addSubview:img];
img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[myArray objectAtIndex:indexPath.row + 1]]];
img.frame = (CGRect) { cell.frame.size.width, 0, cell.frame.size.width, cell.frame.size.height };
[cell.sv addSubview:img];
img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[myArray objectAtIndex:indexPath.row + 2]]];
img.frame = (CGRect) { cell.frame.size.width * 2, 0, cell.frame.size.width, cell.frame.size.height };
[cell.sv addSubview:img];
[cell.sv setContentSize:CGSizeMake(cell.frame.size.width * 3.0, cell.frame.size.height)];
I would have preferred a StoryBoard/Interface Builder way of doing this, but for now, this will suffice.

How to position a image in customized section header so that it appears correctly in both the orientations?

I have a customized section header, in viewForHeaderInSection, I have a view and a button on that so it will be like clicking the section header some action will be performed.Now I need an image in the section header so that it changes on clicking the section header.So I added a image view to the view in viewForHeaderInSection but the image position is not correct,it is different in landscape and portrait.I need the image in the right corner of the section header.I need to do it immediately.
Please help.
Thanks
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/* Create custom view to display section header... */
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 18)];
UIButton *btn= [[UIButton alloc] initWithFrame:CGRectMake(10, 5, self.tableView.frame.size.width, 18)];
[view addSubview:btn];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(275.0f, 1.5f, 8.0f, 18.0f)];
UIImage *image = [UIImage imageNamed:#"Selection-Indicator.png"];
[imageView setImage:image];
[view addSubview:imageView];
return view;
}
oky try this hope this helps u
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/* Create custom view to display section header... */
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 18)];
UIButton *btn= [[UIButton alloc] initWithFrame:CGRectMake(10, 5, self.tableView.frame.size.width, 18)];
[view addSubview:btn];
UIImageView *imageView = [[UIImageView alloc]initWithFrame: CGRectMake(view.bounds.size.width - 20, 1.5f, 8.0f, 18.0f); //set the offset from right
UIImage *image = [UIImage imageNamed:#"Selection-Indicator.png"];
[imageView setImage:image];
[view addSubview:imageView];
return view;
}
and in rotation handling method u need to reload or update the UI
for example
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//so u dont want to reload the tableview then u can reload the sections
// [self.tableVIew reloadData]; //just try this (comment)
//try this
NSArray *cells = [self.tableVIew visibleCells];
NSIndexPath *indexPath = [self.tableVIew indexPathForCell:(UITableViewCell *)[cells lastObject]];
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, totalSectionCount)]; //try by placing the all the section count
[self.tableVIew reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
}
You should have something like this:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
[imgView setFrame:CGRectMake(parentView.frame.size.width-50, 5, 30, 30)];
[parentView addSubview:imgView];
The sizes in CGRectMake are only as an example, but make sure your UIImageView is always related to you parentView.frame.size.width... that should do it!
EDIT: After your code, you should replace:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(275.0f, 1.5f, 8.0f, 18.0f)];
with
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(view.frame.size.width-15, 1.5f, 8.0f, 18.0f)];
play with the -15 to put the image where you wish...
i hope it helps!

UICollectionView with Custom UICollectionViewCell

I have a UICollectionView with a custom cell. I had this working completely till I tried to create the collection view programmatically rather than from the storyboard. I did this because I was not able to change the frame size of the collection view for different screen sizes. So I tried to create a collection view programmatically. My problem is my custom cells are not working.
For my custom cells I draw a circle in the view, user cornerradius on the view of the cell, have a UILabel and a UIImageView.
I draw the circle and the perform the cornerradius thing in the drawRect: of the custom cell. The rest i.e: put image in ImageView and text in UILabel, I do it in the collectionView datasource method.
The problem I am having is I don't get the cornerradius thing working although weirdly enough I can draw a circle on the view. second problem is I don't get the image in the image view and no text in the label
Here is my code. I create the collection view in ViewDidload: method.
[layout setSectionInset:UIEdgeInsetsMake(24, 10, 24, 10)];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[layout setMinimumLineSpacing:15];
[layout setMinimumInteritemSpacing:10];
self.collectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:layout];
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
[self.collectionView registerClass:[customCell class] forCellWithReuseIdentifier:#"cell"];
self.collectionView.backgroundColor= [UIColor blackColor];
self.searchBar.delegate = self;
self.locations = [[NSArray alloc]init];
self.location = [[NSString alloc]init];
[self.view addSubview:self.collectionView];
Here is the drawRect: for my customCell.m
- (void)drawRect:(CGRect)rect
{
UIBezierPath *circularpath = [[UIBezierPath alloc]init];
CGRect Rect = CGRectMake(6, 20, 130, 130);//338
CGPoint mypoint = CGPointMake(Rect.origin.x + (Rect.size.width / 2), Rect.origin.y + (Rect.size.height / 2));
NSLog(#"Circle center point::%f, %f", mypoint.x, mypoint.y);
circularpath = [UIBezierPath bezierPathWithOvalInRect:Rect];
circularpath.lineWidth = 3.0;
[[UIColor whiteColor]setStroke];
UIImage *ori = [UIImage imageNamed:#"drogba.jpg"];
UIImage *image = [[UIImage alloc]initWithCGImage:ori.CGImage scale:1.45 orientation:UIImageOrientationUp];
[[UIColor colorWithPatternImage:image]setFill];
NSLog(#"Circular path:%#", circularpath);
//this works
[circularpath stroke];
//this does not
self.viewForBaselineLayout.layer.cornerRadius = 8.0f;
}
and here is my datasource method
customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
UIImage *image = [[UIImage alloc]init];
if([self.allInfo count]>0){
image = [self getImageForLocation:[self.allInfo objectAtIndex:indexPath.item]];
NSDictionary *dict = [[NSDictionary alloc]initWithDictionary:[self.allInfo objectAtIndex:indexPath.item]];
NSDictionary *city = [dict objectForKey:#"city"];
NSString *name = [[NSString alloc]initWithString:[city objectForKey:#"name"]];
cell.locationLabel.text = name;
cell.imageView.image = [self getImageForSky:dict];
cell.viewForBaselineLayout.backgroundColor = [UIColor colorWithPatternImage:image];
cell.tempLabel.text = [self getTempForLocation:dict];
}
NSLog(#"image description:%f", image.size.height);
count =1;
return cell;
I don't think it is a problem with my data because all I changed is creating Collection view programmatically rather than using storyboard.
EDIT::
I had to alloc init the image view and the labels for the custom cell and then add them as subview. now 3 of my 4 problems are solved. I still cannot get the corner radius to work for me. I want a slightly curved border for my cell
So firstly, because I removed the cell from the storyboard, I had to add the views to the cell's view as subview. Secondly, to get the rounded corners, I had to also set masksToBounds to YES All this had to be done in the initWithFrame: method of the custom cell. Here the code snippet
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.imageView = [[UIImageView alloc]init];
self.imageView.frame = CGRectMake(26, 27, 90, 90);
self.tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(26, 125, 90, 21)];
self.locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(11, 156, 121, 21)];
self.tempLabel.textAlignment = NSTextAlignmentCenter;
self.tempLabel.textColor = [UIColor whiteColor];
self.locationLabel.textAlignment = NSTextAlignmentCenter;
self.locationLabel.textColor = [UIColor whiteColor];
[self.viewForBaselineLayout addSubview:self.imageView];
[self.viewForBaselineLayout addSubview:self.tempLabel];
[self.viewForBaselineLayout addSubview:self.locationLabel];
self.viewForBaselineLayout.layer.masksToBounds = YES;
self.viewForBaselineLayout.layer.cornerRadius = 8.0f;
}
return self;
}
You have init method in Your custom cell
- (id)initWithFrame:(CGRect)frame
change corner radius in this method.
P.S. Works for me.

How to enable zoom for two different UIScrollview in single UIView?

I am displaying two images on single view where both images must have zoom functionality.My page height is around 1000 pixels and images must be display on top and bottom of page. For that I have added two different scroll view and added image views on scroll views.
Now Zoom is working properly for second image .But first image is getting displayed in top left corner of scroll view once zoomed out. It seems there are two images.
Here is the delegate method i am using:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return [[scrollView subviews] objectAtIndex:0];
}
Is there anything i am missing ? or is this not really possible to enable zoom when there are two scroll views for single page.
Here is the code its working for enable zoom when there are two scroll views for single page.
//Create First ImageView
image1 =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 220)];
image1.image = [UIImage imageNamed:#"image1.png"];
image1.contentMode = UIViewContentModeScaleToFill;
//Create and Set Values for Scrollview 1
scroll1 =[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 220)];
scroll1.contentSize = image1.frame.size;
[scroll1 setMinimumZoomScale:1.0];
[scroll1 setMaximumZoomScale:4.0];
scroll1.delegate = self;
scroll1.clipsToBounds = YES;
scroll1.tag =0;
[scroll1 addSubview:image1];
image1.userInteractionEnabled =YES;
scroll1.zoomScale = 1;
[self.view addSubview:scroll1];
Second Scrollview and ImageView
// Create Second ImageView
image2 =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 260)];
image2.image = [UIImage imageNamed:#"image2.png"];
image2.contentMode = UIViewContentModeScaleToFill;
// Create Second ScrollView
scroll2 =[[UIScrollView alloc] initWithFrame:CGRectMake(0, 220, 320, 260)];
scroll2.contentSize = image2.frame.size;
[scroll2 setMinimumZoomScale:1.0];
[scroll2 setMaximumZoomScale:4.0];
scroll2.delegate = self;
scroll2.clipsToBounds = YES;
scroll2.tag =1;
[scroll2 addSubview:image2];
image2.userInteractionEnabled =YES;
scroll2.zoomScale = 1;
[self.view addSubview:scroll2];
This will help you, since it is already working for me.
Delegate method as follow:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
if (scrollView.tag ==0) {
return image1;
}
else return image2;
}

UITableView center a UIImage and UILabel in viewForHeaderInSection

I wanna center vertically and horizontally an image inside of HeaderSection and a label inside of the image. But I don't have a clear idea about how make that. My code is:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImage *image = [UIImage imageNamed:#"bg_title_category"];
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)] autorelease];
UILabel *sectionTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)] autorelease];
UIImageView *sectionHeaderBG;
sectionTitle.text = #"Trial"; //[[tableDataSource objectAtIndex: section] objectForKey: #"Title"];
sectionTitle.textAlignment = NSTextAlignmentCenter;
sectionTitle.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
sectionTitle.textColor = [UIColor whiteColor];
sectionTitle.shadowColor = [UIColor colorWithWhite:0 alpha:0.4];
sectionTitle.shadowOffset = CGSizeMake(1, 1);
sectionTitle.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
sectionHeaderBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width/2, image.size.height)];
sectionHeaderBG.image = [UIImage imageNamed:#"bg_title_category"];
[headerView addSubview:sectionHeaderBG];
[headerView addSubview:sectionTitle];
return headerView;
}
But this code not center anything... Thanks in advance!
The problem is that you're creating headerView with origin (0,0) and it's size the same one as your image; then you're adding all of your views to headerView. When the table view adds this header view it will not be centered, rather it'll placed at (0,0).
What I'd suggest you to do is to create headerView with the same origin (0,0) but with the width of your tableView and the height depening on you. Let's just assume for now the height will be the same as your image plus 10px at the top and bottom just to give it some margin. Then you can add your UIImageView and UILabel inside headerView and center them with respect to it. It'd be something like this:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImage *image = [UIImage imageNamed:#"bg_title_category"];
// Create your headerView with the same width as the tableView and a little taller than the image
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, tableView.bounds.size.width, image.size.height + 20)]; //10px top and 10px bottom. Just for illustration purposes.
// Create the image view
UIImageView *sectionHeaderBG = [[UIImageView alloc] initWithImage:image];
// Now center it and add it to headerView
sectionHeaderBG.center = CGPointMake(headerView.bounds.size.width/2, headerView.bounds.size.height/2);
[headerView addSubview: [sectionHeaderBG autorelease]];
// Now it's turn of the label. Again I suggest using the tableView's width
UILabel *sectionTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
// Now center it. You could even do this when creating it's frame
sectionTitle.center = CGPointMake(headerView.bounds.size.width/2, headerView.bounds.size.height/2);
// do the rest of the configuration for your label...
// and add it to headerView
[headerView addSubview: [sectionTitle autorelease]];
return [headerView autorelease];
}
Hope this helps!

Resources