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.
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'm writing an iOS 6 app and I want to set a title view that will show for all of my views, unless I specify otherwise. I've tried [[UINavigationBar appearance] setTitleView:view] and [[UIBarButtonItem appearance] setTitleView:view], but neither have worked.
Note: this title view cannot be a label, it must be a button.
Any ideas?
Are you using a UINavigationController?
If so, use something like the following to set the background image and font:
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:22.0], UITextAttributeFont,nil]];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navBar.png"] forBarMetrics:UIBarMetricsDefault];
If you only want to apply the changes to Navigation Bars in certain specific subclasses of UIViewController, then use something like the following:
[[UINavigationBar appearanceWhenContainedIn:[VCSubclassToChangeAppearance class], nil]
setBackgroundImage:[UIImage imageNamed:#"navBar.png"]
forBarMetrics:UIBarMetricsDefault];
You can also use this method for creating exceptions to the rule e.g. set one appearance for all, except one in particular.
If you want to change the appearance of the standard buttons on the nav bar, do something like this:
UIImage *standardBackgroundImage = [[UIImage imageNamed:#"navBarButton.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 8, 0, 8)];
[[UIBarButtonItem appearance] setBackgroundImage:standardBackgroundImage
forState:UIControlStateNormal
style:UIBarButtonItemStyleBordered
barMetrics:UIBarMetricsDefault];
In this example, every nav bar button (that's not a back button) and is of the UIBarButtonItemStyleBordered variety, will use the image. You'll need to work on the UIEdgeInsets for your particular button image.
You could try adding a subview (UITextField) to your navigation bar:
UITextField *titleField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100.0, 60.0)];
titleField.text = #"a title";
titleField.center = self.navigationController.navigationBar.center;
[self.navigationController.navigationBar addSubview:titleField];
You can define a global variable after that set title for each viewController by command:
self.title = kDefaultTitle
If you use story board, you can set it on each viewController in Title properties at right bar
Final: I think you can automatically set all title of view (exclude you attack to core)
Because UIBarButtonItem of different view controllers will change the title view of the navigation bar, you can not set the title view globally. You can use #Jake approach or subclass UIViewController in which you can override the setTitle method. And use that class as the superclass of all your own view controllers.
You can put any view into titleView in any ViewController which is wrapped in UINavigationController by following code.
self.navigationItem.titleView = yourView;
Try this one.
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
I'm trying to set a custom shadow image for the navigation bar in my table views, but it's only showing in some views. I've created a super class to set the styles for my table views.
- (void)viewDidLoad
{
[super viewDidLoad];
// Set navigation bar background
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigationbarbackground.png"] forBarMetrics:UIBarMetricsDefault];
// Set navigation bar shadow imag
[self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:#"navigationbarshadow.png"]];
In the view I see at starting my app, no shadow is showed. But when I touch the [+] button in my navigation bar to open my 'add new item' table view, it does show a shadow.
Could someone point me in the right direction here?
you need to set custom backgroudImage for UINavigationBar, then the shadowImage can work.
The Appearance proxy should work.
Just call it somewhere (e.g. in your AppDelegate) upon startup.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self customizeAppearance];
return YES;
}
- (void) customizeAppearance
{
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationbarbackground"] forBarMetrics:UIBarMetricsDefault];
// Set the shadow image for *all* UINavigationBars
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"navigationbarshadow.png"]];
//add other appearance stuff here...
}
However if you create a storyboard with multiple UINavigationController's in it and a bunch of segue's pushing navigation controller's you might get a corrupt view controller structure which might be the problem here.
Another possible issue might be the Clip Subviews option of a Navigation Bar somewhere in your nib file or you storyboard. Make sure it is turned off if you want the shadow (image)!
By the way, if you use imageNamed you don't need to include the file extension.
Try this !
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"navbar-iphone.png"]];
This does not work
[self.navigationController setNavigationBarHidden:YES animated:YES];
when I have this barstyle
rootNavigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
Is it any reason why it shouldn't animate away? Are there any fixes to this?
EDIT
This is NOT working:
[rootNavigationController.navigationBar setBarStyle:UIBarStyleBlack];
[rootNavigationController.navigationBar setTranslucent:YES];
The UIBarStyleBlackTranslucent is deprecated. Use UIBarStyleBlack and set the translucent property to YES instead.