How to insert a UIActivityIndicator into search bar textfield? - ios

I want to add a UIActivityIndicator in the right side of textfield of the search bar.
So that search bar will keep on showing loading status while it fetches data from DB.
Any idea about this?

This is away to change the search magnifier with the spinner
UITextField *searchField = nil;
for (UIView *subview in searchBar.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
if (searchField) {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
searchField.leftView = spinner;
}
or if you like you can add the spinner over the search bar views
[self.searchBar addSubview:spinner];

Add the activity indicator as the subView of searchbar like :
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake(160, 240);
spinner.hidesWhenStopped = YES;
spinner.tag = 7;
[self.searchBar addSubview:spinner];
}
And when you want to remove the activity indicator you can use the tag.

Adding my comment as answer.
You can try to use [searchbar addSubview:activityIndicator]; There are no in built ways to do this. Only thing you can change is the search icon using the method setImage:forSearchBarIcon:state:. This can be used to add a different image in searchbar. Apple doc on Searchbar. A work around would be to loop through the subviews of the search bar and add the activity indicator to the subview at required place.

Related

SearchDisplayController's searchResultsTableView not showing

I'm adding a search bar to the titleView of the navigationItem, so that it looks like this.
But when I tap on the search bar to get focus, it moves out of the screen and the keyboard is dismissed automatically, and then it looks like this.
So, I'm neither able to type anything, nor see any results.
Here is my code where I am adding the search bar to the navigation item in viewDidLoad method.
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0.0, 320.0, 44.0)];
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
[self.searchBar setShowsCancelButton:NO animated:NO];
searchBarView.autoresizingMask = 0;
self.headSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.headSearchDisplayController.delegate = self;
self.headSearchDisplayController.searchResultsDelegate = self;
self.headSearchDisplayController.searchResultsDataSource = self;
[searchBarView addSubview:self.searchBar];
self.searchBar.delegate = self;
self.navigationItem.titleView =searchBarView;
I tried it without setting the autoresizingmask, without any success. I am getting a hint that this behavior is happening because the search bar is in a view which is 44points, which is again inside the titleView. I am guessing that the searchResultsTableView doesn't have enough height to be seen, and thus we're only able to see the overlay. How do I solve this issue? Pointers in the right direction will be extremely helpful.
Here is an animated gif of the situation.
I figured out that the Navigation bar was usually pushed up, when the search was focused, and since the search bar was in the navigation bar it was pushed up with it, and couldn't be visible on the screen.
I solved this by subclassing the UISearchDisplayController to hide the navigationBar. Here is the code.
#import "CustomSearchDisplayController.h"
#implementation CustomSearchDisplayController
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
if(self.active == visible) return;
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
if (visible) {
[self.searchBar becomeFirstResponder];
} else {
[self.searchBar resignFirstResponder];
}
}
#end
Found this answer here: https://stackoverflow.com/a/3257456/1011042

How to set activity indicator for a view when Click the table cell

I am developing one iPad application using story board.In my view controller one table is set as the left side and one more view is set as the right side.I need to set one activity controller above the right side view.Which need to start animating when i click the right side table cell and need to stop after 1s.I am using 'didSelectRowAtIndexPath' function for select table cell.How i will set activity indicator for my situation.
In didSelectRowAtIndexPath method you could make a call to below function:-
-(void)setActivityIndcator:(UIView *)view
{
transparentView = [[UIView alloc] init];
transparentView.frame = view.frame; //Provide frame of right side view.
transparentView.alpha = 0.5; //To provide transparent look and feel.
activityInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityInd.frame = CGRectMake(transparentView.frame.size.width/2, transparentView.frame.size.height/2, 50, 50); //Change as you want it to be
[transparentView addSubview:activityInd];
[activityInd startAnimating];
[self performSelector:#selector(stopIndicator) withObject:self afterDelay:1];
}
-(void)stopIndicator //This method will be called after delay and would remove view and activityInd
{
[activityInd stopAnimating];
[activityInd removeFromSuperview];
[transparentView removeFromSuperview];
}
Also you could create a subclass of it and use it in any viewcontroller by creating it's object. That why u don't need to write down all function everytime.
use this in this method
didSelectRowAtIndexPath
UIActivityIndicatorView *activityView =
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityView.frame = CGRectMake(120, 230, 50, 50);
[self.view addSubview:activityView];
Use the delegate method didSelectRowAtIndexPath of TableView and write the following code into it
UIActivityIndicatorView *activity =
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activity.frame = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 50, 50);
[self.view addSubview:activity];
[activity startAnimating];
Stope this when your work is done and remove it from the view

Display activity indicator only after clicking the button

hi I’m trying view the activity indicator by clicking the button. I have tried but i having some issues. If i drag the activity indicator to my storyboard but its visible while running my app i want to display while clicking the button. in my app I’m giving the photo upload option. So if they click the upload button i want to view the activity indicator . ..
this is code i have tired but its not working.
this is my h file where i have implemented the activity indicator and the button..
- (IBAction)click:(id)sender;
#property (strong, nonatomic) IBOutlet UIActivityIndicatorView *spinner;
this is code which i have used in my m file...
-(void)temp
{
spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
[self.view addSubview:spinner];
[spinner startAnimating];
[spinner release];
}
and im calling this function in my button..
- (IBAction)click:(id)sender {
[self temp];
}
and also im have some other issues i saw the code here the problem is
[yourView addSubview:spinner];
in yourview i give my uiviewcontroller name but its giving error i have changed to
[self.view addSubview:spinner];
pls tell me where im doing wrong and what is the right way to make it.. thanks
Man I got the problem...I think you are adding white activity indicator on white view that's why it is not being display but it is there for sure so...use some other style UIActivityIndicatorViewStyleGray
-(void)temp
{
spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
[self.view addSubview:spinner];
[spinner startAnimating];
[spinner release];
}
First of all since you already have an IBOutlet for the spinner so do not allocate a new one in temp method. Modify your temp method to look like this:
-(void)temp
{
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
[self.view addSubview:spinner];
[spinner startAnimating];
}
I f you are using interface builder like Storyboard or XIB then just set the frame and evrything there itself. And if you want to change the property of it at runtime just use the IBOutlet you have used.
You need not to instantiate again just use its getter and setter to use it accordingly. So here you can do.
- (IBAction)click:(id)sender
{
[self.spinner startAnimating];
}
and synthesize spinner before using it.
And if you want to use it programmatically delete it from XIB and remove all references corresponding to it. And declare in .h file
UIActivityIndicatorView *spinner;
or you can make a property
#property(nonatomic, strong)UIActivityIndicatorView *spinner;
and just use your own code.
Assuming your spinner connected to your view.
In your viewDidLoad you can setup your view, then it'll show when you tap the button:
- (void)viewDidLoad
{
[super viewDidLoad];
self.spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[self.spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
self.spinner.hidden = YES;
}
- (IBAction)click:(id)sender
{
self.spinner.hidden = NO;
[self.spinner startAnimating];
// Additionally you can show spinner in top bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)stopSpinner
{
if (self.spinner.isAnimating) {
self.spinner.hidden = YES;
[self.spinner stopAnimating];
// And hide it when you don't need it anymore
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}
I suggest you see this answer: How to get orientation-dependent height and width of the screen? to re position your spinner.

objective c - Display activity indicator when parsing an xml on tableview cell click

I am a beginner in iOS programming. I am creating a tableview where, on click of each cell a different xml gets parsed. I want to show a activity indicator when xml is parsing and stop that indicator when parsing is finished. my code is,
//Activity Indicator
//in viewDidLoad,
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.frame = CGRectMake(0.0, 0.0, 20.0, 20.0);
activityIndicator.backgroundColor=[UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:0.5];
activityIndicator.center=self.view.center;
[activityIndicator startAnimating];
[self.view addSubview:activityIndicator];
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryView==nil)
{
NSLog(#"index path is %i",indexPath.row+1);
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_selected.png"]];
[myObj1 changecheckedvalue:1 forindex:[[tempArr objectAtIndex:indexPath.row]mainID]];
if (indexPath.row==0)
{
flagToCheck=1;
xmlName=#"Pournima";
[parseArray removeAllObjects];
[self xmlParsing];
}
}
}
How to achieve that? Please help.
You can use MBProgressHUD to display an activity view that sits on top the current view. The library is quite easy to use and implement. You would just import MBProgressHUD.h, and then call this method:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
or if you want to show an activity view inside cell, maybe next to title, you could use something like,
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[cell.imageView addSubview:activityView];
[activityView startAnimating];
you would then remove the activityView from its superview when you are done.
Try using this one : https://github.com/jdg/MBProgressHUD
It has a better look and feel.
Start the activity indicator before calling the method for parsing in "didSelectRowAtIndexPath" and stop it at the end of the method for parsing.

Clear button at uisearchbar not working at all

I created a search bar programmatically and added to my view using the codes below:
- (void)viewDidLoad
{
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(xPositionForSearchBar, yPositionForSearchBar, widthForSearchBar, heightForSearchBar)];
UIView *bg = [[searchBar subviews] objectAtIndex:0];
searchBar.delegate = self;
searchBar.placeholder = #"Search record";
for(UIView *view in searchBar.subviews){
if([view isKindOfClass:[UITextField class]]){
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
[bg removeFromSuperview];
[self.view addSubview: searchBar];
}
The code is implemented with UISearchBarDelegate and UITextFieldDelegate.
I have tried using
- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar
{
NSLog(#"cancel clicked");
searchBar.text = #"";
[aSearchBar resignFirstResponder];
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
NSLog(#"clear");
[self performSelector:#selector(searchBarCancelButtonClicked:) withObject:searchBar afterDelay: 0.1];
return YES;
}
and yet, the text inside the searchBar is not cleared at all when i click on the "clear button" - a circle with a "X" inside.
The clear button works when I implemented it in IB. Wonder why?
Kindly advice, many thanks.
This might happen if you position your search bar out of the bounds of a parent view. Then, the touches aren't delivered to your searchBar properly. This also affects text editing controls like copy & paste.
I have checked your code it work fine on device as well as on simulator.

Resources