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!
Related
I have four right bar button items added in a navigation bar.
UIButton *replybutton = [UIButton buttonWithType:UIButtonTypeCustom];
[replybutton setImage:[UIImage imageNamed:#"reply.png"] forState:UIControlStateNormal];
[replybutton addTarget:self action:#selector(replyAction:)forControlEvents:UIControlEventTouchUpInside];
[replybutton setFrame:CGRectMake(0, 0, 30, 28)];
UIBarButtonItem *replyBarButton = [[UIBarButtonItem alloc] initWithCustomView:replybutton];
// there are 3 more buttons like this..
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:replyBarButton, replyAllBarButton, forwardBarButton, deleteBarButton, nil];
I want to give some extra spacing between these four bar button custom type items. I tried imageInsets,
replyBarButton.imageInsets = UIEdgeInsetsMake(0.0, 40.0, 0.0, 0.0);
but it doesn't give spacing. Could someone advise me, how can i give extra spacing between four right bar custom type button items?
You can use UIBarButtonSystemItemFlexibleSpace or UIBarButtonSystemItemFixedSpace.
Here is an example:
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"icon.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(replyAction:) forControlEvents:UIControlEventTouchUpInside];
[button1 setFrame:CGRectMake(0, 0, 30, 28)];
// other buttons are created the same way
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithCustomView:button3];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithCustomView:button4];
UIBarButtonItem *space1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *space2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *space3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.navigationItem.rightBarButtonItems = #[item1, space1, item2, space2, item3, space3, item4];
In this example, you are adding flexible spaces between every item. This will automatically space your items evenly.
If you want, you can also define the space:
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedSpace.width = 40.0 // adjust this
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
Idea is very simple. I have navigation bar in universal storyboards with custom controls.
UIButton *menu = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
[menu setImage:[UIImage imageNamed:#"menu"] forState:UIControlStateNormal];
[menu addTarget:self.viewDeckController action:#selector(toggleLeftView) forControlEvents:UIControlEventTouchUpInside];
UIButton *refresh = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[refresh setImage:[UIImage imageNamed:#"refresh"] forState:UIControlStateNormal];
[refresh addTarget:self action:#selector(reloadPosts) forControlEvents:UIControlEventTouchUpInside];
UIButton *share = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[share setImage:[UIImage imageNamed:#"share"] forState:UIControlStateNormal];
[share addTarget:self action:#selector(showActionView) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *menuBtn = [[UIBarButtonItem alloc] initWithCustomView:menu];
UIBarButtonItem *refreshBtn = [[UIBarButtonItem alloc] initWithCustomView:refresh];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithCustomView:share];
UIBarButtonItem *negativeSpacerLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacerLeft setWidth:-10];
UIBarButtonItem *negativeSpacerRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacerRight setWidth:-10];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerLeft, menuBtn, nil];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacerRight, shareBtn, refreshBtn, nil];
Everything works correctly - navigation bar has implemented style and buttons/icons. But there is a problem with iPad and iOS 8 (works on iOS7). When I tap on any button, it does not respond. What I did wrong? It works as well on iPhone with iOS 7/8.
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];
i have 2 custom right bar button items, and in portrait mode they overlays each others and only one of them is visible, but on landscape mode both are visible. items are created with custom view which is the UIButton with background image.
optionsBUtton=[UIButton buttonWithType:UIButtonTypeCustom];
[optionsBUtton setImage:[UIImage imageNamed:#"optionsIcon.png"] forState:UIControlStateNormal];
[optionsBUtton setBackgroundImage:[UIImage imageNamed:#"optionsBtn.png"] forState:UIControlStateNormal];
[optionsBUtton sizeToFit];
UIBarButtonItem* btnOptions=[[UIBarButtonItem alloc] initWithCustomView:optionsBUtton];
searchButton=[UIButton buttonWithType:UIButtonTypeCustom];
[searchButton setImage:[UIImage imageNamed:#"searchIcon.png"] forState:UIControlStateNormal];
[searchButton setBackgroundImage:[UIImage imageNamed:#"optionsBtn.png"] forState:UIControlStateNormal];
[searchButton sizeToFit];
UIBarButtonItem* btnSearch=[[UIBarButtonItem alloc] initWithCustomView:searchButton];
rightButtonItems=[[NSArray alloc] initWithObjects:btnOptions,btnSearch, nil];
navItem.rightBarButtonItems=rightButtonItems;
you must user tool bar and set the toolbar with buttons here is example code
// create a toolbar where we can place some buttons
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(saveAction:)];
saveButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:saveButton];
[saveButton release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
// create a standard delete button with the trash icon
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
[deleteButton release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:toolbar] autorelease];
[toolbar release];
Thanks..!