UINavigationBarItem custom - how to get the corner of tabcontroller? - ios

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

Related

How to remove NavigationBar Right space?

I'm playing with Navigation Bar Buttons I need a button from right edge with 0 space, after google search i found below code
self.filterButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
[self.filterButton setBackgroundColor:[UIColor redColor]];
UIImage *buttnImage=[UIImage imageNamed:#"chemistry-filter"];
[self.filterButton setImage:buttnImage forState:UIControlStateNormal];
[self.filterButton addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:self.filterButton];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -25;
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,btn2, nil] animated:YES];
But unfortunatly in iOS 13 negativeSpacer not working. can anybody suggest me solutions.
Attached screenshot here
Thanks.
Let's try this easy way, Let's define the width of space BarButtonItem to be width of self.view.frame.size.width - width of the button.
self.filterButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
[self.filterButton setBackgroundColor:[UIColor redColor]];
UIImage *buttnImage=[UIImage imageNamed:#"chemistry-filter"];
[self.filterButton setImage:buttnImage forState:UIControlStateNormal];
[self.filterButton addTarget:self action:#selector(filterAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc]
initWithCustomView:self.filterButton];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil
action:nil];
negativeSpacer.width = self.view.frame.size.width - 45; // As 45 is the frame width of your filter button
[self.navigationItem setRightBarButtonItems:[NSArray
arrayWithObjects:negativeSpacer,btn2, nil] animated:YES];
Hope this helps!

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];

Putting 4 navigation bar buttons

i am new to iOS programming and i am wondering if it is possible to put 4 bar buttons into the UINavigationBar? I have tried a few methods that i have found on stack overflow but the buttons positions are not equal.
Here is a sample screenshot
.
The method that i used to code my navigation bar is:
UIToolBar *tools = [[UIToolBar alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
UIImage *backImage = [UIImage imagedName:#"backIcon.png"];
UIImage *shareImage = [UIImage imagedName:#"shareIcon.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backImage forState:UIControlStateNormal];
// create the button and assign the image to the leftsidebutton
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[backButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setImage:shareImage forState:UIControlStateNormal];
shareButton.frame = CGRectMake(0, 0, shareImage.size.width, shareImage.size.height);
//create space between the buttons
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
UIBarButtonItem *shareBarButton = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
self.navigationItem.hidesBackButton = YES;
NSArray *leftActionButtonItems = #[customBarButton, bi, shareBarButton];
[tools setItems:leftActionButtonItems animated:NO];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
I did the same for rightBarButtonItem but it is not working so well.
Any help will be greatly appreciated! Thanks!
Try this one :
Edit :
it display Proper check it :
take .h file :
UIToolbar *toolBar_T;
UIBarButtonItem *item1,*item2,*item3,*item4;
take .m file :
toolBar_T=[[UIToolbar alloc] init];
toolBar_T.frame=CGRectMake(0,5,168, 44);
[self.view addSubview:toolBar_T];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(button0:) forControlEvents:UIControlEventTouchUpInside];
button0.frame = CGRectMake(0, 7, 35,29);
item1 = [[UIBarButtonItem alloc] initWithCustomView:button0];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(button1:) forControlEvents:UIControlEventTouchUpInside];
button1.frame = CGRectMake(0, 7, 35,29));
item2 = [[UIBarButtonItem alloc] initWithCustomView:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:[UIImage imageNamed:#"3.png"] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(button2:) forControlEvents:UIControlEventTouchUpInside];
button2.frame = CGRectMake(0, 7, 35,29);
item3 = [[UIBarButtonItem alloc] initWithCustomView:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 setImage:[UIImage imageNamed:#"4.png"] forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(button3:) forControlEvents:UIControlEventTouchUpInside];
button3.frame = CGRectMake(0, 7, 35,29);
item4 = [[UIBarButtonItem alloc] initWithCustomView:butto3];
NSArray *buttons = [NSArray arrayWithObjects: item1, item2,item3, nil];
[toolBar_T setItems: buttons animated:NO];
toolBar_T.barStyle=-1;// For Clear backgroud
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolBar_T];
Hope it will help.
Happy coding...
You can try smth like this...
-(void)setupToolbarButtons:(UIViewController *)navC
{
//create a toolbar in the right
UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 135, 44.01)];
tools.delegate = self;
tools.barTintColor = [UIColor clearColor];
//create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:4];
//create a standart 'play' button
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:nil];
[buttons addObject:bi];
[bi release];
//create a standart 'stop' button
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:nil];
[buttons addObject:bi];
[bi release];
//create a standart 'fast forward' button
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:nil];
[buttons addObject:bi];
[bi release];
//create a standart 'pause' button
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:nil];
[buttons addObject:bi];
[bi release];
//stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
//and put the toolbar in the nav bar
navC.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
}
...also you can add a spacer if it's needed
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
and if something is wrong in vertical spacing, then add this
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
UINavigationBar is Inherits from UIView, so you can simply add any object in.
Try the following code
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:customBarButton, bi, shareBarButton, nil];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:BUTTON OBJECTS, nil];

Resources