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.
Related
I would like to add a progress bar between navigation bar and UISearchBar. May I know how can I implement this? Please help. Thank you.
Here is my current code in Cell.m
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect barFrame = CGRectInset(self.searchBar.frame, 10.0f, 10.0f);
self.searchBar.frame = barFrame;
}
Here is my current code in ViewController.m
Did not reflect in this code after edited. _searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 24)];
- (void)viewDidLoad {
[super viewDidLoad];
_valueProgress = [[LDProgressView alloc] initWithFrame:CGRectMake(0,DCNaviH, ScreenW, 10.0f)];
_valueProgress.type = LDProgressSolid;
_valueProgress.color = ThemeRedColor;
_valueProgress.progress = 0.40;
_valueProgress.flat = #YES;
_valueProgress.showText = #NO;
[self.view addSubview:_valueProgress];
}
- (UISearchBar *)searchBar{
if (!_searchBar) {
_searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 24)];
[_searchBar setBackgroundImage:[UIImage imageNamed:#"ic_searchBar_bgImage"]];
}];
}
return _searchBar;
}
There are a few simple options for you depending on your apps UX. I think the best solution for you based on how you explained your issue would be to include the progress bar to your view and make sure it's above the other views while positioning it below the navigation bar.
_valueProgress = [[LDProgressView alloc] init];
_valueProgress.translatesAutoresizingMaskIntoConstraints = NO;
_valueProgress.type = LDProgressSolid;
_valueProgress.color = ThemeRedColor;
_valueProgress.progress = 0.40;
_valueProgress.flat = #YES;
_valueProgress.showText = #NO;
[self.view addSubview:_valueProgress];
[_valueProgress.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].isActive = YES;
[_valueProgress.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].isActive = YES;
[_valueProgress.heightAnchor constraintEqualToConstant:10.0f].isActive = YES;
if (#available(iOS 11.0, *)) {
[_valueProgress.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor].isActive = YES;
} else {
[_valueProgress.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].isActive = YES;
}
And if the search bar has been added above the progress bar, you can always call [self.view bringSubviewToFront:_valueProgress] afterwards.
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;
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.
I have added a UISearchBar programatically to my UITableView, it was showing perfectly fine untill I decided to add an offset to my UITableView to hide the UISearchBar when the view is loaded. I would like help displaying it again.
This is what my code looks like.
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.contentOffset = CGPointMake(0.0f, 44.0f);
mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
mySearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
mySearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
mySearchBar.keyboardType = UIKeyboardTypeAlphabet;
mySearchBar.delegate = self;
self.tableView.tableHeaderView = mySearchBar;
// Create the search display controller
UISearchDisplayController *searchController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
I am not really sure where to go to from here.
This code will work in both iOS6 and iOS7.
Note that in iOS7 you will loose transparency of NavigationBar
if ([self respondsToSelector:#selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
[self.tableView setContentOffset:CGPointMake(0, mySearchBar.frame.size.height)];
If you want to save default transparency in iOS7 use this code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)]) {
[self.tableView setContentOffset:CGPointMake(0, -20)];
}
else {
[self.tableView setContentOffset:CGPointMake(0, mySearchBar.frame.size.height)];
}
}
I have added a message count using the MKNumberBadgeView via the following code in my uiTableView Homepage -
-(void)counterBtn{
_numberBadge = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(25, -10, 40, 40)];
_numberBadge.strokeColor = [UIColor colorWithRed:239.0/255.0 green:117.0/255.0 blue:33/255.0 alpha:0];
_numberBadge.fillColor = [UIColor colorWithRed:239.0/255.0 green:117.0/255.0 blue:33/255.0 alpha:1];
_numberBadge.shine = NO;
_numberBadge.hideWhenZero = YES;
_numberBadge.value = _countBtnNo;
[self.navigationController.navigationBar addSubview:_numberBadge];
}
All works fine - but if I navigate from this view to a subview the counter is still shown over the back button as in screenshot -
Is it possible to temporarily hide this button - then show it again when I return to the homepage?
I was thinking something along these lines in the subview's viewdidload method? -
[self.navigationController.navigationBar.subviews setHidden:YES];
Try this in HomeScreen:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
_numberBadge.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
_numberBadge.hidden = YES;
}