I have to build a custom tableViewHeader in which at least two labels should be displayed, viz. a projectName and projectDescription. The contents of these two labels vary (i.e. in terms of string-length).
I managed to get a working version for the default device orientation (portrait) but if the user rotates the device to landscape the width of my custom headerView is not adjusted, and the user will see unused white space to the right.
The following code fragment is being used:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CGFloat wd = tableView.bounds.size.width;
CGFloat ht = tableView.bounds.size.height;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0., 0., wd, ht)];
headerView.contentMode = UIViewContentModeScaleToFill;
// Add project name/description as labels to the headerView.
UILabel *projName = [[UILabel alloc] initWithFrame:CGRectMake(5., 5., wd, 20)];
UILabel *projDesc = [[UILabel alloc] initWithFrame:CGRectMake(5., 25., wd, 20)];
projName.text = #"Project: this project is about an interesting ..";
projDesc.text = #"Description: a very long description should be more readable when your device is in landscape mode!";
[headerView addSubview:projName];
[headerView addSubview:projDesc];
return headerView;
}
I noticed that the tableView:viewForHeaderInSection: will only be called once after viewDidLoad, but not after a device orientation change.
Should I implement the willRotateToInterfaceOrientation: method with something like:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[self.tableView reloadSections:nil withRowAnimation:UITableViewRowAnimationNone];
}
I cannot figure out how to get the reloadSections: to work, forcing a recalculation of my customView.
If you use autoresizingMask the header adjusts correctly on rotation. No need to override anything else. I would set the automasks at the end of the code just before returning the view:
[headerView addSubview:projName];
[headerView addSubview:projDesc];
projName.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
projDesc.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
return headerView;
Related
I want to do a large tableView header like in Health app in iOS 11. I added a screenshot to show this element in red text. How to do large a tableView header?
If I understand your question correctly (and unfortunately the screenshot is gone), you can do that using the prefersLargeTitles property. This works on iOS 11 and higher only.
For instance:
if (#available(iOS 11.0, *)) {
self.navigationController.navigationBar.prefersLargeTitles = YES;
self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
} else {
// Fallback on earlier versions
}
You can call this code in viewDidLoad or viewWillAppear of your UITableViewController.
Just set the frame property of the tableHeaderView.
No recorded Data is table view header. you can customize the tableview header with viewForHeaderInSection method of tableview. Your question is already answered in this link. Changing Font Size For UITableView Section Headers
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *TESTLabel = [[UILabel alloc] init];
TESTLabel .frame = CGRectMake(20, 8, 320, 40);
TESTLabel .font = [UIFont boldSystemFontOfSize:18];
TESTLabel .text = [self tableView:tableView titleForHeaderInSection:section];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview: TESTLabel];
return headerView;
}
TRY THIS
My tableview cells are created entirely programmatically (I'm trying to learn to build an app from scratch without using storyboards) and the width of the cells is getting messed up.
Here is a screen shot http://imgur.com/ki6txqg of what the cell looks like in an iPhone 6 Plus. I'm trying to set the cell so that the UIView in the cell(self.view) gets adjusted automatically so that it fills the entire screen. I'm not sure why the width is staying static. Any advice would be greatly appreciated.
-(instancetype)initWithTweet:(PCRTweet *)tweet reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super init];
if (self) {
_tweet = tweet;
reuse = reuseIdentifier;
CGSize cellSize = self.contentView.frame.size;
CGRect backgroundView = CGRectMake(10.f, 5.f, (cellSize.width-20.f), (cellSize.height + 90.f));
self.view = [[UIView alloc] initWithFrame:backgroundView];
self.view.backgroundColor = [UIColor whiteColor];
self.view.layer.cornerRadius = 8;
self.view.layer.masksToBounds = YES;
self.view.layer.borderWidth = 1.0f;
self.view.layer.borderColor = background_color_gray.CGColor;
self.view.layer.masksToBounds = NO;
[self.contentView addSubview:self.view];
CGRect picView = CGRectMake(0.f, 0.f, 65.f, 65.f);
self.contentView.backgroundColor = background_color_gray;
}
return self;
}
Please read the points on following checklist to ensure you doing it all right:
-[ ] Have you checked that your contentView is dynamically changing?
-[ ] Have you tried putting constraints programatically?
-[ ] Try using constraints on the largest view : will auto adjust the relative views
Apart from it, you can auto-resizing for your frame.
You try this two UITableView Delegate method in table view class
For Dynamic Height
#pragma mark - UITableView Delegates
-(CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath {
return 44.0;
}
-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath {
return UITableViewAutomaticDimension;
}
For Width (Get view controller width and take to backgroundView)
CGSize viewWidth = self.contentView.superview.superview.superview.superview.frame.size;
// self.contentView.superview -> return UITableViewCell
// self.contentView.superview.superview -> return UITableViewWrapperView
// self.contentView.superview.superview.superview -> return UITableView
// self.contentView.superview.superview.superview.superview -> return View Controller
CGRect backgroundView = CGRectMake(10.f, 5.f, (viewWidth.width-20.f), (cellSize.height + 90.f));
Try to use auto-resizing for your view.
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
iPhone 6 simulator:
iPhone5 hardware:
Channel Header:
- (id)initWithFrame:(CGRect)frame title:(NSString *)title {
self = [super initWithFrame:frame];
UILabel *l = [[UILabel alloc] initWithFrame:frame];
l.textAlignment = NSTextAlignmentCenter;
l.backgroundColor = BGC;
l.text = title;
[self addSubview:l];
return self;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
ChannelHeader *h = [[ChannelHeader alloc] initWithFrame:CGRectMake(0, 0, self.tv.frame.size.width, 44) title:[self titles][section]];
return h;
}
Why is my tableview header's textfield showing up off center?
Tableview frame in iPhone6 sim:
<UITableView: 0x7fce2c821a00; frame = (0 0; 414 736)
EDIT:
so I changed my simulated nib size from "iPhone 6" to "inferred", and the view boundaries grew. I stretched my UITableView to fit the bounds and now the text is even more off-centered. So somehow it's getting the wrong values for its frame..
Well, your frame for iPhone 6 is wrong.
iPhone 6 screen size is 375 x 667 points.
iPhone 6 plus screen size is 414 x 736 points.
Therefore, if you are running the application on iPhone 6 simulator and tableView frame is giving you iPhone 6 plus boundaries, then that is the error. Your header and UILabel is being rendered correctly according to the given frames.
So if you run your application in iPhone 6 plus simulator, you will get correct results.
More info on frames.
Solution:
If your setting your tableView through nib, and if you are using AutoLayout then you need to apply constraints accordingly.
If you have disabled AutoLayout then apply proper resizing masks to your tableView, i.e. Flexible Height & Flexible Width
Also, it would be a good practice, if you take values of the tableView frame provided by the delegate method instead of referring to the tableView property.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
ChannelHeader *h = [[ChannelHeader alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 44) title:[self titles][section]];
return h;
}
Note: This is just a piece of good practice and it doesn't effect the logic or the answer.
Whilst I await your response on the above comment, I'm quite certain I know why you are having an issue.
If you want to create UITableViewCell(s) which have centred labels there are really only two correct ways.
Either:
Subclass UITableViewCell and create a custom subclass which has an indigenously centred label.
Set the NSTextAlignment of the regular UITableViewCell textLabel control to centre in cellForRowAtIndexPath like so: cell.textLabel!.textAlignment = .Center.
To change the header text alignment, you should do something like this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel * sectionHeader = [[UILabel alloc] initWithFrame:CGRectZero];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.textAlignment = NSTextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:10];
sectionHeader.textColor = [UIColor whiteColor];
return sectionHeader;
}
I tried to place a view on top of my table view. It seems to only cover the cells, but not the section headers. I want it to cover both cells and section headers.
In my UITableViewController subclass:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView* veilView = [[UIView alloc] initWithFrame:CGRectMake(25.0f, 100.0f, 100.0f, 150.0f)];
veilView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5f];
[self.view addSubview:veilView];
}
The section headers are constructed in a typical way:
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CustomTableSectionHeaderView* header = [[CustomTableSectionHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.frame.size.width, [CustomTableSectionHeaderView height])];
header.text = [self.displayAlphabet objectAtIndex:section];
return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return [CustomTableSectionHeaderView height];
}
Try changing the zPosition of the view after you create it.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView* veilView = [[UIView alloc] initWithFrame:CGRectMake(25.0f, 100.0f, 100.0f, 150.0f)];
veilView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5f];
[self.view addSubview:veilView];
[self.addressTextView.layer setZPosition:1000]
}
Higher numbers are on top of other views, and the scale is arbitrary, so you can go as high as you like. The default value is 0, but section headers will have a higher value than that as they float over the cells they refer to when you're scrolling.
It was caused by a timing issue. I'm actually loading the data for the table from my server. I placed the overlay view before the server returned the data. When the server did return the data, I reload the table view, causing the section headers to be created on top of the overlay view.
This will solve the issue:
// When server returns table data:
[self.tableView reloadTable];
[self.veilView.superview bringSubviewToFront:self.veilView];
I'd like to call the following method from my view controller class:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
However for the life of me I can't figure out how to do it. I tried:
UIView *view = [tableView viewForHeaderInSection:section];
UIView *view = [self viewForHeaderInSection:section];
UIView *view = [self tableView:viewForHeaderInSection:section];
All give me errors. It's that extra tableView: bit on the beginning. Can anyone give some advice or at least explain what that tableView:(UITableView *)tableView means?
Thanks!
Steve
I don't know why you would want to call it, but if it is implemented in the same object that you're calling it from then you can use self:
UIView* view = [self tableView:tableView viewForHeaderInSection:section];
otherwise, you can get the delegate from the tableView and call the delegate:
id<UITableViewDelegate> theDelegate = tableView.delegate;
UIView* view = [theDelegate tableView:tableView viewForHeaderInSection:section];
Why would you want to call it? This is one of the UITableViewDelegate methods that are normally called automatically when the table is being constructed by the tableView. The tableView class object fills in the parameters that it needs when it makes the call to this method. The view controller only needs to provide the right delegate methods, customized by you, so it can set it up properly.
Did you customize the code, as in this example, in your delegate class?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:20];
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);
// If you want to align the header text as centered
// headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);
headerLabel.text = <Put here whatever you want to display> // i.e. array element
[customView addSubview:headerLabel];
return customView;
}
I'm not near my Mac, or I would give you one of my own examples. But this is the general way this method is used.
By the way, you can see that the parameter tableView is not referenced in the sample code above. If you really want to call this method, use nil. UITableViewDelegate protocol allows the controller to be delegate for more than one tableView. If this happens, the method should test to see which tableView is reference, so that specialized behavior can be accommodated for each tableView.
Additional info:
If you just want to see what the height of your tableView's header is, you can evaluate its sectionHeaderHeight property. There are other properties like this, such as sectionFooterHeight and rowHeight.
You should know that delegate methods are there to help the tableView, using your customization. So the delegate method tableView:heightForHeaderInSection: is actually for you to customize the header height. Your delegate methods tells the tableView what the height is. It isn't a way to examine a property of the tableView.
Apples documentation says that if you customize by using tableView:heightForHeaderInSection: then the tableView sectionHeaderHeight is not valid. You would expect this, because that property refers to the height of all of the section headers.
Using the sectionHeaderHeight property, which you can write to in this case, you can set all of the headers to the same height.
Here is some sample code from something I'm working on now. I've added an NSLog statement for you.
resultsTableVC = [[[ResultsTableVC alloc] initWithController:self] retain];
self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height-120) style:UITableViewStyleGrouped] retain];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.tableView.autoresizesSubviews = YES;
self.tableView.layer.cornerRadius = 10.0f;
self.tableView.delegate = resultsTableVC;
self.tableView.dataSource = resultsTableVC;
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.separatorColor = [UIColor defaultResultTableBackgroundColor];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[self.view addSubview:self.tableView];
NSLog(#"header: %f, row: %f", tableView.sectionHeaderHeight, tableView.rowHeight);
(Someone will probably point out that I don't need some of those retains. I'm still working on that.)
This tells me that the standard section height is 100 and the standard row height is 44.0. I have a pointer to my tableView, a property that I can use through this class.
Now if you are setting the header height using tableView:heightForHeaderInSection: then you should have the height already calculated in your program. I don't think you can query for the height of a particular section (or row) once you set it.
Does this help?