How do I add an activity indicator in navigation bar? - iphone

I'm using activity indicator view in my app.
When I click on a button, I want the activity indicator to display in the navigation bar for 10 seconds then automatically hide.
I'm using the following code in view did load:
CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
self.activity = [[UIActivityIndicatorView alloc]
initWithFrame:frame];
[self.activity sizeToFit];
self.activity.autoresizingMask =
(UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
UIBarButtonItem *loadingView = [[UIBarButtonItem alloc] initWithCustomView:self.activity];
loadingView.target = self;
self.navigationItem.rightBarButtonItem = loadingView;
And in the button action I only start the indicator:
[self.activity startanimating];
But I cannot see the indicator in navigation bar.
Please tell me if there are any problems in my code...

UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
UIBarButtonItem *itemIndicator = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[self.navigationItem setLeftBarButtonItem:itemIndicator];
[activityIndicator startAnimating];

I don't understand why you're using target (and you even do not specify an action)...anyway I just tested your code and introduced this slight adjustment. Every time I want to start the activity indicator I use this piece of code:
UIActivityIndicatorView *test = (UIActivityIndicatorView*)self.navigationItem.rightBarButtonItem.customView;
[test startAnimating];
Obviously I don't use an ivar because it's not needed. It's just a cast and from your UIViewController and you can obtain it in a very fast way.
Hope it helps.

Related

label is not visible in UIToolBar

I am attempting to set a label into a subclassed UIToolBar. I am first creating the label, then converting it to a UIBarButtonItem and setting them to the toolbar, but the toolbar just appears blank.
here is my code in my init method of my subclassed toolbar
NSMutableArray *items = [[NSMutableArray alloc] init];
// prep the total label
_totalLabel = [[UILabel alloc] init];
_totalLabel.font = [_totalLabel.font fontWithSize:15];
_totalLabel.textColor = [UIColor whiteColor];
_totalLabel.text = NSLocalizedString(#"TOTAL", nil);
_totalLabel.frame = CGRectMake(0, 0, 100, 44);
_totalLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
_totalLabel.layer.borderWidth = 2;
_totalLabel.layer.borderColor = [UIColor greenColor].CGColor;
_totalLabel.backgroundColor = [UIColor clearColor];
UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer2];
UIBarButtonItem *total = [[UIBarButtonItem alloc] initWithCustomView:_totalLabel];
[items addObject:total];
[self setItems:items];
any idea whats wrong?
Where are you calling this code from? If you have it in initWithFrame: or initWithCoder: it will not work.
Try this:
- (void)layoutSubviews {
[super layoutSubviews];
if(self.items.count == 0) {
//Your code here
}
}
That will get it working. The question is though should you be doing this in layoutSubviews? Probably not. You could also put a setup method on your UIToolbar and call that at a later point in your application.
This is not a uitoolbar. this is a navigation bar so please remove navigationbar and add toolbar . Try this type .

Want to add the UIActivityIndicator beside the UIBarButtonItem

How can i add the activity indicator beside the right bar button item (CreateNew button)?
One of the approaches that you can use is initializing the Bar Button using some custom view.
A bar minimum code which can help you get some hint is
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIView* aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 40)];
[aView setBackgroundColor:[UIColor blackColor]];
[[aView layer] setCornerRadius:8.0f];
[[aView layer] setBorderColor:[UIColor whiteColor].CGColor];
UIActivityIndicatorView* loadView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[loadView startAnimating];
[aView addSubview:loadView];
[loadView setFrame:CGRectMake(5, 5, 30, 30)];
[loadView release];
UIButton* aButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 2, 80, 35)];
[aButton setImage:[UIImage imageNamed:#"btnLogin.png"] forState:UIControlStateNormal];
[aView addSubview:aButton];
[aButton release];
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:aView];
[self.navigationItem setRightBarButtonItem:barButton];
[aView release];
[barButton release];
}
This is If you do it programmatically , else you can also make use of nib. Creating bar button this way will look something like this -
You can look for more option making using of [[UIBarButtonItem alloc] initWithCustomView:aView]; method.
Hope it helps!!
For this try to customize UINavigationController Class.
Add Activity Indicator on navigationBar and control it from your ViewController.
You can add the activity indicator using the following code:
//Create an instance of activity indicator view
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
//set the initial property
[activityIndicator stopAnimating];
[activityIndicator hidesWhenStopped];
//Create an instance of Bar button item with custome view which is of activity indicator
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
//Set the bar button the navigation bar
[self navigationItem].rightBarButtonItem = barButton;
//Memory clean up
[activityIndicator release];
[barButton release];
Please refer this link for detailed code.

ios : create NavigationBar programmatically, but cannot set the title

I create an NavigationBar programmaticlly without using xib file.
And create one left button with text "left", added to the navigationBar, and set the title to "Demo".
code :
-(void) createNavBar:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
{
self.navBarController = [[[UINavigationController alloc] init] autorelease];
navBar = [navBarController navigationBar];
navBarController.navigationBar.bounds = CGRectMake(0, 0, 320, navBarHeight);
[navBarController.navigationBar setBackgroundImage:[[UIImage imageNamed:#"www/images/ios_hd_bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 0, 2, 0)]
forBarMetrics:UIBarMetricsDefault];
[navBarController view];
navBarController.navigationItem.title = #"title";
[navBarController.view setFrame:navBarController.navigationBar.bounds];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button = [self renderNavBarTextBtn:#"left" withButton:button direction:#"left"];
[navBar addSubview:button];
[[[self webView] superview] addSubview: navBarController.view];
}
however, the program can successfully set the navigationbar background image, show the left button, but cannot set the title. so strange?
complements:
I use cordova framework to invoke . and The inteface just be provided to cordova invoke.
Thanks for answers below, but I still can't solve my problem.
ScreenShot: (Note: showing the left button but not showing the title)
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:#"left" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = _backButton;
self.navigationItem.title=#"Demo";
[_backButton release],
[_backButton = nil];
Please try this:
self.navigationItem.title=#"Your Title";
Set
navBar.title =#"Your title";
You can set your view title, which show title on navigation bar
self.title=#"My Navigation View";
Or you can set navigationBarTitle in the view where you want to show
self.navigationItem.title=#"My Navigation";
Try this code snippet :
//nativeControls.hideLeftNavButton();
nativeControls.setupRightNavButton(
"About",
"",
"onRightNavButton"
);
nativeControls.showNavBar();
otherwise refer to this blog :
http://zsprawl.com/iOS/2012/05/navigation-bar-with-nativecontrols-in-cordova/

What to do when UIBarButtonItem are not visible in UINavigationBar?

Hello Everyone
I am writing a simple code in ViewDidLoad, and class is child of UITableViewController, like below but buttons are not visible, while title is visible,
Another thing that I am clicking a button which in ViewDidLoad method ViewController.m, and which is call a method, code of that method is as below
//Code of button target method
-(void)statusMethod {
NSLog(#"statusMethod");
Status *ob=[[Status alloc]init];
[self presentModalViewController:ob animated:YES];
}
//Code of ViewDidLoad of Status.m
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, width, 43)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:navBar];
[navBar release];
UIBarButtonItem *home = [[UIBarButtonItem alloc] initWithTitle:#"HOME" style:UIBarButtonItemStylePlain target:self action:#selector(homeMethod)];
self.navigationItem.rightBarButtonItem = home;
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonSystemItemAdd target:self action:#selector(addMethod)];
self.navigationItem.leftBarButtonItem = add;
UILabel *label = [[UILabel alloc] initWithFrame:
CGRectMake(10,10,width-20,25)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.text = #"Status";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:25];
label.textAlignment = UITextAlignmentCenter;
[navBar addSubview:label];
[label release];
Any Idea, what is problem in code?
I you don't understand, then u can ask me again,
I will praise, if I will get solution of my problem
It seems like self.navigationItem is only looked at by the UINavigationController (controller, not bar). The bar itself isn’t going to check that.
So you should either
1) Use a real UINavigationController which comes with its own navigationBar and will handle the navigationItems for you. The self.navigationItem code above will work in that case. (you should consider self.navigationItem.title = #"Status";)
If your UITableViewController is going to be pushing stuff on and off the navigation stack, this is the path you should take in any case.
2) Use the UINavigationBar’s own navigation item. Except it doesn’t seem to come with a navigation item, so you have to add your own:
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Status"];
[navBar setItems:[NSArray arrayWithObject:navItem]];
[navItem release];
and then set the left and right buttons as
[navBar topItem].leftBarButtonItem = add;
[add release];
[navBar topItem].rightBarButtonItem = home;
[home release];
Dealing with UINavigationController/UINavigationBar/UINavigationItem can be confusing, but luckily Apple has a decent explanation of how all these things work together at the top of its UINavigationController documentation.

Hide ActivityIndicator and display Title in NavigationBar

i display an activityindicator like
CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
loading = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[loading startAnimating];
[loading sizeToFit];
loading.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
self.navigationItem.titleView = loading;
in my navigationbar on the top. And i call:
[loading stopAnimating];
to stop.
This works like it should, but after hiding the activity indicator i want to display a text (self.title = #"text";) and this is not working.
What do i miss?
Thanks for your help!
Try setting the self.navigationItem.titleView to nil and then set the title.