UITableView separator lines briefly bleed through overlaid UIView - iphone

Further to this question I asked recently, I was able to get the explanation view appearing in front of the tableview successfully, but I now have a problem whereby the tableview's separator lines are briefly visible underneath my explanation view when the tableview first loads. This is in spite of setting the explanation view's background colour, setting it to opaque and bringing it to the front. It's just a brief flash, but it's noticeable and distracting.
Here is my code (_explanationView is a UIView instance variable that I set to nil in the view controller's dealloc method):
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor darkGrayColor];
CGRect frame = CGRectMake(50.0f, 120.0f, 220.0f, 155.0f);
_explanationView = [[UIView alloc] initWithFrame:frame];
_explanationView.userInteractionEnabled = NO;
_explanationView.backgroundColor = [UIColor whiteColor];
_explanationView.opaque = YES;
_explanationView.layer.borderColor = [UIColor darkGrayColor].CGColor;
_explanationView.layer.borderWidth = 1.0f;
_explanationView.layer.cornerRadius = 10.0f;
_explanationView.layer.masksToBounds = YES;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 0.0f, 180.0f, 145.0f)];
label.backgroundColor = [UIColor whiteColor];
label.textColor = [UIColor darkGrayColor];
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont systemFontOfSize:14.0f];
label.text = #"Explanation of this screen...";
[_explanationView addSubview:label];
[self.tableView addSubview:_explanationView];
}
I found that I had to bring the explanation view to the front in the viewDidAppear: method, otherwise the tableview's separator lines are permanently visible underneath.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableView bringSubviewToFront:_explanationView];
}
What can I do to stop the tableview's separator lines from briefly being visible underneath my custom view?

I found this answer which indicates that handling the order of a custom subview in tableView can be done in the layoutSubviews method.

Related

Custom SectionHeaderView in landscape persisting in portrait view after rotation

I'm using a custom sectionheader class with a tableview, and getting some unexpected behavior after rotation. Here's the use case:
Tap on cell in UITableView
View pushed onto stack.
Rotate the view to landscape.
Rotate back to portrait.
Pop the view.
On the iPhone 3G only, a landscape-sized section header now appears stuck somewhere down the middle of the view (in addition to the portrait-sized section header, which appears, as it should, at the top of the tableview). The extraneous header scrolls with the UITableView cells, and switching away from and back to the view (the UITableView is nested within a UITabBarController) doesn't fix the problem.
I can't reproduce this problem on the iPhone 4, or in the Simulator. It seems that, for some reason, a landscape oriented sectionheaderview is being added to the uitableview after popping the second level view, but why would this be? Note that the same problem is reproduced when the default (and not a custom) header is used. I've also checked whether it's a problem with device orientation being returned incorrectly, and that does not appear to be the case.
Here's the init code for the custom SectionHeaderView class, if it's helpful:
-(id)initWithFrame:(CGRect)frame title:(NSString*)title delegate:(id <SectionHeaderViewDelegate>)aDelegate {
self = [super initWithFrame:frame];
if (self != nil) {
float lineHeight = 0.5f;
// check line sizing for retina/non-retina
if (![Utilities hasRetina])
{
lineHeight = 1.0f;
}
// Set up the tap gesture recognizer.
delegate = aDelegate;
// Create and configure the title label.
CGRect titleLabelFrame = self.bounds;
titleLabelFrame.origin.y -= 12.5;
titleLabel = [[UILabel alloc] initWithFrame:titleLabelFrame];
titleLabel.text = title;
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.font = [UIFont fontWithName:#"Georgia" size:15.0];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
[self addSubview:titleLabel];
// add thin white line to top of section header
UIView *topBorder = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.bounds.size.width, lineHeight)];
[topBorder setBackgroundColor:[UIColor whiteColor]];
[self addSubview:topBorder];
[topBorder release];
// Set the colors for the gradient layer.
static NSMutableArray *colors = nil;
if (colors == nil) {
colors = [[NSMutableArray alloc] initWithCapacity:3];
UIColor *color = nil;
color = [UIColor colorWithRed:57.0/255.0 green:56.0/255.0 blue:105.0/255.0 alpha:1.0];
[colors addObject:(id)[color CGColor]];
color = [UIColor colorWithRed:54.0/255.0 green:53.0/255.0 blue:95.0/255.0 alpha:1.0];
[colors addObject:(id)[color CGColor]];
color = [UIColor colorWithRed:57.0/255.0 green:56.0/255.0 blue:105.0/255.0 alpha:1.0];
[colors addObject:(id)[color CGColor]];
}
[(CAGradientLayer *)self.layer setColors:colors];
[(CAGradientLayer *)self.layer setLocations:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.48], [NSNumber numberWithFloat:1.0], nil]];
}
return self;
}
Why would an additional landscape version of the custom SectionHeaderView be added in the portrait view, only on the iPhone 3G?
Thanks in advance for any help.
Still no clue what's causing this problem, but I eventually solved it by adding a check in viewWillAppear to make sure the old header was removed.

How to make a UITableView as transparent one?

In my application, i am presenting an UITableViewController class (Grouped style) to the user for the particular scenario. I want to show the table view's cells only. The background should be transparent so that the user can see the previous page.
I tried with the following codes in viewDidLoad method of the table view controller. But they are not working..
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;
Is it possible to show the cells with a dimmed background?
Thanks in Advance
I use: (in viewDidLoad)
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
Then in cellForRowAtIndexPath:
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor colorWithWhite:1 alpha:.55];
Not sure if this is what you wanted, but it gives you the option to make everything clear. (obviously the alpha would change for your case, but that measures theo pacity of the cells)
Just altering the tableView doesn't help. You have to mess with the cells also. Inside the cellForRowAtIndexPath:(NSIndexPath *)indexPath method, put this before the return statement:
UIView * bgView =[[UIView alloc] initWithFrame:CGRectZero] autorelease];
bgView.backgroundColor = [UIColor clearColor];
cell.backgroundView = bgView;

Some Gap remains between Table Top and Table Header only when table loads and scrolled down to the last row?

Edit: It works fine, But still when the table get Loads Some gap remains between table top and table header abt 30 pxcls(or 1 row) ..?
Please help....
I am scrolling it works fine. All the table rows are scrolling and table header is fixed.
But when I scroll to end of the table it looses its property of fix. I mean is boundry scrolling. I want to freeze it absolutely. At the boundary condition it looses the top position of the table and slides about 20 pxcls.
//Header Format starting
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (tableView.tableHeaderView) { // header was already created... go away
return tableView.tableHeaderView;
}
CGFloat width = 300.0f;
CGRect rectArea = CGRectMake(10.0f, 5.0f, width, 25.0);
tableView.tableHeaderView = [[[UIView alloc] initWithFrame:rectArea] autorelease];
//UIColor *orange = [UIColor colorWithRed:(255.0f/255.0f) green:(228.0f/255.0f) blue:0.0f alpha:1.0f];
[tableView.tableHeaderView setBackgroundColor:[UIColor grayColor]];
rectArea = CGRectMake(02.0f, 1.0f, width, 20.0);
UILabel *lbl = [[UILabel alloc] initWithFrame:rectArea];
lbl.text = NSLocalizedString(#"Bill Total", #"");
lbl.textAlignment = UITextAlignmentLeft;
//lbl.font = [UIFont systemFontOfSize:13.0f];
lbl.font = [UIFont fontWithName:#"Courier New" size:14];
lbl.font=[UIFont italicSystemFontOfSize:14];
lbl.textColor = [UIColor blackColor];
lbl.backgroundColor = [UIColor clearColor];
lbl.numberOfLines = 2.0f;
lbl.lineBreakMode = UILineBreakModeWordWrap;
//[lbl sizeToFit];
[tableView.tableHeaderView addSubview:lbl];
[lbl release];
// self.table.tableHeaderView.layer.cornerRadius = 6.0f;
return table.tableHeaderView;
}
If I understand your question correctly, it sounds like you just need the table not to bounce. If that's the case, all you need to do is set yourTable.bounces = NO; in your viewDidLoad function. Either that or uncheck the "Bounces" option in the NIB if you used one to layout your table.
Here's how to fix the problem you mentioned in your edit... You need to replace the following references to tableView.headerView with references to a new view that's not being passed into the delegate method.
UIView *headerView = [[[UIView alloc] initWithFrame:rectArea] autorelease];
//...
[headerView setBackgroundColor:[UIColor grayColor]];
//...
[headerView addSubview:lbl];
//...
return headerView;
I tried it and it did the trick for me. Hope that helps.

iPhone - Customized UINavigation bar along entire stack

I want to set an image and a label at the center of my UINavigationBar, along all my navigation stack.
What I'm currently doing is adding it to my navigation item titleView.
The "problem" with this approach is that I have to call this method in the viewDidLoad for each view controller I push to my navigation stack.
The other way around is to add the UILable and UIImageView directly to the UINavigationBar, however that why I have to calculate the center myself, and in addition I read that's not the recommended approach.
Any Idea how to get what I want ?
My Code:
CGRect navTitle = controller.navigationController.navigationBar.bounds;
CGFloat aHeight = navTitle.size.height;
UIView* container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 163, aHeight)];
UIImage* statusImg = [UIUtils getStatusImage];
UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,aHeight/2-statusImg.size.height/2, 33., 32.)];
statusImage.autoresizingMask = UIViewAutoresizingNone;
statusImage.image = statusImg;
statusImage.backgroundColor = [UIColor clearColor];
[statusImage setTag:1];
[statusImage setHidden:NO];
initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];
titleLabel.textAlignment = UITextAlignmentLeft;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont boldSystemFontOfSize:20.];
titleLabel.shadowOffset = CGSizeMake(1, -1);
titleLabel.opaque = NO;
titleLabel.backgroundColor = [UIColor clearColor];
[titleLabel setTag:2];
[container addSubview:statusImage];
[container addSubview:titleLabel];
controller.navigationItem.titleView = container;
[statusImage release];
[titleLabel release];
[container release];
Found the a nice way to do it :
Registering yourself as the delegate of UINavigationController will let you receive a callback each time a new controller is about to be pushed.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
Inside that function, getting the viewController and operating on his navigationitem will do the trick.

How to add a custom background to UISearchDisplayController's table view?

I want to add a custom UIImageView to UISearchDisplayController's table view background and set table view's background color to clearColor. Tried a few different approach but couldn't find the right solution. Any idea how to approach this?
Note: I don't want to add to searchDisplayController's searchResultsTableView's view hierarchy, but rather overlay another sibling view below it)
You can set the background image in a similar way you would for your main table, only set it in the searchDisplayControllerDidBeginSearch delegate method. For instance:-
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
[controller.searchResultsTableView setDelegate:self];
UIImageView *anImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"gradientBackground.png"]];
controller.searchResultsTableView.backgroundView = anImage;
[anImage release];
controller.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
controller.searchResultsTableView.backgroundColor = [UIColor clearColor]; }
You can also do this wherever you instantiate your UISearchDisplayController. In my app I was doing this in my UITableView viewDidLoad method and was matching the styles between the two tables:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor blackColor];
self.tableView.backgroundColor = [UIColor grayColor];
self.tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
searchController.searchResultsTableView.separatorColor = self.tableView.separatorColor;
searchController.searchResultsTableView.backgroundColor = self.tableView.backgroundColor;
searchController.searchResultsTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
}