UISearchBar inside UIPopoverController moving on click - ios

I'm currently trying to place a table view and search bar inside a popover but I'm getting a really weird bug. Whenever I click on the search bar, the cancel button animates in and the bar promptly lowers about the size of a status bar.
I've tried playing around with the UIBarPosition delegate, but that doesn't do anything either. I've tried just about everything I can think of so I thought I'd ask your help. Here's the code I use in the UITableViewController to add the search bar to the table header:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self)
{
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, popoverWidth, singleRowHeight)];
searchBar.delegate = self;
searchBar.showsScopeBar= YES;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
searchDisplayController.searchResultsTableView.rowHeight = singleRowHeight;
self.automaticallyAdjustsScrollViewInsets = NO;
self.tableView.tableHeaderView = searchBar;
return self;
}
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
return UIBarPositionTop;
}
Thanks!

according to Extra space above search bar when UISearchDisplayController is active
- (void)viewDidLoad {
[super viewDidLoad];
if ([self respondsToSelector:#selector(edgesForExtendedLayout:)]) { /// iOS 7 or above
self.edgesForExtendedLayout = UIRectEdgeNone;
}
}

Related

Scope buttons hiding behind the UITableview

I am creating a search screen using UISearchController, in which I have to show three scope buttons.I have put search bar programmatically.But somehow the scope buttons are hiding behind the UITableView.
- (void)viewDidLoad {
[super viewDidLoad];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.tblFoundList.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]] setDefaultTextAttributes:#{NSForegroundColorAttributeName: [UIColor blackColor]}];
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]] setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:#"Search by" attributes:#{NSForegroundColorAttributeName: [UIColor lightGrayColor]}]];
self.searchController.searchBar.tintColor = [UIColor purpleColor];
self.searchController.searchBar.barTintColor = [UIColor groupTableViewBackgroundColor];
self.searchController.searchBar.scopeButtonTitles = #[#"A",#"B", #"C"];
[self.searchController.searchBar sizeToFit];
}
Could anyone help in this? Thank you.
As I mentioned earlier I had created UISearchController programmatically, so the UITableview in the image is UISearchController's UITableview, that is why nothing was affecting it.
So I got help from
UISearchController Search Bar Position Drops 64 points
and
UISearchBar presented by UISearchController in table header view animates too far when active
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
if (bar == self.searchController.searchBar) {
return UIBarPositionTopAttached;
}
else { // Handle other cases
return UIBarPositionAny;
}
}
On adding following method the search bar got shifted up and the scope buttons were visible.
But thank you all for helping out.

Subclasses of UISearchBar and UISearchController breaks VoiceOver

In my app I have a UIViewController that lets users search in a server database. (It's not filtering an existing list.) Results are displayed in a UITableView, which is the searchResultsController of the custom UISearchController.
We need a different behaviour of the 'Cancel' button so we created subclasses of UISearchController and UISearchBar.
My issue is: VoiceOver doesn't see the UITableView that display results. Instead of selecting the header, then the first row and so on, it selects the whole area and reads something about closing the view. (There's no 'Close' button). It seems like the UITableView showing the results is hidden. Here's the view hierarchy when the searchResultsController is presented.
If I initiate the searchController with the default class, I get the correct behaviour (for VoiceOver, not for the 'Cancel' button).
The documentation doesn't say anything specific about subclassing.
Any hint?
Here's the code when we initialize them:
//Showing results
self.resultsTableController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
self.resultsTableController.tableView.delegate = self;
self.resultsTableController.tableView.dataSource = self;
self.resultsTableController.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
// Configuring searchController
self.searchController = [[RedactedNameSearchController alloc] initWithSearchResultsController:self.resultsTableController];
// When initialized with UISearchController, it works:
// self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.delegate = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self ;
UISearchController subclass code:
#interface MySearchController () {
UISearchBar *searchBar;
}
#end
#implementation MySearchController
- (UISearchBar *)searchBar
{
if (searchBar == nil) {
searchBar = [[MySearchBar alloc] initWithFrame:CGRectZero];
searchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
}
return searchBar;
}
#end
`UISearchBar is implemented like so:
#implementation MySearchBar
-(void)setShowsCancelButton:(BOOL)showsCancelButton {
}
-(void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated {
}
#end

UISearchController's SearchBar gets detached and overlaps TableView if rotated while not visible

So I've got a UITableView with a UISearchController. It loads up fine, then I do my search and select a result, pushing a new view controller to my UINavigationController.
If, while the new VC is onscreen, I rotate my phone, then rotate back, and go back...when I get back to the UITableView, the UISearchBar is nowhere to be seen. If, to troubleshoot, I hide the UINavigationBar, or if I look at the view hierarchy, I find that it has moved up behind the navigation bar and is being covered.
I've tried all kinds of hackey solutions, like moving the frame of the bar after it finishes loading, with no success. I really need help on this one, I'm stumped. None of the stuff I've found on google has helped.
Here are screenshots of before, after, and the view heirarchy:
Screenshots
Here is all the View-related code:
- (void)viewDidLoad {
[super viewDidLoad];
if (!self.title)
self.title = #"GLOBAL SEARCH";
self.tableView.backgroundColor = [UIColor whiteColor];
//TESTING
//if I don't have this, the bar detaches from the navigation bar
self.edgesForExtendedLayout = UIRectEdgeAll;
self.extendedLayoutIncludesOpaqueBars = YES;
//test
self.definesPresentationContext = YES;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.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.searchController.searchBar.backgroundImage = [[UIImage alloc] init];
self.searchController.searchBar.backgroundColor = kConstantBaseColor;
self.searchController.searchBar.barTintColor = kConstantBaseColor;
self.searchController.searchBar.tintColor = kConstantLabelColor;
//set color
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{NSForegroundColorAttributeName:kConstantLabelColor}];
self.searchController.hidesNavigationBarDuringPresentation = NO;
[self.searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.tableView.clipsToBounds = NO;
self.tableView.tableHeaderView.clipsToBounds = NO;
self.tableView.tableHeaderView.backgroundColor = kConstantBaseColor;
//put purple block above, in case user scrolls up
CGRect bufferFrame = CGRectMake(self.searchController.searchBar.frame.origin.x,
self.searchController.searchBar.frame.origin.y-300,
self.searchController.searchBar.frame.size.width*2,
300);
UIView *purpleBuffer = [[UIView alloc] initWithFrame:bufferFrame];
purpleBuffer.backgroundColor = kConstantBaseColor;
[self.tableView addSubview:purpleBuffer];
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
}];
}
Does anyone have any solutions? I'm stumped and have been beating my head against it for days. Thank you so much.

tableHeaderView UISearchBar not showing

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)];
}
}

PFQueryTableViewController (parse.com) behaves weirdly when UISearchBar is added as header.

For some reason, my PFQueryTableViewController's table view behaves weirdly when I add a UISearchBar as the header. When added, the table view fails to load objects queried from the DB when the app starts up. It displays Loading without actually displaying any objects. When I pull-to-refresh however, it populates with the objects perfectly. Another problem is the cell dividers (thin gray lines) disappear. Here is the relevant code:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
self.parseClassName = #"userListing";
self.textKey = #"listingName";
self.pullToRefreshEnabled = YES;
self.paginationEnabled = YES;
self.objectsPerPage = 15;
self.tableView.rowHeight = 60;
self.locationForQuery = [[PFUser currentUser] objectForKey:#"userLocation"];
//append the add button and title to the navigation bar
UIBarButtonItem* addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addListing)];
[self.navigationItem setRightBarButtonItem:addButton];
self.navigationItem.title = #"Listings";
}
return self;
}
- (void)viewDidLoad{
self.locationSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.locationSearchBar.delegate = self;
self.tableView.tableHeaderView = locationSearchBar;
}
Let me know what else you need to see.
Figured out the problem. I forgot to call [super viewDidLoad] in my overwrite.

Resources