I created a UITableView programmatically and now I want to add a fixed header to it.
As I the below code the header also scrolls with my tableview.
UILabel *label = [[[UILabel alloc]
initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
label.backgroundColor = [UIColor darkGrayColor];
label.font = [UIFont boldSystemFontOfSize:15];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.8];
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = #"my header";
myTable.tableHeaderView = label;
Is there a function which disables header scrolling?
Thank you
No, you need to either change the view to be a section header (where it will remain at the top of the tableview while that section is visible), or you need to make the header view a sibling to the tableview in the tableview's superview -- you can create a UIView just for the purpose of holding the header view and the table view.
Related
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
messageLabel.text = CONNECTED;
messageLabel.textColor = [UIColor blackColor];
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = NSTextAlignmentCenter;
[messageLabel sizeToFit];
self.TableView.backgroundView = messageLabel;
The last row of this code set backgroundView to my UILabel but it stays centered and if I scroll the table view, the message position stays fixed. How to solve this problem?
I want that the UILabel to follow the scroll event.
Just add your UILabel as a subview to your tableView and send it back.
[self.TableView addSubview:messageLabel];
[self.TableView sendSubviewToBack:messageLabel];
Now when you scroll the tableView, this view will also get scrolled.
Other code enhancements (Not related to the question)
You can initialize your label like this
UILabel *messageLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
You can see the following tutorials of TableView:
swift: https://www.weheartswift.com/how-to-make-a-simple-table-view-with-ios-8-and-swift/
objective-c: http://www.makemegeek.com/uitableview-example-ios/
Basically,you should add your label inside a cell and it will work.
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
In my UINavigationBar I have a multi line title, I am using the following the code to setup:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 50)];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 2;
label.font = [UIFont boldSystemFontOfSize: 15.0f];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = #"Headloss due to Friction (Hazen Williams Eq.)";
self.navigationItem.titleView = label;
Since this is on a sub-view it has a "Back" button, added by the UINavigationController. So I figured that by adding the label.textAlignment = NSTextAlignmentCenter; my title would be centered, but it's actually centering my title from the end of my "Back" button to the end of my screen.
How can I override this so that my title is actually centered based on my screen dimension, (basically, the way it would be by default should the back button not be present).
Good question that's pretty tough. If I find a more elegant solution I'll edit my answer but this is what I came up with:
UIView *spaceView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithCustomView:spaceView];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObject:space];
And just adjust the width of the UIView for how far you want your titleLabel to move.
I have a problem with my navigation bar. I'm setting a custom font, and the centering is not right, the back button it's moving everything to the right.
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 100)];
navLabel.backgroundColor = [UIColor clearColor];
navLabel.text = dataFromOtherView.text;
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0];
navLabel.shadowOffset = CGSizeMake(0, 0);
navLabel.font = [UIFont fontWithName:#"Trebuchet MS" size:22.0];
navLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = navLabel;
It looks like the label is so large that there is not enough 'open/flexible space' between the left side of the label and the right side of the back button. Try reducing the width of UILabel when you create it.
A UITableViewCell comes "pre-built" with a UILabel as its one and only subview after you've init'ed it. I'd really like to change the background color of said label, but no matter what I do the color does not change. The code in question:
UILabel* label = (UILabel*)[cell.contentView.subviews objectAtIndex:0];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
Your code snippet works fine for me, but it must be done after the cell has been added to the table and shown, I believe. If called from the initWithFrame:reuseIdentifier:, you'll get an exception, as the UILabel subview has not yet been created.
Probably the best solution is to add your own UILabel, configured to your standards, rather than relying on this (very rickety) path to the built-in one.
This doesn't work because the UITableViewCell sets its label backgroundColors in the layoutSubviews method.
If you want to change the color of the built-in textLabel or detailTextLabel, subclass the UITableViewCell and override layoutSubviews. Call the super implementation, THEN change the backgroundColor property to what you want.
- (void) layoutSubviews
{
[super layoutSubviews];
self.textLabel.backgroundColor = [UIColor redColor];
}
Add your own label to the contentView when you are allocating the cell, rather than relying on the extraction of the built in one. Then you can control all values:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
[cell.contentView addSubview:label];
for (UIView *views in views.subviews)
{
UILabel* temp = (UILabel*)[views.subviews objectAtIndex:0];
temp.textColor = [UIColor whiteColor];
temp.shadowColor = [UIColor blackColor];
temp.shadowOffset = CGSizeMake(0.0f, -1.0f);
}