stange behaviour UIRefreshControl with UISearchBar - ios

i'm using UIRefreshControl with UISearchBar when i'm pull to refresh i see a white background at the top of uiviewcontroller
UISearchBar *searchBar =
[[UISearchBar alloc] initWithFrame:
CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.tableView.tableHeaderView = searchBar;
_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:#selector(update) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:_refreshControl];
the problem is the white background above
any ideas?

Create a background view for the table and set its color to clearColor, like this
- (void)viewDidLoad {
self.tableView.backgroundView = [UIView new];
self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
}

Related

pull to refresh not working in collection view when collection view have no data in ios

This is my refresh control in worldview method.
self.colView.refreshControl = [[UIRefreshControl alloc] init];
self.colView.refreshControl.backgroundColor = [UIColor purpleColor];
self.colView.refreshControl.tintColor = [UIColor whiteColor];
[self.colView.refreshControl addTarget:self
action:#selector(refresh)
forControlEvents:UIControlEventValueChanged];
Please try this code. Collection view lost our height.
self.colView.alwaysBounceVertical = YES;
Your code looks fine, Just you have to move tableview at top and set some frame before adding it to collectionview. This will work even if your collection view content view have 0 frame size:
-(void)addPullToRefressToTableView {
UIView *refreshView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 0, 0)];
[self.tableView insertSubview:refreshView atIndex:0];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.tintColor = [UIColor grayColor];
[refreshControl addTarget:self action:#selector(reloadDatas) forControlEvents:UIControlEventValueChanged];
NSMutableAttributedString *refreshString = [[NSMutableAttributedString alloc] initWithString:#"Pull To Refresh"];
[refreshView addSubview:refreshControl];
}

Adding UISegmentedControl in UISearchDisplayController

I want to add the UISegmentedControl below the searchBar and above the TableView of UISearchDisplayController. Currently UISearchDisplayController only shows its tableView under its SearchBar.
But i want to add a UISegmentedControl below the SearchBar so that I have SearchBar on the top, after SearchBar I have a UISegmentedControl and below that UISegmentedControl I have UITableView. Is that any way to do this using UISearchDisplayController or i have to make my own SearchDisplayController that have its own SearchBar , UISegmentedControl and TableView?
any suggestion? Thanks
Enable ShowScopeBar and add as many as segments by adding scop titles and handle them by following method
By Code
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[searchBar setShowsScopeBar:YES];
[searchBar setScopeButtonTitles:#[#"button1",#"button2"]];
[[self view] addSubview:searchBar];
By XIB
Delegate method to handle scope button action
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
}
Create a view in table
UIView *viewsearch=[[UIView alloc]initWithFrame:CGRectMake(0,-10, 320,83)];
[self.tblname addSubview:viewsearch];
[self.view addGestureRecognizer:revealController.panGestureRecognizer]
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,5, 320, 40)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchBar.delegate=self;
[searchBar setBarTintColor:[UIColor colorWithRed:(249/255.0) green:(9/255.0) blue:(99/255.0) alpha:1]];
searchBar.tintColor = [UIColor whiteColor];
searchBar.placeholder = #"Search items e.g. jacket";
Addsearchbar view in that view.
[viewsearch addSubview:searchBar];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
Use UISegmentedControl in this searchview.
NSArray *itemArray = [NSArray arrayWithObjects: #"General", #"Near me", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(50,53, 225, 22);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
[segmentedControl addTarget:self action:#selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tintColor = [UIColor colorWithRed:(249/255.0) green:(10/255.0) blue:(99/255.0) alpha:1];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin;
[viewsearch addSubview:segmentedControl];
I find the solution for my Problem that i added the UISegmentedControl as a Header of TableView of UISearchDisplayController. In this way it just looked like that i have added a UISegmentedControl Separately under the searchBar of UISearchDisplayController.
Thank You every one who tried to help.

iOS7: Not able to add search bar to Navigation Controller

In my SearchViewController.m, I added
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
[self.searchDisplayController.searchBar setBarTintColor:[UIColor whiteColor]];
self.searchResultsView.dataSource = self;
self.searchResultsView.delegate = self;
NSLog(#"fetching businesses");
[self fetchBusinesses];
// fix UITableViewCell height
self.tableView.rowHeight = 90;
}
But I can not see search bar. All I see is
What am I missing?
Thanks
You can add search bar programatically like follows:
make property of both UISearchBar and UISearchDisplayController i.e.
#property (nonatomic,strong) UISearchBar *searchBar ;
#property (nonatomic,strong) UISearchDisplayController *searchDisplayController;
then write following code:
- (void)viewDidLoad
{
searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate =self;
self.searchBar.showsCancelButton =TRUE;
[self.searchBar setHidden:NO];
[self.searchBar becomeFirstResponder];
[self.searchDisplayController setActive:YES];
[self.view addSubview:self.searchBar];
self.searchDisplayController =[[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.delegate =self;
self.searchDisplayController.searchResultsDataSource= self;
self.searchDisplayController.searchResultsDelegate = self;
}
also you need to implements UISearchDisplayDelegate, UISearchBarDelegate delegates for the same ViewController
Hope This code will work for you . Give it a try and let me know your feedback.
Now i find the answer , i just tested it on my side and its working fine at my end ,
// init search bar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
searchBar.showsCancelButton=YES;
// set up searchDisplayController
UISearchDisplayController *searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
[self.searchDisplayController.searchBar setBarTintColor:[UIColor whiteColor]];

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