Adding subviews to tableHeaderView with UISearchBar in iOS7 misbehaves - ios

How can one add two simple UILabels to a TableHeaderView and retain the default searchbar behavior of the search display controller in iOS7?
Or, visually:
Why does this code:
UIView *tableHeadView = self.tableView.tableHeaderView;
UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 36, 320, 30)];
[tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
tableHeaderLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
tableHeaderLabel.text = #"Questions"
UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 320, 30)];
[tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter];
tableHeaderPrononce.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
tableHeaderPrononce.text = #"test test test test";
[tableHeadView addSubview:tableHeaderLabel];
[tableHeadView addSubview:tableHeaderPrononce];
added to a UITableViewController viewDidLoad event (which contains a UISearchDisplayerController)
gives this lovely result in iOS6:
and this horrible terrible result in iOS7:
The behavior:
During normal mode the UILabels I added are not shown. while search is active the UILabels suddenly appear ON TOP OF the table cells and does not scroll away
In addition I am getting crashes while searching in iOS 7 which never occurred on iOS6. Probably not related to this piece of code but nevertheless I should mentioned that.
I tried all I could find on SO about fixing this issue but always something else breaks or disappears (mainly the UISearchBar).
Help

Look dude, before you modify your code or get frustrated with the behavior of the iOS7, it is advised to go through the UI Transition Guide given by apple.
After reading this you will get to know, why the grouped style tableView is looking annoying is not at all annoying from ios7 point of view. Each group extends the full width of the screen.
Moreover the UISearchBar behavior can be controlled if you read through Search Bar and Scope Bar
I hope that helps. Should you need all the code, please let us know. we can provide sample.
Here is Sample Code as Required Adding UISearchBar to View rather than TableViewHeader
Adding UISearchBar to self.view
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f, 320, 44.0f)];
searchBar.delegate = self;
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchBar.searchBarStyle = UISearchBarStyleMinimal;
[self.view addSubview:searchBar];
Adding UITableView to self.view
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:tableView];
Creating and Adding UITableView.tableHeaderView
UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 15)];
[tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
tableHeaderLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
tableHeaderLabel.text = #"Questions";
tableHeaderLabel.alpha = 0.9;
UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 320, 15)];
[tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter];
tableHeaderPrononce.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
tableHeaderPrononce.text = #"test test test test";
tableHeaderPrononce.alpha = 0.7;
UIView *aHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
[aHeaderView setBackgroundColor:[UIColor clearColor]];
[aHeaderView addSubview:tableHeaderLabel];
[aHeaderView addSubview:tableHeaderPrononce];
tableView.tableHeaderView = aHeaderView;
Here are the results:
SearchDisplayController
// Create search controller
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
searchController.delegate = self;

You can do this simply by UITableViewDelegate method shown below:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
sectionHeaderView.backgroundColor = [UIColor grayColor];
UILabel *headerLabel = [[UILabel alloc] init];
[sectionHeaderView addSubview:headerLabel];
return sectionHeaderView;
}

Related

iOS tableviewheader subview not shown

I use a UITableView and I want to setup a text at the beginning.
My code is:
UIView *tableHeaderView = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, self.tableView.frame.size.width, 20.0)];
tableHeaderView.backgroundColor = [UIColor groupTableViewBackgroundColor];
UILabel *tableHeaderLabel = [[UILabel alloc]initWithFrame:CGRectMake(15.0,0,tableHeaderView.frame.size.width-15.0,tableHeaderView.frame.size.height)];
tableHeaderLabel.text = #"Countries";
if([UIFont respondsToSelector:#selector(preferredFontForTextStyle:)])
tableHeaderLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
else
tableHeaderLabel.font = [UIFont boldSystemFontOfSize:14.0];
[tableHeaderView addSubview:tableHeaderLabel];
self.tableView.tableHeaderView = tableHeaderView;
[tableHeaderView bringSubviewToFront:tableHeaderLabel];
[tableHeaderLabel release];
[tableHeaderView release];
The problem is that the text isn't shown.
If I suppress the backgroundColor of the main header view, or if I replace it with a transparent one as if the Label awas under the main header view.
So I added the line:
[tableHeaderView bringSubviewToFront:tableHeaderLabel];
but this doesn't fix the issue.
I can't use the Label directly as the tableViewHeaderView directly becaus I want to left space at the left of the text.
Anybody have an idea to help me?
Thanks.
The only problem you have here is a typo in the first line, initialized an UIView wrongly as an UILabel. The compiler will not compliant as UILabel is a subclass of UIView.
I ran the following code and it worked as expected: (The code is for ARC, if you are not using it, don't forget to do the release!)
- (void)viewDidLoad {
[super viewDidLoad];
UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0,
self.tableView.frame.size.width,
20.0)];
tableHeaderView.backgroundColor = [UIColor groupTableViewBackgroundColor];
UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 0,
tableHeaderView.frame.size.width - 15.0,
tableHeaderView.frame.size.height)];
tableHeaderLabel.text = #"Countries";
tableHeaderView.backgroundColor = [UIColor groupTableViewBackgroundColor];
if([UIFont respondsToSelector:#selector(preferredFontForTextStyle:)]) {
tableHeaderLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
} else {
tableHeaderLabel.font = [UIFont boldSystemFontOfSize:14.0];
}
[tableHeaderView addSubview:tableHeaderLabel];
self.tableView.tableHeaderView = tableHeaderView;
}

How can we insert multiple titles on UInavigationBar in ios

Hi i am beginner in iOS and in my project i need to insert "titles" at "left side center" and "right side center" on UInavigationBar as like below image please help me how should i do below my requirement
See below code and try :
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = [[UIScreen mainScreen] bounds];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, 44)];
lbl1.backgroundColor = [UIColor redColor];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.text = #"Dashboard";
[self.navigationController.navigationBar addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, 44)];
lbl2.backgroundColor = [UIColor redColor];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[self.navigationController.navigationBar addSubview:lbl2];
}
You can customize the titleView of navigation bar. Read the official documentation here.
As per your question, I tried the below working code:-
-(UIView *)getTitleView
{
CGRect frame = [[UIScreen mainScreen] bounds];
UIView *viewTitle = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, self.navigationController.navigationBar.frame.size.height)];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, self.navigationController.navigationBar.frame.size.height)];
lbl1.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
lbl1.textAlignment = NSTextAlignmentCenter;
[lbl1 setTextColor:[UIColor whiteColor]];
lbl1.text = #"Dashboard";
[viewTitle addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, viewTitle.frame.size.height)];
lbl2.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
[lbl2 setTextColor:[UIColor whiteColor]];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[viewTitle addSubview:lbl2];
return viewTitle;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.titleView = [self getTitleView];
}
I divide the code in separate method to follow modular approach. For any part of code, which is of separate task, write that piece of code in different method. This will lead to more readability.

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

UICollection view hiding UINavigationBar

Basically I am trying to set the background color of my UINavigationBar but it doesn't show anything whatsoever.
Please, how can I fix this ?! Thanks a Million !!
This is my code in viewDidLoad:
[super viewDidLoad];
self.filteredContentList = [[NSMutableArray alloc] init];
// Do any additional setup after loading the view.
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(self.collectionView.frame), 30)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.placeholder = #"Search...";
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView addSubview:self.searchBar];
[self.view addSubview:self.searchBar];
[self.collectionView setContentOffset:CGPointMake(0, 44)];
[self.collectionView setContentInset:UIEdgeInsetsMake(80, 0, 0, 0)];
self.navigationController.navigationBar.backgroundColor = [UIColor greenColor];
This works fine with iOS 7, and you adapt to your code above:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[navBar setBarTintColor:[UIColor greenColor]];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(navBar.frame), 30)];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchBar.placeholder = #"Search...";
[self.view addSubview:searchBar];
[self.view addSubview:navBar];

Searchbar in tableView and reload data

I can not search because my search bar is a header of tableView and then I reload tableView I have not search results.
I know there is a lot of ways to solve it but what is more wisely?
My code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectionIndex
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 270, kRowHeight)];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 270, kRowHeight)];
self.searchBar.delegate = self;
view.opaque = NO;
view.backgroundColor = [UIColor clearColor];
self.searchBar.opaque = NO;
self.searchBar.translucent = NO;
self.searchBar.backgroundColor = [UIColor clearColor];
[view addSubview: self.searchBar];
self.searchBar.barStyle = UISearchBarStyleDefault;
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin ;
self.searchBar.barTintColor = [UIColor clearColor];
return view;
}
And I want that then I scroll tableView my searchBar will disappear in top and then I scroll up it will appear. Is any simple to do it?
You need to use tableHeaderView and not section header. Just put in your viewDidLoad
UISearchBar* searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 270, kRowHeight)];
self.tableView.tableHeaderView = searchBar;

Resources