Navigationbar right button is not placed correctly - ios

I implemented a UITabBarController with 4 items.
I set the initial ViewController from AppDelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
HomeTabBarViewController* homeVC = [[HomeTabBarViewController alloc] init];
[self.window setRootViewController:homeVC];
[self.window makeKeyAndVisible];
return YES;
}
Here is HomeTabBarViewController ViewDidLoad Code:
UIViewController *view1 = [[UIViewController alloc] init];
[view1.view setBackgroundColor:[UIColor redColor]];
UIViewController *view2 = [[UIViewController alloc] init];
UINavigationController * centerNav=[[UINavigationController alloc]initWithRootViewController:view1];
HomePagingViewController * view3=(HomePagingViewController*)[storyboard instantiateViewControllerWithIdentifier:#"HomePagingViewController"];
UINavigationController * centerNav3=[[UINavigationController alloc]initWithRootViewController:view3];
UIViewController *view4 = [[UIViewController alloc] init];
UINavigationController * centerNav4=[[UINavigationController alloc]initWithRootViewController:view4];
[view2.view setBackgroundColor:[UIColor greenColor]];
[view3.view setBackgroundColor:[UIColor purpleColor]];
[view4.view setBackgroundColor:[UIColor grayColor]];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:centerNav];
[tabViewControllers addObject:view2];
[tabViewControllers addObject:centerNav3];
[tabViewControllers addObject:centerNav4];
[self setViewControllers:tabViewControllers];
view1.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Home"
image:[UIImage imageNamed:#"home_tab_item.png"] selectedImage:[UIImage imageNamed:#"home_tab_item_active.png"]] ;
view2.tabBarItem =
[[UITabBarItem alloc] initWithTitle:#"Bookmark"
image:[UIImage imageNamed:#"bookmark_tab_item.png"] selectedImage:[UIImage imageNamed:#"bookmark_tab_item_active.png"]];
view3.tabBarItem =
[[UITabBarItem alloc] initWithTitle:#"Paper"
image:[UIImage imageNamed:#"paper_tab_item.png"] selectedImage:[UIImage imageNamed:#"paper_tab_item_active.png"]];
view4.tabBarItem =
[[UITabBarItem alloc] initWithTitle:#"More"
image:[UIImage imageNamed:#"menu_tab_item.png"] selectedImage:[UIImage imageNamed:#"menu_tab_item_active.png"]];
In HomePagingViewController ViewDidLoad I setUp a Navigation as in code
-(void)navigationSetUp{
self.navigationController.navigationBar.translucent=NO;
UIBarButtonItem * settingBtn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"settings.png"] style:UIBarButtonItemStylePlain target:self action:#selector(menuHandler:)];
UIBarButtonItem * searchBtn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"settings.png"] style:UIBarButtonItemStylePlain target:self action:#selector(menuHandler:)];
self.navigationItem.leftBarButtonItem = settingBtn;
self.navigationItem.rightBarButtonItem = searchBtn;
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ie_New_logo.png"]];
}
After this when I changed the tab for HomePagingViewController the navigation right button not placed correctly there is no margin from right.
Refer Image :
Please Help.

I resolved the issue , there is no issue with the code . Actually for Epaper we using third party SDK So that this problem occur.I Removed SDK & the code is working fine now.

you should think UIBarButtonSystemItemFixedSpace effect.
eg:
UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
--
Why use a system navigation bar ?Custom navigation bar is nice!like:https://github.com/chenliangloveyou/EasyNavigation

Related

blank Toolbar from setItems

My UINavigationController toolbar is blank and no items show up on it. Here is my code
self.navigationController.toolbarHidden = NO;
UIToolbar* toolbar = self.navigationController.toolbar;
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStylePlain
target:self
action:#selector(buttonPushed)]];
[toolbar setItems:items animated:YES];
That's not the proper way to setup the toolbar. UIViewController has a toolbarItems property. The nav controller will use that property to automatically populate its toolbar.
Your code should be:
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *btnTest = [[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStylePlain
target:self
action:#selector(buttonPushed)]];
self.toolbarItems = #[ btnTest ];

putting labels, buttons on the navigation bar iOS

I have created custom navigation controller,
I want to be added, a date at the left, a back button on the right and the title next to back button.
I tried to add one label, but it does not work. Please show me a way
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.text = #"My Text";
[self.navigationController.navigationBar addSubview:navLabel];
[self.view addSubview:naviBarObj];
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,8,200,30)];
navLabel.text = #"My Text";
navLabel.textColor = [UIColor redColor];
[naviBarObj addSubview:navLabel];
[navLabel setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:naviBarObj];
try this it will work .It works for me :)
add custom view in UIToolbar
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f,190.0f, 44.01f)];
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] init];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
[tools setItems:buttons animated:NO];
UIBarButtonItem *buttons_ = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = buttons_; //same to leftBarButtonItem
You can also achieve this from Interface Builder
1. First add a UIView which will look like following
2. Then add a UILabel inside the UIView which will look like following
3. In the Center It will look like following
Now add necessary constraint to meet your requirement.
You need to add navigation item to the navigation bar. One per each navigation level. So you should have at least one of them.
** update **
UINavigationItem * myNavItem = [[UINavigationItem alloc] initWithTitle:#"Some title"];
/*
* add buttons and labels
*/
[self.navigationController.navigationBar pushNavigationItem:myNavItem animated:NO];
You need to change the titleView of the topItem. Like this:
naviBarObj.topItem.titleView = navLabel;
** update: Complete code (tested and working) **
UINavigationBar *naviBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(.0, .0, 320.0, 44.0)];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.titleView = titleLbl;
[naviBar pushNavigationItem: navItem animated: NO];
[navItem release]; // you don't need this for ARC
[self.view addSubview: naviBar];
[naviBar release]; // you don't need this for ARC

UIBarButtonItem not firing its action method

In viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES animated:NO];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Articles" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Source" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Aa" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Rabbit" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];
Method:
- (void)backButtonTapped {
[self.navigationController popViewControllerAnimated:YES];
}
I have a breakpoint on the call inside the method but it never gets called. Why is this method never getting called?
I had a UITapGestureRecognizer on the whole view that intercepted the tap on the UIBarButton. I solved it thanks to this answer, which basically stopped the UITapGestureRecognizer from beginning unless it was outside of the UIToolBar.
Your code is not complete and I dont know if you didn't implement it or just didn't wrote it here, so just to make sure, this should work:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Articles" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)]];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];
Try like this. Its working fine.
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]initWithTitle:#"back"style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
[self.view addSubview:toolbar];

How to create Navigation using ToolBar button

I have created a Toolbar button and given action() to navigate to other ViewController.But the code is not working for Navigation.And not able to assign a image to the button.How can i solve this?
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setToolbarHidden:NO];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 418, 320, 44);
[self.view addSubview:toolbar];
[toolbar release];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"update" style:UIBarButtonItemStyleBordered target:self action:#selector(updateAddress:)];
NSMutableArray *toolbarItems = [NSMutableArray arrayWithObjects:customItem, nil];
[toolbar setItems:toolbarItems animated:NO];
}
-(void)updateAddress{
DisplayMapViewController *updateView=[[DisplayMapViewController alloc]init];
UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:updateView];
[self.navigationController popToViewController:navi animated:YES];
}
First you should set the selector right, you should take out the : from the selector because you don't have parameters for the updateAddress method
should be like this
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"update" style:UIBarButtonItemStyleBordered target:self action:#selector(updateAddress)];
Then You need to initialize the DisplayMapViewController with its NibName
DisplayMapViewController *updateView=[[DisplayMapViewController alloc]initWithNibName:#"DisplayMapViewController" bundle:nil];
and then just pop to the view you are intended to.
[self.navigationController popToViewController:updateView animated:YES];
if you have already rootViewController then you could just popToRootViewController

UIToolbar creation

i have created a toolbar as below shown code and now i dont knw how to add the items to it.
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 418, 350, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[[UIBarButtonItem alloc] init] autorelease]];
[toolbar setItems:items animated:YES];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}
Try this code
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"Item" style:UIBarButtonItemStyleBordered target:self action:#selector(action:)];
NSArray *items = [NSArray arrayWithObjects: customItem, nil];
[toolbar setItems:items animated:NO];
[customItem release];
You can also refer to this link:
How to create this toolbar programmatically
Please do google search and Stack Overflow search before posting.
use
[self setToolbarItems: buttons animated:NO];
as u want to set toolbaritems and uiviewcontroller has toolbar itself.

Resources