Currently I'm using my own custom images in UIBarButtonItems with the following code:
UIButton *profileBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35.0f, 35.0f)];
[profileBarButton setImage:[UIImage imageNamed:#"profile-barbutton.png"] forState:UIControlStateNormal];
[profileBarButton addTarget:self.navigationController action:#selector(toggleMenu) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:profileBarButton];
This works perfectly when I have a defined action to call, such as presenting a modalviewcontroller and turning on edit mode. However, I'm confused as to how I'd set the action for going back from one view to another, not modally? Is there a specific method I could call programmatically? Usually the navigationcontroller takes care of this...
for going back from one view to another, not modally you may write like this:
- (void) toggleMenu
{
if (self.navigationController.visibleViewController == self)
{
[self.navigationController popViewControllerAnimated: YES];
}
}
Use this code:
[self dismissViewControllerAnimated:YES completion:nil];
Check with this code...
MyViewController *vc = [[MyViewController alloc] initWithNibName:#"MyNib" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentModalViewController:nc animated:YES];
[vc release];
[nc release];
Then normally push code like...
OtherViewController *vc = [[OtherViewController alloc] initWithNibName:#"MyOtherNib" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
:)
Try this code:
UIImage *backButtonImage = [UIImage imageNamed:#"backButton.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBackBarItem;
In the #selector(back), "back" is the method that triggers the popup method of navigationcontroller. Like this:
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
Related
I can not add on NavigationBar backbutton. I want to perform this operation on a ViewController, I have tried different ways, but could not reach a solution. Do you need to use Backbutton NavigationController to use, or you can perform on a ViewController?
Waiting for your help.
If your ViewController is within a navigation controller, you should be able to do this:
[self.navigationController popViewControllerAnimated:YES];
Hide the default back button
[self.navigationItem setHidesBackButton:YES animated:YES];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"back_arrow.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 35, 23);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
- (void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}
Well, I have a tab based app with a navigationController for each tab. When I am at the root of each navigationController and I push a view, the animation runs perfect, but when I am on a pushed view and I want to pop it the navigationController gets animated but not the view. This is what I use to pop it:
[self.navigationController popViewControllerAnimated:YES];
and to push it:
[self.navigationController pushViewController:activityController animated:YES];
any suggestion?
EDIT:
I have a tableView in the root controller. Every time I select a row I run this code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ActivityViewController *activityController = [[ActivityViewController alloc] initWithNibName:#"ActivityViewController" bundle:nil];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:[UIImage imageNamed:#"BackButonItem"] forState:UIControlStateNormal];
[backButton setBackgroundImage:[UIImage imageNamed:#"BackButonItem_Pressed"] forState:UIControlStateHighlighted];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(15, 10, 55, 30)];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
activityController.navigationItem.leftBarButtonItem = backButtonItem;
activityController.navigationItem.hidesBackButton = YES;
[self.navigationController pushViewController:activityController animated:YES];
}
and this is my popBack method:
- (void) popBack
{
[self.navigationController popViewControllerAnimated:YES];
}
Did you try with
[self.navigationController popToViewController:self animated:YES];
It seems to have solved the problem for a lot of people.
I am using the below statement to define the transitionstyle
_viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
in the below code but the above statement is not flipping horizontally while presenting it modally
#property (nonatomic, assign) UIModalTransitionStyle *modalTransitionStyle;
#synthesize modalTransitionStyle;
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_viewController = [[ModalViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
UIBarButtonItem * button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
UIBarButtonItem * button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
[_viewController.navigationItem setLeftBarButtonItem:button animated:YES];
navigationController.navigationBar.tintColor = [UIColor brownColor];
_viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController presentModalViewController:_viewController animated:YES];
Anyone knows that why it is so. When everthing is coded programmtically.
Thanks for help though.
There's a lot of confusing naming going on here.
AFAICT, _viewController is the root view controller of your navigationController. But you reference self.viewController... is this the same view controller? If so, why are you not consistently using accessor methods? If not, it's not clear to me that you setting the modal transition style on the correct view controller ([self viewController] vs. _viewController).
(Btw, please work on formatting your code so that it displays in a more reasonable way when you paste it in.)
Wrong way to approach the problem. I suggest you to review your code in order to improve readability and maintainability.
Supposing that you've a viewcontroller with a target action associated with a button, something like this:
...
[aButton addTarget:self
action:#selector(dismissView:)
forControlEvents:UIControlEventTouchUpInside];
...
You can write a selector method, in order to display your new viewcontroller as follow:
- (void)dismissView:(id)sender {
SecondViewController *secondVC = [[SecondViewController alloc] init];
secondVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:secondVC animated:YES];
[secondVC release];
}
Modalviewcontroller is showing gap after loading. Gap is after staus bar which moved modalviewcontroller down and gap shows me main window. So how i can remove this gap which is showing between status bar and modalviewcontroller. There is no interface builder involved. Creating everything programmtically. Help for this will be really appreciated.
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
[button addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithCustomView:button];
- (void)displayModalViewaction: (id) sender
{
self.viewController = [[Infoviewcontroller alloc] init];
UINavigationController *navigationController=[[UINavigationController alloc] init];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[navigationController pushViewController:_viewController animated:YES];
[self.view addSubview:navigationController.view];
}
Thanks
In your _viewController, you need to update this code if you want to hide the gap -
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
But ideally it needs to be fixed by-
[self presentModalViewController:navigationController animated:YES];
Not sure why it is not working for you.
Assuming this code is in a UIViewController you probably want to do something like this.
self.viewController = [[[Infoviewcontroller alloc] init] autorelease];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
By adding the view frame size [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; fixed the gap between status bar and modal view.
Below is my coding
// Navigation logic may go here -- for example, create and push another view controller.
NextViewController *nextViewController =
[[NextViewController alloc] initWithNibName:#"NextViewController" bundle:nil];
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];
UIImage *backImage=[UIImage imageNamed:#"backbutton.png"];
UIBarButtonItem * backBarButtonItem = [[UIBarButtonItem alloc]
initWithImage:backImage style:UIBarButtonItemStylePlain target:nil
action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
}
Here, from my own project. (I am doing this in viewDidLoad)
UIImage *image = [UIImage imageNamed:#"btn-custom-back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:#selector(popView) forControlEvents:UIControlEventTouchUpInside];
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
[buttonView addSubview:button];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
[buttonView release];
self.navigationItem.leftBarButtonItem = backItem;
[backItem release];
And the method:
- (void)popView {
[self.navigationController popViewControllerAnimated:YES];
}
The modification to the back button has to be done before you push the new view controller onto the navigation stack.
So just move your code around to look like this:
UIImage *backImage=[UIImage imageNamed:#"backbutton.png"];
UIBarButtonItem * backBarButtonItem = [[UIBarButtonItem alloc]
initWithImage:backImage style:UIBarButtonItemStylePlain target:nil
action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
NextViewController *nextViewController =
[[NextViewController alloc] initWithNibName:#"NextViewController" bundle:nil];
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];