I'm trying to change my navbar's background with the code:
- (void)viewDidLoad
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"header_bg_smooth.png"] forBarMetrics:UIBarMetricsDefault];
[super viewDidLoad];
...
}
It works, but the problem I'm having is that the change first takes effect when you go BACK the the view where this code is located. So it you go to this view the navbar is not changed but if you go further and then go back to this view the changes take effect.
Does anyone have any clue what the issue might be?
Thanks in advance
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"header_bg_smooth.png"] forBarMetrics:UIBarMetricsDefault];
...
}
viewDidLoad only fired once when the view is loaded. To run the code every time when you see the view, put the codes in viewDidAppear or viewWillAppear (depends on your usage)
This answer helps you choose from viewDidLoad , viewDidAppear or viewWillAppear.
Use setBackgroundImage:forBarMetrics: method: and write this method in viewWillAppear
[navbar setBackgroundImage:[UIImage imageNamed:#"navbar"]
forBarMetrics:UIBarMetricsDefault];
this will help you a lot....
try this..
UIView *backgroundView = ...
[navigationBar insertSubview:backgroundView atIndex:0];
Also see this link....setting-custom-background-for-uinavigationbar-what-will-work-on-ios5-and-ios4-too
Related
In iOS 7 there's the new swipe to pop gesture: You swipe from left to right on the left side of your screen and the UINavigationController pops back to the previous UIViewController.
When I create a custom back button like this, the swipe to pop gestures doesn't work anymore:
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStyleBordered target:self action:#selector(navigateBack)];
[customBackButton setBackButtonBackgroundImage:barBackBtnImg forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[customBackButton setBackButtonBackgroundImage:barBackBtnImgHighlighted forBarMetrics:UIBarMetricsDefault];
self.navigationItem.backBarButtonItem = customBackButton;
How can I use a custom back button and have the native swipe to pop gesture?
Update:
That's what's happening in navigateBack:
- (void)navigateBack {
[self.navigationController popViewControllerAnimated:YES];
}
There is no need to add your own gesture recognizer. The UINavigationController already does that for you.
You need to set the delegate for the interactivePopGestureRecognizer before enabling it.
Do the following two things:
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
[self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
Just add the following line of code:
[self.navigationController.interactivePopGestureRecognizer addTarget:self action:#selector(handleGesture:)];
You can add your own UIGestureRecognizer and pop the UIViewController yourself. See the docs for further info.
I use
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"nav_back.png"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"nav_back.png"]];
[UIBarButtonItem.appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -64) forBarMetrics:UIBarMetricsDefault];
To avoid crashes you have to be careful how you add and remove your custom back selector. The reason is that the navigation controller stays around while you pushing popping controller.
As already stated after adding your custom back button+selector, you should do the following in viewDidApear.
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)])
{
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
[self.navigationController.interactivePopGestureRecognizer addTarget:self action:#selector(navigateBack)];
}
Then in viewWillDisapear do
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)])
{
[self.navigationController.interactivePopGestureRecognizer removeTarget:self action:#selector(performCompletion)];
}
The timing of these calls is a key. You can run into crashes otherwise, see more details on the reason in here
There is a new gesture recognizer UIScreenEdgePanGestureRecognizer. You can add it to your view and handle respectively (call navigateBack), replicating view controllers navigation behaviour.
What did you do in "navigateBack" ?
Use this method like this :
- (void)navigateBack
{
[self.navigationController popViewControllerAnimated:YES];
}
try adding this into the custom back button
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
I have a MFMailComposeViewController that I'm presenting and I want to clear the image from the navigationBar.
On iOS 5, this works fine:
[self.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
But on iOS 6, this has no effect. How could I do this?
Thank you!
You jus hide the navigation bar..
self.navigationController.navigationBarHidden = YES;
or
[self.navigationController setNavigationBarHidden:YES animated:animated];
I'm sorry, I have to answer my own question. I made an error: I had set the UINavigationBar app-wide by doing
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"bar01.png"] forBarMetrics:UIBarMetricsDefault];
which had the effect of causing all navbars to have the bar01. Removing this line made it so that the MFMailComposer navbar was the default color with no need to nil out the image.
I am using a custom Switch view controller and you would hope when switching between different views the "view did load" function or "view did finished loading" functions run but they do not.
Here what I am using:
- (IBAction)gotoKeyboardViews:(id)sender
{
YellowViewController *yellowController =
[[YellowViewController alloc]
initWithNibName:#"YellowViewController"
bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
[buttonKeyboard removeFromSuperview];
buttonStart = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonStart.frame = CGRectMake(117,413, 103, 37);
[buttonStart setTitle:#"Restart" forState:UIControlStateNormal];
[buttonStart addTarget:self action:#selector(gotoBlueView:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonStart];
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
}
gotoKeyboardViews suppose to switch the views To YellowView From BlueView...But I think I am a little bit off about implementing the navbar. Please don't tell me to go with the navbar because I don't like their rigidness in design.
The viewWillAppear method is executed as soon as the view gets active again. Maybe that's the hook you are searching?
Reference: Apple UIViewController Class Reference
those methods are only called when the nib file is loaded or the if you've override loadView. They will not get called again unless a new view controller is instantiated.
I have a customized navigation bar with a image background. I do want to show the title on the background but I need its text for the back button in the next view.
self.title=#"" will not put (naturally) in the back button the previous title.
Based on the suggestion of pheekicks, I found a tip to do it:
UILabel *label = [[UILabel alloc] init];
self.navigationItem.titleView = label;
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor clearColor], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil]];
if you want to switch between view controllers, and you want to hide title text of navigation bar, that still appear back button, in root view controller, you should override this method:
- (void) viewDidAppear:(BOOL)animated{
self.navigationItem.titleView = m_anyViewYouWant;
}
This is OK!
This is a fairly old post. But I got around this problem by setting the title in the viewWillDisappear method, so it does not show when the view is displayed, but shows in subsequent views back button.
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self setTitle:NSLocalizedString(#"mytext", nil)];
}
Try:
self.titleView.hidden = YES;
I'm using this line to hide the navigation bar on viewDidLoad:
self.navigationController.navigationBarHidden=YES;
I'm creating a simple modal ViewController. I'm creating a nib with a button and on that button press calling a method to display modal viewController in which I'm creating the viewController and a button inside it like this.
UIViewController *modalViewController = [[UIViewController alloc]initWithNibName:nil bundle:nil];
modalViewController.view.backgroundColor = [UIColor redColor];
modalViewController.;
UIButton *btnDismissViewController = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnDismissViewController.frame = CGRectMake(60, 160, 150, 50);
[btnDismissViewController setTitle:#"DISMISS" forState:UIControlStateNormal];
[btnDismissViewController addTarget:self action:#selector(dismissViewCOntroller) forControlEvents:UIControlEventTouchUpOutside];
btnDismissViewController.backgroundColor = [UIColor grayColor];
[modalViewController.view addSubview:btnDismissViewController];
[self presentModalViewController:modalViewController animated:YES];
This view is appearing properly but after pressing the button on modalViewController, the target method for dismissing the modalViewController isn't called. I'm definitely missing something obvious but not getting what. Can anybody please help?
Thanx in advance.
I agree with Ole's comment... additionally, make sure you are dismissing it within your dismissViewCOntroller method similar to this:
[self.parentViewController dismissModalViewControllerAnimated:YES];
I think there might be a typo in your code, dismissViewCOntroller seems like it should be dismissViewController, but maybe that was intentional, and the control state should be UIControlEventTouchUpInside.