How to use a SearchController.searchBar as a tableview.sectionHeader - ios

question one :
when a SearchController.searchBar as a tableview.tableHeaderView, and tableview have backgroundColor
why it has a strange color: image
question two :
How to use a SearchController.searchBar as a tableview.sectionHeader
when i try do it, my tableView will be so crazy image
code snippet :
- (UISearchController *)searchController{
if (!_searchController) {
_searchController = [[UISearchController alloc] initWithSearchResultsController: self.resultVC];
_searchController.searchResultsUpdater = self;
_searchController.delegate = self;
self.definesPresentationContext = YES;
// 是否添加半透明覆盖
_searchController.dimsBackgroundDuringPresentation = YES;
// 是否隐藏导航栏
_searchController.hidesNavigationBarDuringPresentation = YES;
// 可以通过此种方式修改searchBar的背景颜色
_searchController.searchBar.barTintColor = GL_NAVBAR_COLOR;
UIImageView *barImageView = [[[_searchController.searchBar.subviews firstObject] subviews] firstObject];
barImageView.layer.borderColor = GL_NAVBAR_COLOR.CGColor;
barImageView.layer.borderWidth = 1;
// 可以通过此种方式可以拿到搜索框,修改搜索框的样式
UITextField *searchField = [[[_searchController.searchBar.subviews firstObject] subviews] lastObject];
searchField.backgroundColor = [UIColor yellowColor];
searchField.placeholder = #"请输入搜索内容";
}
return _searchController;
}
- (void)configureUI{
self.tableView = [[UITableView alloc] initWithFrame: self.view.bounds style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview: self.tableView];
self.tableView.backgroundColor = backgroundColor;
self.searchController.searchBar.frame = CGRectMake(0, 0, ScreenWidth, 44);
#if tableHeaderViewSearchBar
self.tableView.tableHeaderView = self.searchController.searchBar;
#endif
}
#if !tableHeaderViewSearchBar
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return self.searchController.searchBar;
}
#endif

You can place the UISearchBar of UISearchController in the navigation bar so that it remains fixed at the top.
So, just add below code at the end of configureUI method and remove last 2 lines of code inside the same method.
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.navigationItem.titleView = self.searchController.searchBar;
self.definesPresentationContext = YES;

I have a provisional idea:
- (void)viewDidLoad {
[super viewDidLoad];
[self.searchController.searchBar sizeToFit];
self.tableView.frame.origin.y = CGRectGetMaxY(self.searchController.searchBar.frame);
[self.view addSubview: self.searchController.searchBar];
}
To answer question one: you need to set tableView.backgroundView
UIView *tableBackgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
tableBackgroundView.backgroundColor = GL_BACKGROUD_COLOR;
self.tableView.backgroundView = tableBackgroundView;

Related

Custom UITableViewCell auto layout doesn't work

I'm trying to implement a custom table cell using Auto Layout programmatically like below, but for some reason I didn't get the expected result.
Expected:
Actual:
My observation is:
The cell height doesn't grow as the content grows, and content overflows;
The bar element should be a vertical blue bar, but it's not properly showing up;
Setting background colors on the UIView elements doesn't work at all for some reason.
Please share some pointers on what I did wrong. Thanks in advance
UITableViewCell Code is below:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.infoContainer = [[UIView alloc] init];
self.title = [[UILabel alloc] init];
self.time = [[UILabel alloc] init];
self.bar = [[UIView alloc] init];
[self.infoContainer addSubview:self.bar];
[self.infoContainer addSubview:self.title];
[self.infoContainer addSubview:self.time];
[self.contentView addSubview:self.infoContainer];
self.infoContainer.translatesAutoresizingMaskIntoConstraints = NO;
self.title.translatesAutoresizingMaskIntoConstraints = NO;
self.time.translatesAutoresizingMaskIntoConstraints = NO;
self.bar.translatesAutoresizingMaskIntoConstraints = NO;
[self.infoContainer.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor constant:18].active = YES;
[self.infoContainer.rightAnchor constraintEqualToAnchor:self.contentView.rightAnchor constant:-18].active = YES;
[self.infoContainer.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:10].active = YES;
self.infoContainer.backgroundColor = [UIColor yellowColor];
[self.bar.leftAnchor constraintEqualToAnchor:self.infoContainer.leftAnchor constant:0].active = YES;
[self.bar.topAnchor constraintEqualToAnchor:self.infoContainer.topAnchor constant:0].active = YES;
[self.bar.bottomAnchor constraintEqualToAnchor:self.infoContainer.bottomAnchor constant:0].active = YES;
[self.bar.heightAnchor constraintEqualToAnchor:self.infoContainer.heightAnchor].active = YES;
[self.bar.widthAnchor constraintEqualToConstant:10];
self.bar.backgroundColor = [UIColor blueColor];
[self.title.leftAnchor constraintEqualToAnchor:self.bar.rightAnchor constant:15].active = YES;
[self.title.rightAnchor constraintEqualToAnchor:self.infoContainer.rightAnchor constant:0].active = YES;
[self.title.topAnchor constraintEqualToAnchor:self.infoContainer.topAnchor constant:0].active = YES;
[self.time.leftAnchor constraintEqualToAnchor:self.title.leftAnchor constant:0].active = YES;
[self.time.rightAnchor constraintEqualToAnchor:self.title.rightAnchor constant:0].active = YES;
[self.time.topAnchor constraintEqualToAnchor:self.title.bottomAnchor constant:10].active = YES;
}
return self;
}
and in the table view, I have:
self.recentView.rowHeight = UITableViewAutomaticDimension;
self.recentView.estimatedRowHeight = 64.0f;
Thanks!
Automatic height completely depends on hooking constraints properly from top to bottom , so you miss 2 constraints
1-
[self.infoContainer.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:10].active = YES;
2-
[self.time.bottomAnchor constraintEqualToAnchor:self.infoContainer.bottomAnchor constant:10].active = YES;
Tip : If you set top & bottom constraints here
[self.bar.topAnchor constraintEqualToAnchor:self.infoContainer.topAnchor constant:0].active = YES;
[self.bar.bottomAnchor constraintEqualToAnchor:self.infoContainer.bottomAnchor constant:0].active = YES;
Then no need for
[self.bar.heightAnchor constraintEqualToAnchor:self.infoContainer.heightAnchor].active = YES;

Unable to select search result in UISearchController

I have a tableView list in my IOS app. I wish to search my tableView list with UISearchController. By default the list will display correctly.
When I active search controller (Click on the UISearchController), my existing list will disappear and when I key in related keywords, result will be displayed accordingly.
But when I check on related search result, searchController will be deactivate and back to default list.
Any idea?
- (void)viewDidLoad {
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.barTintColor = ThemeLightGrayColor;
self.searchController.hidesNavigationBarDuringPresentation = NO;
[self.searchController.searchBar.layer setBorderColor:[UIColor colorWithRed:229.0/255 green:229.0/255 blue:229.0/255 alpha:1].CGColor];
[self.searchController.searchBar setPlaceholder:#"Search"];
[self.searchController.searchBar.layer setBorderWidth:0.5];
[self.searchController.searchBar setKeyboardType:UIKeyboardTypeDefault];
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]] setDefaultTextAttributes:#{ NSFontAttributeName: [UIFont fontWithName:#"Bitter" size:14]}];
[self.searchController.searchBar sizeToFit];
_searchResultArr=[NSMutableArray array];
_tableView.tableHeaderView = ({
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0, DCNaviH, 0, self.searchController.searchBar.frame.size.height + 50);
_segPromotion = [[UISegmentedControl alloc] initWithItems:#[#"All",#"Near Me",#"Coming Soon"]];
_segPromotion.selectedSegmentIndex = 0;
_segPromotion.backgroundColor = ThemeWhiteColor;
_segPromotion.frame = CGRectMake(10, self.searchController.searchBar.frame.origin.y+5 + self.searchController.searchBar.frame.size.height , ScreenW-20, 30);
[_segPromotion setTitleTextAttributes:#{NSFontAttributeName : [UIFont fontWithName:#"Bitter" size:13],NSForegroundColorAttributeName: ThemeDarkBlueColor } forState:UIControlStateNormal];
[_segPromotion setTitleTextAttributes:#{NSFontAttributeName : [UIFont fontWithName:#"Bitter" size:13],NSForegroundColorAttributeName : ThemeWhiteColor} forState:UIControlStateSelected];
[_segPromotion addTarget:self action:#selector(SegmentChangeViewValueChanged:) forControlEvents:UIControlEventValueChanged];
[view addSubview:self.searchController.searchBar];
[view addSubview:_segPromotion];
view;
});
[self.view addSubview:_tableView];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString];
[self.tableView reloadData];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self updateSearchResultsForSearchController:self.searchController];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
[self updateSearchResultsForSearchController:self.searchController];
}
- (void)searchForText:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[self.searchBar scopeButtonTitles][self.searchBar.selectedScopeButtonIndex]];
}
What you have to do is adding self.searchController.obscuresBackgroundDuringPresentation = NO; when initializing the searchController.
This is because, as you have used [[UISearchController alloc] initWithSearchResultsController:nil], the results will be shown in the same view controller (because of nil parameter). For this reason, if obscuresBackgroundDuringPresentation is set to YES (this is the case by default), the results area won't be touchable.

IOS 11: Hide TableView does not working

Hi I can't hide my table view using [self.autocompleteTableView setHidden:YES];, but It works on ios 10 but in ios 11 it's not hiding the table view.
Anybody could help me?
There's my code:
-(void) seachBarSetup{
self.autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.profileMapSearchBar.frame.origin.x, self.profileMapSearchBar.frame.origin.y + self.profileMapSearchBar.frame.size.height, self.profileMapSearchBar.bounds.size.width, self.view.frame.size.height - self.profileMapSearchBar.bounds.size.height - 64) style:UITableViewStylePlain];
self.autocompleteTableView.estimatedRowHeight = 60.0;
self.autocompleteTableView.rowHeight = UITableViewAutomaticDimension;
self.autocompleteTableView.delegate = self;
self.autocompleteTableView.dataSource = self;
if (#available(iOS 11.0, *)) {
self.autocompleteTableView.insetsContentViewsToSafeArea = YES;
}
self.profileMapSearchBar.alpha = 0.8;
self.profileMapSearchBar.delegate = self;
[self.view addSubview:self.autocompleteTableView];
[self.autocompleteTableView setHidden:YES];
self.profileMapSearchBar.barTintColor = [UIColor whiteColor];
[self.profileMapSearchBar sizeToFit];
}
Try this self.autocompleteTableView.hidden = YES; insted of [self.autocompleteTableView setHidden:YES];
OR
You will remove tableview from view by using this
[self.autocompleteTableView removeFromSuperview];
This will work.

iOS SearchBar and SeachDisplayController misplacement

I need to use a searchbar but I use this code :
self.searchBar = [UISearchBar new];
_searchBar.frame = CGRectMake(0, 0, 200, 44);
_searchBar.searchBarStyle = UISearchBarStyleMinimal;
_searchBar.placeholder = #"Search stores";
_searchBar.delegate = self;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_searchController.searchResultsDataSource = self;
_searchController.searchResultsDelegate = self;
_searchController.delegate = self;
self.definesPresentationContext = YES;
self.edgesForExtendedLayout = UIRectEdgeNone;
and the result is this... I lost my hamburgare menu button and I cant change the width of the searchbar. I also have a strange gap and it seeems to be as big as my navbar. How can I get back the hamburger menu button and fix the strange gap?
Put following code into ViewDidLoad. It will work for iOS 7+
if(SYSTEM_VERSION_GREATER_THAN(#"6.1")) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
Edited
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
CGRect frame = self.searchResultsTableView.frame;
frame.origin.y = CGRectGetHeight(self.searchContentsController.navigationController.navigationBar.frame);
frame.size.height = CGRectGetHeight(frame) - CGRectGetMinY(frame);
self.searchResultsTableView.frame = frame;
frame = self.searchBar.frame;
self.searchBar.frame = frame;
[self.searchContentsController.view insertSubview:self.searchBar aboveSubview:self.searchResultsTableView];
}
One More Try to Implement this solution.
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
self.navigationController.navigationBar.translucent = YES;
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
self.navigationController.navigationBar.translucent = NO;
}
Note : UISearchDisplayController is deprecated in iOS 8. Apple Document
Use following code support in iOS8
searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.dimsBackgroundDuringPresentation = NO;
searchController.hidesNavigationBarDuringPresentation = NO;
searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.tableView.tableHeaderView = self.searchController.searchBar;
Also you can download the sample code from here.

iOS 7 UITableView inside a popover

I have a UITableView which has some custom styling. This table view appears in two places in the app, one of which is inside a UIPopoverController. However when the tableview is inside the popover it takes on the default tableview styling as stated in the UI Transition Guide under "Popover".
The problem I have is that there appears to be nowhere to change this behaviour. Regardless of where I try and modify properties of the tableview the view inside the popover doesn't change.
Anyone dealt with this issue before or have any ideas?
Here is the init method of LibraryProductView where I create the table view:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.sectionOrdering = [NSArray arrayWithObjects:
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DESCRIPTION],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DOCUMENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_ACTIVE_INGREDIENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_PRODUCTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_DOCUMENTS], nil];
self.backgroundColor = [UIColor whiteColor];
self.tableView = [[UITableView alloc] initWithFrame:CGRectInset(self.bounds, 10, 0) style:UITableViewStyleGrouped];
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.showsVerticalScrollIndicator = NO;
[self addSubview:self.tableView];
}
return self;
}
Here is where the containing view (LibraryProductView) is added to the popover:
- (IBAction)didTouchInformationButton:(id)sender
{
if (_infoPopover != nil && _infoPopover.isPopoverVisible)
{
[_infoPopover dismissPopoverAnimated:YES];
return;
}
CGSize preferredSize = CGSizeMake(600.0f, 500.0f);
LibraryProductViewController* productController = [[[LibraryProductViewController alloc] initWithPreferredSize:preferredSize] autorelease];
productController.filterByMyCompany = NO;
productController.product = _activityInput.product;
UINavigationController* nav = [[[UINavigationController alloc] initWithRootViewController:productController] autorelease];
nav.title = _activityInput.product.name;
RELEASE(_infoPopover);
_infoPopover = [[UIPopoverController alloc] initWithContentViewController:nav];
_infoPopover.popoverContentSize = CGSizeMake(preferredSize.width, preferredSize.height + 46);
[_infoPopover presentPopoverFromRect:_infoButton.frame inView:_infoButton permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
The LibraryProductView is created within viewDidLoad method of LibraryProductViewController.
- (void)viewDidLoad
{
[super viewDidLoad];
self.libraryProductView = [[LibraryProductView alloc] initWithFrame:(usingPreferredSize ? CGRectMake(0.0, 0.0, preferredSize.width, preferredSize.height) : self.view.bounds)];
self.libraryProductView.dataSource = self;
self.libraryProductView.delegate = self;
[self.view addSubview:self.libraryProductView];
}
To set properties for the TableView you might do so in
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
[tableView setBackgroundColor:[UIColor redcolor]];
[tableView setSeparatorColor: [UIColor blueColor]];
return 1;
}
This, of course, assumes you have set UITableViewDataSource in your .h file

Resources