I want to put an 'i' button on the nav bar (on all the screens in which the nav bar appears). Touching on that button should bring up a view (perhaps modal) in which I can have controls. How do I put the button on the nav bar, and what how will I execute the call back?
Info Button Code:
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(infoButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[self.navigationItem setLeftBarButtonItem:modalButton animated:YES];
[modalButton release];
The code above calls method -(void)infoButtonAction. Add this to all of your ViewControllers or just create one .nib and use that for all of your views.
You can also achieve it on storyboard.Drag a UIButton to you navigation bar instead of the usual UIBarButtonItem. You will see that your unbutton will be "nested" inside the bar button item which can be expanded.
Then you can change your button's type property to info light | dark from the attribute inspector.
Related
I have created a utility app that links by button to another xib called scene - I am trying to create a navigation control for that link. When the button is clicked to then have a 'back' button on my scene xib. I don't wish to have a navigation bar visible on the Main View Controller or the Flipside View Controller. I'm quite new to iOS and I have no idea how to do this?
Would it maybe just be better to have a button going back to menu on a custom HUD? I don't know if that can be done?
Thank you for any help in advance, and thank you for your time
you could create a custom UINavigationBar on your scene xib, and add the custom back button to it if you don't want to create NavigationController , alternate would be that you could just make your first view as NavigationController and push the Scene view over it and it will brings the back button on the child view which is scene, keep your navigationBar hidden when you are on MainViewController and show only on scene view.
For hide UINavigationBar:
[[self navigationController] setNavigationBarHidden:YES animated:YES];
And Your can crate Custom UIButton and put anywhere (As Your requirement).
and in its method, Write code for go back to UIViewController (previous UIVieController).
Such like,
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack addTarget:self action:#selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
btnBack.frame = CGRectMake(10, 10.5, 36, 39); // change it , As your wish
[btnBack setBackgroundImage:[UIImage imageNamed:#"MMBack.png"] forState:UIControlStateNormal];
[self.view addSubview:btnBack];
// call method of UIButton
-(void)goBack:(UIButton *) Sender
{
[self.navigationController popViewControllerAnimated:YES];
}
I have a UINavigationController and I have to keep the the default back button "the back arrow style" I just want to ask if I can change the back button action without build new one and change its style
AFAIK you cannot change the action of the default back button itself but you can place a UIBarButtonItem as leftBarButtonItem there and assign your own action.
If there is a leftBarButtonItem defined then this is shown and not the default back button.
However, keep the GUI guidelines in mind when doing tricks like this.
No. If you want a custom back button, you have to create a custom UIBarButtonItem, then assign it to the appropriate property:
self.navigationItem.backBarButtonItem = myCustomBackItem;
The back button in UINavigationBar is generated automatically as soon as u Push a new UIView. In order for you to customize the Back button is to Create a new UIToolBar + a UIBarButtonItem with custom view.
Code below is the sample to use a custom UIBarButtonItem in UIToolBar.
// create button
UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape!
[backButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
// create button item -- possible because UIButton subclasses UIView!
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
// add to toolbar, or to a navbar (you should only have one of these!)
[toolbar setItems:[NSArray arrayWithObject:backItem]];
navItem.leftBarButtonItem = backItem;
Link below is the design of iOS buttons in PSD format for further modifications.
http://www.chrisandtennille.com/pictures/backbutton.psd
You can make custom button and can make action on it but you can not change default backButton action.....
self.navigationItem.leftBarButtonItem = getBackBtn;
The UINavigationController sends a message to it's delegate when it pushes and pops a ViewController.
You can find out when the back button gets pressed by implementing the following and adding <UINavigationControllerDelegate> in your .h file
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.delegate = self;
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
self.navigationController.delegate = nil;
}
-(void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated{
//Test here if the View Controller being shown next is right below the current
// ViewController in the navigation stack
//
//Test by:
// 1. comparing classes, or
// 2. checking for a unique tag that you previously assigned, or
// 3. comparing against the [navigationController viewControllers][n-2]
// where n is the number of items in the array
if ([viewController isKindOfClass:NSClassFromString(#"ViewControllerClassThatGetsPushedOnBACK")){
//back button has been pressed
}
if (viewController.tag == myUniqueTagIdentifier){
//back button has been pressed
}
if ([navigationController.viewControllers[navigationController.viewControllers.count-2]==viewController]){
//back button has been pressed
}
}
Apple Docs UINavigationController Class Reference:
The root view controller is at index 0 in the array, the back view
controller is at index n-2, and the top controller is at index n-1,
where n is the number of items in the array.
I have problem in Navigation Controller. I can't create dynamic buttons on Navigation Bar.
I have used Navigation Bar. Inside this, I have one TabBar.
And Inside TabBar, I have one more Navigation Controller.
I can create buttons on that. But I want Dynamic buttons on parent Navigation controller of TabBar.
For Button It's not working.
My Code:
UIBarButtonItem *searchBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(btnClick:)];
//[self.navigationItem setRightBarButtonItem:searchBtn];
[app.navigationController.navigationItem setRightBarButtonItem:searchBtn];
[searchBtn release];
For Hide the parent Navigation Controller, It's working.
app.navigationController.navigationBarHidden =YES;
How can I solve this problem.?
if i understand you correctly you should change this
[app.navigationController.navigationItem setRightBarButtonItem:searchBtn];
[searchBtn release];
into
[app.navigationItem setRightBarButtonItem:searchBtn];
[searchBtn release];
I would like to center a button in my navigationBar, which is currently defined as a rightBarButtonItem :
UIBarButtonItem *audioBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(toggleAudioPlayback:)];
self.navigationItem.rightBarButtonItem = audioBtn;
[audioBtn release];
You can use the .titleView property of your navigation item - but you'll need to create your own assets for this (you won't be able to use a UIBarButtonItem). From Apple's docs:
...(titleViews) can contain buttons. Use
the buttonWithType: method in UIButton
class to add buttons to your custom
view in the style of the navigation
bar. Custom title views are centered
on the navigation bar and may be
resized to fit.
I want to add a Bar button to the Navigation bar on a Navigation based app, the bar however is on the MainWindow and has no code behind so I Can't connect it to an IBAction.
how do i go about connecting it to an action ?
You can add the button to the navigation bar programmatically like this and also connect it up to an action with the init code:
UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:<UIBarButtonStyle that you want> target:self action:#selector(<some_action>:)];
self.navigationItem.rightBarButtonItem = button;
[button release];