reveal a progressIndicator in Navigationbar - iphone

I want to display a revolving progress indicator in place of the title in NavigationBar when I click a button (refresh), and when refresh success, the indicator disappear and the title will be revealed again.
Any suggestions? thank you.

you can code for adding UIActivityIndicatorView into UInavigatiobar like:-
UIView *activityView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
activityView.backgroundColor = [UIColor clearColor];
UIActivityIndicatorView *act =[[UIActivityIndicatorView alloc] init]; //create this globule so u can stop it while your progress finished
act.frame=CGRectMake(0, 0, 30, 30);
[activityView addSubview:act];
self.navigationItem.titleView = activityView;
NOTE:- [act startAnimating]; and [act stopAnimating]; as par your Need
Or you can add this into UInavigation BarbuttonItem
UIView *activityView = [[UIView alloc] initWithFrame:CGRectMake(0,0,30,30)];
UIActivityIndicatorView *act =[[UIActivityIndicatorView alloc] init];
act.frame=CGRectMake(0, 0, 30, 30);
UIBarButtonItem *actItem = [[[UIBarButtonItem alloc] initWithCustomView:activityView] autorelease];
[activityView addSubview:act];
self.navigationItem.rightBarButtonItem = actItem;
NOTE:- [UIActivityIndicatorView startAnimating]; and [UIActivityIndicatorView stopAnimating]; as par your Need

Related

How to programmatically add a button to remove a WebView?

I have tried to add a UIBarButtonItem to my UINavigation bar but I can't get it to show up:
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width,self.view.frame.size.height)];
[self.view addSubview:self.webView];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(#"Back", #"")
style:UIBarButtonItemStyleDone
target:self
action:#selector(remove:)];
[self.navigationItem setLeftBarButtonItem: backButton];
UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
self.webView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
[self.webView loadRequest:request];
}
- (void)remove:(id)sender
{
[self removeFromParentViewController];
[self.webView stopLoading];
self.webView.delegate = nil;
}
I can't figure out why the button isn't there. Any help would be much appreciated!
Thanks.
Before add UIBarButtonItem to UINavigationBar, First change your UIWebView frame,
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width,self.view.frame.size.height - 50 )];
Here add (-50) to height of UIWebView.
And now, i just write code for how to add UIBarButtonItem to UINavigationBar:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:navBar];
UIBarButtonItem *Button = [[UIBarButtonItem alloc]
initWithTitle:#"MyButton"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(buttonAction)];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"This is My Title "];
item.rightBarButtonItem = Button;
item.hidesBackButton = YES;
[navBar pushNavigationItem:item animated:NO];
Here is Method name is buttonActionthat execute/call when you tapped on UIBarButtonItem.
-(void)buttonAction
{
//stuff of code;
}
remove these two lines from your code and check it,
UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
or you can add like this,
UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"remove"
style:UIBarButtonItemStyleDone
target:self
action:#selector(remove:)];
UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:#"remove"];
[item setLeftBarButtonItem:backButton];
[myBar setItems:[NSArray arrayWithObject:item]];
Richard, I understand what you want to achieve but I am not sure whether you are using a navigation controller already.
Case 1: using UINavigationController
If you are using Navigation Controller already then you don't need to add new Navigation bar.
If you are using NavigationController then show the use the bar from Navigation Controller and your code will work.
below two lines would not be required.
UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
Case 2: Not using UINavigationController
If you are not using UINavigationController then you are required to add a Bar and add Bar button in the navigation item of Bar.
UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"remove"
style:UIBarButtonItemStyleDone
target:self
action:#selector(remove:)];
UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:#"remove"];
[item setLeftBarButtonItem:backButton];
[myBar setItems:[NSArray arrayWithObject:item]];
and you need to remove following line
[self.navigationItem setLeftBarButtonItem:backButton];
It will work perfectly. Let me know if you find any issues.

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.

How to create this toolbar programmatically [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am really having a hard time creating a UIToolBar programmatically(no xib file). This is what I would like to create.
But till now, I feel I have not approached it in the right way. I have tried adding a barbuttonitem to it, but I cant create the effect as shown in the png. How should I go about it. Help needed.
Edit: I have added a bigger image
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"toolbar title" style:UIBarButtonItemStylePlain target:self action:#selector(onToolbarTapped:)];
NSArray *toolbarItems = [NSArray arrayWithObjects:spaceItem, customItem, spaceItem, nil];
[self setToolbarItems:toolbarItems animated:NO];
Create your toolbar with CGRECT to put it where you want
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(x, y, width, height)];
You can customize it with background image, title, text color, background color, ...
Next create your button
UIBarButtonItem *theButton = [UIBarButtonItem alloc] initWithTitle:#"button title" style:UIBarButtonItemStylePlain target:self action:#selector(selector:)];
Add it to yout toolbar :
NSArray *buttons = [NSArray arrayWithObjects: theButton, nil];
[myToolbar setItems:buttons animated:NO]
add your toolbar to the current view
[self.view addSubview:mytoolbar]
Just typed not tested, hope it'll help
ToolBar and buttons have custom images
- (void)viewDidLoad {
[super viewDidLoad];
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)];
[myToolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"toolbar_40.png"]] autorelease] atIndex:0];
UIBarButtonItem *barButtonDone = [self createImageButtonItemWithNoTitle:#"button_down.png" target:self action:#selector(closeMe:)];
UIBarButtonItem *barButtonShare = [self createImageButtonItemWithNoTitle:#"button_share.png" target:self action:#selector(actionShareButton:)];
UIBarButtonItem *barButtonFlexibleGap = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
myToolbar.items = [NSArray arrayWithObjects:barButtonDone,barButtonFlexibleGap,barButtonShare,nil];
[self.view addSubview:myToolbar];
[myToolbar release];
}
-(void)closeMe:(id)sender
{
NSLog(#"closeMe");
}
-(void)actionShareButton:(id)sender
{
NSLog(#"actionShareButton");
}
-(UIBarButtonItem *)createImageButtonItemWithNoTitle:(NSString *)imagePath target:(id)tgt action:(SEL)a
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [[UIImage imageNamed:#"button_slice.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
UIImage *buttonPressedImage = [[UIImage imageNamed:#"button_slice_over.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
CGRect buttonFrame = [button frame];
buttonFrame.size.width = 32;
buttonFrame.size.height = 32;
[button setFrame:buttonFrame];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
imageView.image = [UIImage imageNamed:imagePath];
[button addSubview:imageView];
[imageView release];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];
[button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return [buttonItem autorelease];
}
These png files need to be added
toolbar_40.png for toolbar image
button_down.png for button image
button_share.png for button image
button_slice.png for button background
button_slice_over.png for button background (pressed)
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar.backgroundColor = [UIColor clearColor];
Then add a UIBarButtonItem to toolbar.
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)];
myToolbar.barStyle = UIBarStyleBlack;
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
myLabel.text = #"Sperre aufheben";
myLabel.textColor = [UIColor whiteColor];
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.backgroundColor = [UIColor clearColor];
[myToolbar addSubview:myLabel];
[self.view addSubview:myToolbar];
[myLabel release];
[myToolbar release];
Seems like a better fit for a nav bar (at least if you are keeping with the spirit of what you are doing: IE adding a title to the bottom of the screen)
Anyway I created a simple test project and this is what I did to get the functionality pictured, you may have to insert it into the view/controller differently but it gets what you want relatively easy and doesn't use the button bar
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)];
[navBar setBarStyle:UIBarStyleBlack];
UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:#"Your Title"];
NSArray *array = [[NSArray alloc] initWithObjects:title, nil];
[navBar setItems:array];
[self.view addSubview:navBar];
}

Activity Indicator within UINavigationBar

I've read a few answers about this particular topic, but for some reason, my code doesn't seem to work. It's within the UISearchBarDelegate delegate method -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
UIBarButtonItem *activity = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[[self navigationItem] setRightBarButtonItem:activity];
[activity release];
[activityIndicator startAnimating];
Any help or suggestions are much appreciated!
I believe the designated initializer for UIActivityIndicatorView is initWithActivityIndicatorStyle:. Try creating activityIndicator like this.
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
try this
UIView *view_actviti = [[UIView alloc] initWithFrame:CGRectMake(6,310,59,32)];
UIActivityIndicatorView *act =[[UIActivityIndicatorView alloc] init];
act.frame=CGRectMake(0, 0, 30, 30);
UIBarButtonItem *actItem = [[[UIBarButtonItem alloc] initWithCustomView:view_actviti] autorelease];
[view_actviti addSubview:act];
self.navigationItem.rightBarButtonItem = actItem;
[act startAnimating];
try this:
UIActivityIndicatorView *activityIndicator =
[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
UIBarButtonItem * barButton =
[[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[[self navigationItem] setRightBarButtonItem:barButton];
[barButton release];
[activityIndicator startAnimating];
Also use this for stopping and removing activityIndicator:
[[self navigationItem] setLRightBarButtonItem:nil];
[activityIndicator stopAnimating];
[activityIndicator release];

Can you add a UIView above a UINavigationBar?

I have seen it done on some apps, where the navigation bar is actually smaller than the default 44px, and there is a UIView (which has functionality) above the nav bar...
I want more than a custom background image, which I did manage to figure out how to do, but I dont know where to start getting something like this done.
Any help is greatly appreciated :)
Mark
I found a sort of way to do this:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
UIImageView *back = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"logo.png"]];
[back setFrame: CGRectMake(0, 0, 320, 20)];
[tempView addSubview: back];
[[self view] addSubview: tempView];
[[self view] addSubview: navBar];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: #"Controls"];
[navBar pushNavigationItem:navItem animated:NO];
which seems to do the trick, although I cannot seem to figure out how to get this 'into' the navigationController so that back buttons work, at the moment I have to manually insert a leftBarButtonItem into the navItem, the back button never seems to show...
Yes you can,
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
navBar.navigationBar.layer.zPosition =-1;
self.view insertSubview:navBar atIndex:[[self.view subviews] count]];
[self.view insertSubview:tempView atIndex:[[self.view subviews] count]];