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;
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;
In my application i want to change the tab bar controller color,How to assign a custom color to the tab bar controller lik uinavigation bar in ios6?Can any one give me some refrences?
Put this on app delegate:
UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;
tabBarController.view.tintColor = [UIColor redColor];
It's better than the first answer because it also changes the edit view tint color.
You can use the setTintColor option as described
[tabbarController.tabBar setTintColor:[UIColor greenColor]];
or you can set the background image
[tabbarController.tabBar setBackgroundImage:[UIImage imageNamed:#"tab_bg.png"];
if your TabBarController is defined int the AppDelegate you may need additional coding to access it.
First to set the background image
[[[[(UITabBarController *)[[(AppDelegate *)[UIApplication sharedApplication].delegate window] rootViewController]tabBar]setBackgroundImage:[UIImage imageNamed:#"tab_bg.png"]]]];
Second to set the tintcolor if required
[[[[(UITabBarController *)[[(AppDelegate *)[UIApplication sharedApplication].delegate window] rootViewController]tabBar]setTintColor:[UIColor redColor]]]];
dont forget to import your AppDelegate.h file.
You can use this call
tabbarController.tabBar.tintColor = [UIColor redColor];
To change only tabBar color, you can achieve by this:
tabbarController.tabBar.tintColor = [UIColor redColor];
But, for more, you need to create Custom TabBar and can do with it like color change, custom tabbar icon changes, etc.
Hope, it will be helpful to you.
Cheers.
Approach that worked for me (tested in iOS6) is:
[[UITabBar appearance]setTintColor:[UIColor redColor]];
in AppDelegate.h file in method application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions. Try this if it's still actual. I see the question still stays unsolved.
I'm trying to create a custom navigation bar with title and two custom baritems.
What I've done so far:
1. subclass a UINavigationBar
2. override -(void) drawRect:(CGRect)rect method, to draw image as a background
Now I get stuck when making right bar button item.
In my CustomBar class I've overriden pushNavigationItem method as follows:
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated {
[super pushNavigationItem:item animated:NO];
item.rightBarButtonItem.customView.frame = CGRectMake(250, 5, 60,30);
}
and in my view controller I've done something like:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationController.navigationItem.rightBarButtonItem = barButton;
[self.navigationController.navigationBar pushNavigationItem:self.navigationItem animated:NO]
but I always get SIGABRT when calling
[super pushNavigationItem:item animated:NO];
If I leave that line out, everything runs fine, but there is no button in the navigation bar.
What is the proper way to add custom bar button item to custom navigation bar?
EDIT:
For clarification, I've added an image.
This is what I need.
I'm trying to create the logout button on the right
Have you tried removingnavigationController while setting the item ? I used to do that for setting the title, never used to get the title.
Try [[self navigationItem] setRightBarButtonItem:yourButton];
In your case :
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = barButton;`
I am trying to add a title to my nav bar which I have added programmatically. I know i can create a label and add it as a subview, but then I need to add tons of configurations for it to be placed correctly like in a typical nav bar title. Is there any other way to do that than this:
- (void)viewDidLoad
{
[super viewDidLoad];
//Adding navBar programmatically
CGFloat width = self.view.frame.size.width;
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,width,52)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//Adding title programmatically
UILabel *lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(10,10,80,30)];
lblTitle.text = #"Title";
[navBar addSubview: lblTitle];
[self.view addSubview:navBar];
}
And if not (sorry for another question, just think it relates) how do I set up my lblTitle to look like a normal nav bar title that you will find in most of the apps.
Thanks!
If you are adding the navigation bar by programmatically means, use this
(void)viewDidLoad {
[super viewDidLoad];
self.title = #"yourTitle";
}
You can also use
self.navigationItem.title = #"yourTitle";
You can use
navigationBar.topItem.title = #"Your Title";
The best answer to your question is just to use a UINavigationController to get its navigation bar for free, but if for some reason you can’t do that, you’ll need to use a custom UILabel and play with its properties until you match the style of the built-in one.
If a simple UILabel does not satisfy you, then yes, you need tons of configuration or you can do that in Interface Builder to polish your view. Instead of adding it as a subview, I suggest that you set it as navigationItem.titleView in your -(void)viewDidLoad.
I have created one custom view and added a web view as subview of that custom view. And i have created one navigation bar in programmatically. Now i want to create one Left or Right navigation bar buttons in that view.
navigBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navigBar.tintColor = [UIColor blackColor];
[self.view addSubview:navigBar];
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonSystemItemAction target:self action:#selector(homeButtonAction)];
navigBar.rightBarButtonItem = homeButton; // it's not supports
How can i create navigation bar item in the custom navigation bar?, i want to remove the custom view. So please Help me out!
Thanks.
On your navigationbar reference you could invoke the two methods:
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated
- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated
In your custom view controller class write something like this:
self.navigationItem.leftBarButtonItem = yourButton;
self.navigationItem.rightBarButtonItem = yourSecondButton;
So you could write something like this:
[myNavBarReference pushNavigationItem:self.navigationItem animated:NO];
rightBarButton is defined for UINavigationItem. See the documentation for the UINavigationItem and for the UINavigationBar (Specially Adding Content to a Navigation Bar).
The method to use is - (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated
See http://developer.apple.com/library/ios/documentation/uikit/reference/UINavigationItem_Class/Reference/UINavigationItem.html
You would invoke methods to self.navigationItem.