extra button in rightbarbuttonitems not showing - objective-c

I create two UIBarButtonItem in the code below. Later I add them to rightBarButtonItems yet only one of the two buttons shows up (the one I put first on the list).
I thought perhaps that the title's view (which is not set) would be in front of the rightBarButtonItems, since the Apple docs says "If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the left side of the bar are not displayed."
But that doesn't seem to be it either, since the view's frame = {0,0}{0,0}
I can't figure out what I'm doing wrong. Can anybody tell me how to get all rightBarButtonItems to show? Code:
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(shareAction:)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:[UIImage imageNamed:#"custom-bar-button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(barButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *wishlistButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItems:[[NSArray alloc] initWithObjects:shareButton, wishlistButton, nil]];

Use UIBarButtonItem:
UIBarButtonItem *button = [UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"custom-bar-button.png"] style:UIBarButtonItemStylePlain target:self action:#selector(barButtonTapped:)];
instead of:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:[UIImage imageNamed:#"custom-bar-button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(barButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

Related

Created Pushed Animation for BarButton

On a view controller, I have a back button with custom image via this code
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
[backButton setImage:[UIImage imageNamed:#"left207.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(goToParent) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBtn;
self.navigationItem.rightBarButtonItem = rightButton;
But when I run the app, there is no pushed sort of effect. It doesn't turn dark for a second.
You need to set another image for highlighted state:
[backButton setImage:[UIImage imageNamed:#"highlightedButton"] forState:UIControlStateHighlighted];

How to make a custom UIBarButton for Left Bar Button for BackBarButton

I am trying to customize UI of an application for iOS 7. I want exactly BackBarButton of iOS 7 but with different color for both arrow and title and different font for title. This is iOS 7 back button.
I tried to customize it with following code
UIImage *backButtonImage = [UIImage imageNamed:#"back.png"];
UIButton *customBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customBackButton addTarget:self action:#selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[customBackButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[customBackButton setFrame:CGRectMake(0, 0, 30, 30)];
backButton = [[UIBarButtonItem alloc] initWithCustomView:customBackButton];
[backButton setAction:#selector(backButtonPressed)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithCustomView:customBackButton];
It seemed like this:
There is two problem with above Image.
First it is not aligned to left(iOS back button sticked to the left).
Second it doesn't have title of previous page.
I just added title attribute to custom button.
[customBackButton setTitle:#"title" forState:UIControlStateNormal];
But it was like this:
How can I fix the problems(stick to left and title)?
For this, you need to have a custom image with arrow & title.
if you want to customise the title, you can just use below code>
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"My Title" style:UIBarButtonItemStylePlain target:nil action:nil];
But if you want it to set for an image, you need an image with having the back button to the left of the image.
Image:
2x Image:
See the images in editor, you can notice that image size is 70x32 and the arrow is more towards left end. You can have image with text also & use it.
Hope that helps.
I just found out how It is possible
I wrote following code and it perfectly worked!
UIBarButtonItem * backButton = [UIBarButtonItem new];
UIView * backButtonView = [UIView new];
[backButtonView setFrame:CGRectMake(0, 0, 90, 32)];
UIImageView *backButtonImage = [UIImageView new];
[backButtonImage setImage:[UIImage imageNamed:#"Setting/back.png"]];
[backButtonImage setFrame:CGRectMake(-15, 0, 30, 30)];
UILabel * backButtonLabel = [UILabel new];
[backButtonLabel setFrame:CGRectMake(8, 0, backButtonView.frame.size.width, 30)];
[backButtonLabel setBackgroundColor:[UIColor clearColor]];
[backButtonLabel setTextColor:[UIColor whiteColor]];
[backButtonLabel setTextAlignment:NSTextAlignmentLeft];
[backButtonLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:18]];
[backButtonLabel setText:title];
UIButton *customBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customBackButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[customBackButton setFrame:CGRectMake(0, 0, 90, 30)];
[backButtonView addSubview:customBackButton];
[backButtonView addSubview:backButtonImage];
[backButtonView addSubview:backButtonLabel];
backButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
[backButton setAction:action];
[self.navigationItem setLeftBarButtonItem:backButton animated:YES];

How to reduce padding between UIBarButton items in UINavigationBar IOS 7?

UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"addButton.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(addSubProperty:) forControlEvents:UIControlEventTouchUpInside];
[addButton setFrame:CGRectMake(0, 0,51,44)];
UIBarButtonItem *firstBarButton = [[UIBarButtonItem alloc] initWithCustomView:addButton];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteButton setImage:[UIImage imageNamed:#"deleteButton.png"] forState:UIControlStateNormal];
[deleteButton addTarget:self action:#selector(deleteProperty:) forControlEvents:UIControlEventTouchUpInside];
[deleteButton setFrame:CGRectMake(0, 0,50,44)];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-10];
UIBarButtonItem *secondBarButton = [[UIBarButtonItem alloc]initWithCustomView:deleteButton];
NSArray *navBtnArr = [[NSArray alloc] initWithObjects:negativeSpacer,firstBarButton,secondBarButton, nil];
self.navigationItem.rightBarButtonItems = navBtnArr;
After this i got this result ,as you guys can see i did able to manage move my first barbutton left byt that negativeSpacer but i donot know how to reduce space between those two button.
and by doing this my back also goes disappear any idea will really appreciated .
Using Xcode 6.4, This worked beautifully for me with 1 line of code!
https://stackoverflow.com/a/26469607/3634990
self.myBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 25, 0, -25);
Note, this moves the image location, but not the touch area, so it's a great fix only if you want to move the image just a little bit.

how to get the 3d map button in code?

are there any way to get one 3D button like this:
what i want to do, is something like this:
MK3dBUTTON *3dButton = [[MK3dBUTTON alloc] initWithMapView:self.mapView];
[3dButton setTarget:self];
UIBarButtonItem *3dbarButton = [[UIBarButtonItem alloc] initWithCustomView:3dButton];
[self.uiTollbar setItems:#[3dbarButton];
so how can i take this 3d button directly in code?
thanks
My solution:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(changeMapViewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 50, 30)];
[button setTitle:#"3D" forState:UIControlStateNormal];
[button setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[button setTitle:#"3D" forState:UIControlStateHighlighted];
[button setTitleColor:[self.view.tintColor colorWithAlphaComponent:0.15] forState:UIControlStateHighlighted];
[button.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:25]];
UIBarButtonItem *changeMapViewItem = [[UIBarButtonItem alloc] initWithCustomView:button];
This is what it looks like:
I tried it with:
UIBarButtonItem *changeMapViewItem = [[UIBarButtonItem alloc] initWithTitle:#"3D" style:UIBarButtonItemStylePlain target:self action:#selector(changeMapViewButtonPressed:)];
[changeMapViewItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"HelveticaNeue-Thin" size:25], NSFontAttributeName,nil] forState:UIControlStateNormal];
But then the Text does not align with the icons in the UIToolbar.
HTH
Dominik

Position UIBarbuttonItem to the VERY left

I have a UIBarButtonItem that I want hugging the very left of the screen. I've tried putting a fixed barbutton with width=0 before the button and other configurations but there's always several pixels between the button and x=0.
I've also tried changing the imageInset of the UIButton
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
moreButton.frame = CGRectMake(0, 0, 60, 35);
[moreButton setImage:[UIImage imageNamed:#"Morebutton"] forState:UIControlStateNormal];
moreButton.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 8);
[moreButton addTarget:self action:#selector(pressedMoreButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *moreBar = [[[UIBarButtonItem alloc] initWithCustomView:moreButton] autorelease];
But tapping near the edge of the screen doesn't trigger the selector (probably because only the image is moved but not the button itself) and increasing the size of the button doesn't affect the gap between the button and the left side.
Thanks
try to put your button to the container view and change position of button on the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
button.contentMode = UIViewContentModeScaleAspectFit;
button.frame= CGRectMake(-4.0, 0.0, 30, 30);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cellTextSize.width+10, buttonImage.size.height)] autorelease];
v.backgroundColor = [UIColor clearColor];
[v addSubview:button];
return [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];