Init UIViewController code:
self.view.backgroundColor= [UIColor whiteColor];
CGSize boundsSize = self.view.bounds.size;
CGRect rectTableViewFrame = CGRectMake(0, 0, boundsSize.width, boundsSize.height - 64);
UITableView* contentTableView = [[UITableView alloc] initWithFrame:rectTableViewFrame];
contentTableView.backgroundColor = [UIColor clearColor];
contentTableView.separatorColor = [UIColor clearColor];
contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Next add in UITableView custom view (header):
CustomView* customView = [[CustomView alloc] init];
CGRect customViewFrame = customView.frame;
customViewFrame.origin.y = - customView.size.height;
customView.frame = customViewFrame;
contentTableView.contentOffset = CGPointMake(0, customViewFrame.origin.y);
contentTableView.contentInset = UIEdgeInsetsMake(customViewFrame.frame.size.height, 0, 0, 0);
[contentTableView addSubview: customView];
Problem: When scroll contentTableView the top customView there is a gray line... How delete this?
I don't use methods:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
But if use simple view with white background color there is no line.
you can try to set no-line style like:
contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
and add a gray line at the bottom of you UITableViewCell.
Related
I want to implement the drop shadow and rounded corner in UITableViewCell.
- (void)tableView:(UITableView *)tableView willDisplayCell:(MembershipCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.contentView.backgroundColor = [UIColor clearColor];
CGRect rect = cell.frame;
UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(8,8,rect.size.width-8,rect.size.height)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(0,0);
whiteRoundedCornerView.layer.shadowOpacity = 0.75;
[cell.contentView addSubview:whiteRoundedCornerView];
[cell.contentView sendSubviewToBack:whiteRoundedCornerView];
}
I am using the above code but it shows the shadow in the top and left side of the cell.
That is because you dont have space on right and bottom, you need to have some space at right and bottom as well for shadow to show up. Also, you can add shadowRadius to the layer for controlling the radius of the shadow. Try following
- (void)tableView:(UITableView *)tableView willDisplayCell:(MembershipCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.contentView.backgroundColor = [UIColor clearColor];
CGRect rect = cell.frame;
UIView *whiteRoundedCornerView;
if (![cell.contentView viewWithTag:SOME_TAG_VALUE]) {
whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(8,8,rect.size.width-16,rect.size.height-16)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(0,0);
whiteRoundedCornerView.layer.shadowOpacity = 0.75;
whiteRoundedCornerView.layer.shadowRadius = 1.0;
whiteRoundedCornerView.tag = SOME_TAG_VALUE;
[cell.contentView addSubview:whiteRoundedCornerView];
}
[cell.contentView sendSubviewToBack:whiteRoundedCornerView];
}
how to create section border in tableview and i want to represent the ui as per the attached image ? i displayed spaces between sections but cannot set the border line for section.
For this you need to add and import
#import <QuartzCore/QuartzCore.h>
After that follow below steps
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *section = [[UIView alloc]init];
section.frame = CGRectMake(0, 0, tableview.frame.size.width, height);
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:#"imageName"]];
imageView.frame = CGRectMake(0, 0, YOUR_WIDTH, YOUR_HEIGHT); //set your frame
[section addSubview:imageView];
labelSection = [[UILabel alloc]init];
labelSection.textAlignment = NSTextAlignmentLeft;
labelSection.frame = CGRectMake(10, 5, tableview.frame.size.width, 20);
[labelSection setBackgroundColor:[UIColor clearColor]];
[labelSection setFont:[UIFont boldSystemFontOfSize:15]];
NSString *name = #"section title";
labelSection.text = name;
[labelSection setTextColor:[UIColor blackColor]];
[section addSubview:labelSection];
section.layer.borderWidth = 2.0f;
section.layer.cornerRadius = 1.0f;
section.layer.masksToBounds = YES;
section.layer.borderColor=[UIColor lightGrayColor].CGColor;
return section;
}
If you want to set row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44; //It default size.If you want to change to other size you can change.
//OR
// return 90; //Set to your needed size
}
I have a UITableView with grouped style - and I have a custom header view:
+ (UIView *) viewForHeaderWithText: (NSString*) title
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.frame = CGRectMake(10.0f, 0.0f, 300.0f, 20.0f);
label.font = [UIFont systemFontOfSize:12.0f];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft;
label.text = title;
if (!IS_OS_7_OR_LATER) {
label.textColor = [UIColor lightGrayColor];
}
[viewHeader addSubview:label];
return viewHeader;
}
}
So the section header width frame is fixed.
but For different orientation and way of presenting the UITableViewController the cell width (iOS6) is changed - as u can see on the screenshots. But I need to make offset for the section title and cell equal.
Now I have:
What I need:
I tried to change the cell width - but not solve this.. Any help?
You need to impelement this below uitableView delegate method:-
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
I have a UITableView that I have given a headerView that has a green background. I have also given the UITableView that same green background color so that when the tableView is pulled down it feels like a seemless color above the tableView. Works perfectly.
The problem is my tableView only has 4 rows, and below that it shows the green color again.
How can I show white below the rows instead so that I only see the green background color when pulling down the tableView?
You can add an extra top view, same as in this answer to UIRefreshControl Background Color:
CGRect frame = self.tableView.bounds;
frame.origin.y = -frame.size.height;
UIView *bgView = [[UIView alloc] initWithFrame:frame];
bgView.backgroundColor = [UIColor greenColor];
[self.tableView insertSubview:bgView atIndex:0];
You could add a fifth cell with no content, and make it, say 400 points high, and limit the size of the table's contentView so that you can't scroll to the bottom of that cell.
-(void)viewWillLayoutSubviews {
self.tableView.contentSize = CGSizeMake(self.tableView.frame.size.width, 500);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat h = (indexPath.row == 4)? 400:44;
return h;
}
Putting the contentSize setting in viewWillLayoutSubviews ensures that it will be reset to that value if you rotate your device.
as I said in these comments, in this way the headerView will be under the navigationBar and if you pull down the tableview you will see the headerView green. So you can set white color for tableView:
(i tested it and work)
-(void) viewDidLoad {
self.tableView.backgroundColor = [UIColor whiteColor];
headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor greenColor];
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, 320, 120)];
lab.backgroundColor = [UIColor greenColor];
[headerView addSubview:lab];
self.tableView.tableHeaderView = headerView;
}
#pragma mark - Table view data source
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 80;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return headerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
Same as the accepted answer - in Swift 5:
var frame : CGRect = self.tableView.bounds
frame.origin.y = -frame.size.height
let bgView : UIView = UIView.init(frame: frame)
bgView.backgroundColor = UIColor.green
self.tableView.insertSubview(bgView, at: 0)
Also add the following code (right after the above code) to make sure it works for other sized devices:
bgView.translatesAutoresizingMaskIntoConstraints = true
bgView.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth]
I want to put a custom view inside my uitableviewheader, this custom view has an UILabel and a UITextView, the label have a fixed size while uitextview need to change depending the content of uitextview.
For doing it I use this code:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 100;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,100)];
headerView.backgroundColor = [UIColor blueColor];
NSString *userString = [user username];
UILabel *userLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
[userLabel setTextAlignment:NSTextAlignmentCenter];
[userLabel setText:userString];
userLabel.backgroundColor = [UIColor redColor];
[headerView addSubview:userLabel];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, 320, 60)];
NSString *postString = [currentPost objectForKey:#"postTextKey"];
textView.text = postString;
[headerView addSubview:textView];
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
frame = headerView.frame;
frame.size.height = textView.frame.size.height+40;
headerView.frame = frame;
self.tableView.tableHeaderView.frame = headerView.frame;
return headerView;
}
the problem is that if the headerview have the correct height the tableviewheader is wrong. Where is the mistake?
1) tableHeaderView is different from section header view (headerViewForSection:)
2) If you want to change the height you specify it in the - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section method and then call the reloadData method if the height changes.
Note: you don't want to do that too often.