UITableView center a UIImage and UILabel in viewForHeaderInSection - ios

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!

Related

Add footer view UITableView

I have this code to add a footer view to my table view
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f,320.0f,80.0f)];
UILabel *tableFooter = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
tableFooter.textColor = [UIColor blueColor];
tableFooter.backgroundColor = [self.tableView backgroundColor];
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = #"test";
[footerView addSubview:tableFooter];
self.tableView.tableFooterView = footerView;
but then I when I run this I do not see the label at the bottom of my code, I am it to my view, and then set my view as to footer, I see the extra space but not the label?
Not sure why this is happening?
Thanks

unwanted white space "under" tableView subview when tableView scrolled to its content limits

I have a UITableViewCell as a subview in a custom view controller. It works great except that when it scrolls to its top or bottom of its contentSize limit, it "keeps going" and leaves some white space exposed behind it. This is particularly irritating because I do have a UIView covering the entire screen behind the tableView, and that view is set to a non-white color. I also added a subview exactly underlaying the tableview with the same background color, again attempting to block the white. I also set the UIApplication Window background color to a non white color. None of this worked.
I would have thought that even if my TableView bounces around its origin frame, the "exposed" view should match the underlying view rather than be white. How can I fix my tableView so that it retains all its scroll properties but doesn't reveal white when it bounces around at the end of a scroll?
Here is a screen shot of the effect. The white appears the tableViewHeader and below a UISCrollView that occupies the top of the screen. This appears when I scroll the tableView all the way to one extreme. The white space appears at the bottom rather than the top of the tableView if I scroll all the way to the other end.
Here's the relevant code, quite vanilla I think:
#interface JudgeViewController () <UITextFieldDelegate, UITextViewDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate, UITableViewDataSource, UITableViewDelegate, UIViewControllerRestoration, UIScrollViewDelegate>
#property (nonatomic) UITableView *tableView;
#end
functions to set tableViewCells
#pragma mark - tableview appearance and actions
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
StatsViewController *svc = [[StatsViewController alloc] init];
svc.user = self.object.answerUser[indexPath.row];
svc.fromJudge = YES;
[self.navigationController pushViewController:svc animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.object.answerArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell ==nil)
{
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode: UILineBreakModeWordWrap];
[label setMinimumFontSize:SMALL_FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:SMALL_FONT_SIZE]];
[label setTag:1];
// [[label layer] setBorderWidth:2.0f];
[[cell contentView] addSubview:label];
}
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
NSString *text = self.object.answerArray[indexPath.row];
CGSize constraint = CGSizeMake(.8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, 200000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:SMALL_FONT_SIZE] constrainedToSize:constraint];
if(!label)
label = (UILabel *)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, .8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, MAX(size.height, 44.0f))];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(.85*width, label.frame.size.height/2-2*CELL_CONTENT_MARGIN, .12*width, 20);
[button1 setTitle:#"UP" forState:UIControlStateNormal];
button1.titleLabel.font =[UIFont systemFontOfSize:SMALLEST_FONT_SIZE];
[button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(upVoteA:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button1];
[cell.contentView bringSubviewToFront:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.frame = CGRectMake(.85*width, label.frame.size.height/2+2*CELL_CONTENT_MARGIN, .12*width, 20);
[button2 setTitle:#"DOWN" forState:UIControlStateNormal];
button2.titleLabel.font =[UIFont systemFontOfSize:SMALLEST_FONT_SIZE];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(downVoteA:) forControlEvents:UIControlEventTouchUpInside];
CGFloat moduloResult = indexPath.row % 2;
if(moduloResult>0)
{
cell.backgroundColor = [UIColor colorWithRed:1 green:0.647 blue:0 alpha:.6];
}
else
{
cell.backgroundColor = [UIColor colorWithRed:1 green:0.647 blue:0 alpha:.4];
}
cell.opaque = NO;
cell.alpha = 0.2;
[cell.contentView addSubview:button2];
[cell.contentView bringSubviewToFront:button2];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(void)keyboardToJudge
{
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row < [self.object.answerArray count])
{
NSString *text = self.object.answerArray[indexPath.row];
CGSize constraint = CGSizeMake(.8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, 200000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE-2.0f] constrainedToSize:constraint];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN)*2;
}
else
{
return 200.0f;
}
}
functions setting out layout:
-(void)viewDidLoad
{
...among other things setting up top scroll view (top part of view with gray background and orange text)...
if(self.navigationController.navigationBar.frame.size.height>0)
{
self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 5, width, height*SCROLL_VIEW_OFFSET)];
}
else
{
self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, statusBarHeight+5, width, height*SCROLL_VIEW_OFFSET)];
}
self.scroll.backgroundColor = BACKGROUND_COLOR;
self.scroll.contentSize =CGSizeMake(width, .5*height);
self.scroll.delegate = self;
self.scroll.contentInset = UIEdgeInsetsMake(30, 0, 30, 0);
[self.scroll setShowsHorizontalScrollIndicator:NO];
...adding buttons to self.scroll...
[self.view addSubview:self.scroll];
....
self.tableView = [[UITableView alloc] initWithFrame:frame];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _width, _height*.1)];
self.tableView.tableFooterView.backgroundColor = BACKGROUND_COLOR;
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _width, _height*.1)];
self.tableView.tableHeaderView.backgroundColor = BACKGROUND_COLOR;
....tableView hidden state is changed to yes in another function if row count is zero but not usually...
self.tableView.hidden = NO;
[self.view addSubview:self.tableView];
...
}
Finally, I also call :
[self.tableView reloadData];
after reloading data from a webserver and depending on the results either set the tableView to hidden or not (not hidden if there are results to display). That should be every line of code that touches the tableView subview.
Add to your viewDidLoad
[self.tableView setBackgroundColor:BACKGROUND_COLOR];
Set the background color of your tableView and you'll be fine
Change this line:
self.scroll.contentInset = UIEdgeInsetsMake(30, 0, 30, 0);
To this:
self.scroll.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
You are adding a footer to your tableview with that line. The following image is from the IOS dev Library
Set the table view estimate to 0 (Uncheck Automatic) in the size inspector

Making a TableViewCell Section Footer Transparent

I am trying to make a section footer of a UITableViewCell transparent and this is what I am doing right now:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIColor *background = [UIColor clearColor];
UIImageView *imageView = [[UIImageView alloc] initWithImage:(UIImage *)background];
imageView.frame = CGRectMake(10,10,1,30);
return imageView;
}
But I am getting a problem from casting background to a UIImage. Is this possible in this way, is there a better way to cast it or am I simply going about this wrong? I am basically using the footer as a way to create a clear spacer between cells. Need some guidance on this.
This code will add clear footer to your section, where you can set the desired height to whatever you need. Below it has been set to 30, to match your code in the question.
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
footerView.backgroundColor = [UIColor clearColor];
return footerView;
}
Instead of type casting the colour into the UIImage, use the backgroundColor property :
Try this :
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,1,30)];
imageView.backgroundColor = [UIColor clearColor];

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.

Resources