ios why the buttonItem not show? - ios

UIMapViewController *mapViewContr = [[[UIMapViewController alloc] initwithClientKey:clientKey] autorelease];
UINavigationController *nc = [[[UINavigationController alloc] initWithRootViewController:mapViewContr] autorelease];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 44, 44);
[button setImage:[UIImage imageNamed:#"info2.png"] forState:UIControlStateNormal];
UIBarButtonItem *lbutton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
nc.navigationItem.leftBarButtonItem = lbutton;
above codes not work, the leftBarButtonItem not show.but when i set leftBarButtonItem in function: UIMapViewController.vieDidLoad , the leftBarButtonItem show . why?
the flowing codes is in UIMapViewController.vieDidLoad
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(0, 0, 44, 44);
// [button setBackgroundImage:[UIImage imageNamed:#"info2.png"] forState:UIControlStateNormal];
[_button setImage:[UIImage imageNamed:#"info2.png"] forState:UIControlStateNormal];
UIBarButtonItem *lbutton = [[[UIBarButtonItem alloc] initWithCustomView:_button] autorelease];
self.navigationItem.leftBarButtonItem = lbutton;

In the first block of code you are calling:
nc.navigationItem.leftBarButtonItem = lbutton;
This should be:
mapViewContr.navigationItem.leftBarButtonItem = lbutton;
You always want to reference the navigationItem of the view controller, not the navigation controller.

Related

IOS 11 Navigation Bar Button Item

We were using the below code to show leftbarbutton item in our navigation bar in IOS 10 SDK and XCode 8, however when we updated to xcode9 and ios 11 sdk. The left bar button item doesnt show. I searched through the web , but couldnt find any solution to that.
Does anyone know any answer?
UIBarButtonItem *cancelButton = nil;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"11")) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
//button.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
//button.contentEdgeInsets = UIEdgeInsetsMake(13, 8, 13, 8);
NSLayoutConstraint *widthConst = [button.widthAnchor constraintEqualToConstant:32.0];
NSLayoutConstraint *heightConst = [button.heightAnchor constraintEqualToConstant:32.0];
[button setImage:[UIImage imageNamed:#"cross"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(hide) forControlEvents:UIControlEventTouchUpInside];
cancelButton = [[UIBarButtonItem alloc] initWithCustomView:button];
widthConst.active = YES;
heightConst.active = YES;
} else {
cancelButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"cross"] style:UIBarButtonItemStylePlain target:loginRouter action:#selector(hide)];
}
cancelButton.tintColor = [UIColor whiteColor];
loginViewController.navigationItem.leftBarButtonItem = cancelButton;
loginRouter.loginViewController = loginViewController;
CardNavigationController *navigationController = [CardNavigationController darkBlueNavigationControllerWithClient:client];
navigationController.viewControllers = #[ loginViewController ];
navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[onViewController presentViewController:navigationController animated:YES completion:nil];
And you can find the result in ios 11 below
Check the following example
UIImage *imgCart = [self imageWithImage:[UIImage imageNamed:#"ic_cart"] scaledToSize:CGSizeMake(35, 35)] ;
imgCart = [_utils changeColorOf:imgCart to:[UIColor whiteColor]];
UIButton *btnCart = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[btnCart addTarget:self action:#selector(btnCartClicked:) forControlEvents:UIControlEventTouchUpInside];
[btnCart setBackgroundImage:imgCart forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnCart];
It does not work when you set 'cancelButton.tintColor = [UIColor whiteColor]' in ios11. you can use the method "setTitleTextAttributes".

UINavigationBarItem custom - how to get the corner of tabcontroller?

i want to ask how i can get the icon of navigationtabbaritem to the left corner (left:0) and right corner (right:0).
my code :
_custombtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
_custombtn.backgroundColor = [UIColor redColor];
[_custombtn setBackgroundImage:[UIImage imageNamed:#"ic_msg.png"]forState:UIControlStateNormal];
[_custombtn addTarget:self action:#selector(viewBn:) forControlEvents:UIControlEventTouchUpInside];
self.csbtn = [[UIBarButtonItem alloc]initWithCustomView:_custombtn];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(0.0, 0.0, 44, 44);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *spaceFix = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
spaceFix.width = -10;
self.navigationItem.rightBarButtonItems = #[spaceFix, aBarButtonItem];
Please add spaceFix for resolve your problem may this help lot

Space between uibarbuttons in navigation bar, Xcode 6.1

I have tried almost all answers here, How to remove the blank space between the two uibarbuttons shown below the code i use is
UIButton *backBTN= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[backBTN addTarget:self action:#selector(backACT:) forControlEvents:UIControlEventTouchUpInside];
[backBTN setBackgroundImage:[UIImage imageNamed:#"menu_icon.png"] forState:UIControlStateNormal];
UIBarButtonItem *backkb = [[UIBarButtonItem alloc] initWithCustomView:backBTN];
// self.navigationItem.leftBarButtonItem = backkb;
UIButton *homeBTN= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[homeBTN addTarget:self action:#selector(homeACT:) forControlEvents:UIControlEventTouchUpInside];
[homeBTN setBackgroundImage:[UIImage imageNamed:#"logout_btn.png"] forState:UIControlStateNormal];
UIBarButtonItem *btnhome = [[UIBarButtonItem alloc] initWithCustomView:homeBTN];
// btnhome.imageInsets = UIEdgeInsetsMake(-10, 0, 0, 0);
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects: backkb, btnhome, nil]];
self.navigationItem.hidesBackButton = YES;
Try this,
// Create a UIView to add both buttons
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
UIButton *backBTN= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[backBTN addTarget:self action:#selector(backACT:) forControlEvents:UIControlEventTouchUpInside];
[backBTN setBackgroundImage:[UIImage imageNamed:#"menu_icon.png"] forState:UIControlStateNormal];
//Add button to UIView
[leftView addSubview:backBTN];
UIButton *homeBTN= [[UIButton alloc] initWithFrame:CGRectMake(30, 0, 30, 30)];
[homeBTN addTarget:self action:#selector(homeACT:) forControlEvents:UIControlEventTouchUpInside];
[homeBTN setBackgroundImage:[UIImage imageNamed:#"logout_btn.png"] forState:UIControlStateNormal];
//Add button to UIView
[leftView addSubview:homeBTN];
//Set UIView as CustomView for bar button
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftView];
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects: leftBarButton, nil]];
self.navigationItem.hidesBackButton = YES;
I have tried this and got output as
used some background colour to know the borders

MenuItemButtom action doesn't work

I want to add a custom image as left menu item. Thats why I'm using customView else I get my image but in blue.
This is how I achieve it:
UIImageView *menuItemImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MenuButton"]];
UIBarButtonItem *leftMenuItemButton = [[UIBarButtonItem alloc] initWithCustomView:menuItemImageView];
[leftMenuItemButton setAction:#selector(menuClicked:)];
[leftMenuItemButton setTarget:self];
self.navigationItem.leftBarButtonItem = leftMenuItemButton;
Everything works except when I click my button nothing happens. It never comes in this:
- (void)menuClicked:(id)menuClicked {
NSLog(#"%s menuClicked", __PRETTY_FUNCTION__);
}
Someone could help me?
The problem here seems to be using ImageView , UIBarButtonItem target and action only works if the custom view is UIButton.
Try something like this
UIButton* someBtn = [UIButton buttonWithType: UIButtonTypeInfoLight];
UIBarButtonItem *leftMenuItemButton = [[UIBarButtonItem alloc] initWithCustomView:someBtn];
[leftMenuItemButton setAction:#selector(menuClicked:)];
[leftMenuItemButton setTarget:self];
self.navigationItem.leftBarButtonItem = leftMenuItemButton;
Hope this helps
First, you need to create a custom UIButton and UIBarButtonItem
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(menuClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateHighlighted];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
Add the button to navigationBar
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:barButton, nil]];
Thanks to n00bprogrammer:
UIBarButtonItem * menuItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:#"MenuButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:#selector(menuClicked:)] ;
self.navigationItem.leftBarButtonItem = menuItem;
Both answers point to the right direction.
This is how I got it working:
UIButton *showCalendar = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
showCalendar.frame = CGRectMake( 0, 0, 40, 30 );
[showCalendar setImage:[UIImage imageNamed:#"calendar"] forState:UIControlStateNormal];
[showCalendar addTarget:self action:#selector(toggleCalendar) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:showCalendar];
Here is the code, add a tap gesture to your imageview:
UIImageView *menuItemImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MenuButton"]];
UIBarButtonItem *leftMenuItemButton = [[UIBarButtonItem alloc] initWithCustomView:menuItemImageView];
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(menuClicked:)];
[menuItemImageView addGestureRecognizer:tapGesture];

iOS custom right navigation bar

I'm trying to set an image for a right bar item in my navigation controller but iOS 6 keeps showing a black glow. I have tried a number of solutions from stack overflow but can't get it to work. The current code I have is this:
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"gear"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(someMethod)];
[rightItem setImageInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
[[self navigationItem] setRightBarButtonItem:rightItem];
In iOS7 is looks like this which is what I want:
This is how it looks in iOS6
try this one :
UIImage *faceImage = [UIImage imageNamed:#"gear.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );//set bound as per you want
[face addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.rightBarButtonItem = backButton;
may it will help you.
Create a UIButton first with the image, then do:
[[UIBarbuttonItem alloc] initWithCustomView:button];
Use custom view:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; // change this to use your image
[rightButton addTarget:self
action:#selector(yourAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightButtonItem;
Try this:
UIImage* image = [UIImage imageNamed:#"sample_Image.png"];
CGRect frameimg = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(MethodName:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBtn =[[UIBarButtonItem alloc] initWithCustomView:someButton];
[self.navigationItem setRightBarButtonItem:barBtn];

Resources