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

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

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 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.

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

Show UIBarButtonItem just like Apple's iPad Mail App

Apple's iPad Mail app has a few icons that appear embedded in the setRightBarButtonItems area of the navigationItem of the detail view of the split view controller:
How can I add icons to the navigationItem bar like this (I have the icons already).
My issue is that the UIBarButtonItem class reference doesn't seem to have an appropriate UIBarButtonItemStyle that allows for no border around the button. I've tried configuring the UIBarButtonItem via initWithCustomView yet tapping the button doesn't work.
Thanks in advance for any suggestions
Cheers
EDIT: I could use UIBarButtonItemStylePlain however it doesn't look 'embedded' like the apple buttons, which is the look I am after.
I've worked it out and can produce the following result:
Here is the code I use. It could be optimised however is nice and repetitive for demonstration purposes:
- (void)setupNavigationItemButtons {
float buttonWidth = 60;
float buttonHeight = 40;
UIImage *imageA = [UIImage imageNamed:#"212-action2.png"];
UIImage *imageB = [UIImage imageNamed:#"111-user.png"];
UIImage *imageC = [UIImage imageNamed:#"122-stats.png"];
UIButton *buttonA = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
UIButton *buttonB = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
UIButton *buttonC = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
[buttonA addTarget:self action:#selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
[buttonB addTarget:self action:#selector(doSomethingElse:) forControlEvents:UIControlEventTouchUpInside];
[buttonC addTarget:self action:#selector(doSomethingRandom:)forControlEvents:UIControlEventTouchUpInside];
[buttonA setImage:imageA forState:UIControlStateNormal];
[buttonB setImage:imageB forState:UIControlStateNormal];
[buttonC setImage:imageC forState:UIControlStateNormal];
UIBarButtonItem *buttonItemA = [[UIBarButtonItem alloc] initWithCustomView:buttonA];
UIBarButtonItem *buttonItemB = [[UIBarButtonItem alloc] initWithCustomView:buttonB];
UIBarButtonItem *buttonItemC = [[UIBarButtonItem alloc] initWithCustomView:buttonC];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:buttonItemA, buttonItemB, buttonItemC,nil]];
}

Move UIBarButtonItem few pixels down?

I have created a custom UINavigationBar which is a tiny bit taller than Apples default navigation bar.
I can’t seem to find a way to move the UIBarButtonItem down to be directly centered between the two dashed lines.
Is there an easy way to do this? I’ve tried creating a custom button but had no success. Ideally I would just like to move the default back button down a couple of pixels.
Code used to create UINavigationBar, custom header image and UIBarButtonItem:
//Create image for navigation background
UIImage *NavigationPortraitBackground = [[UIImage imageNamed:#"navbackground.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage: NavigationPortraitBackground
forBarMetrics:UIBarMetricsDefault];
//Centered title image
UIImageView *headerImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"hometitle.png"]];
[headerImage setFrame: CGRectMake(0, 0, 180, 49)];
self.navigationItem.titleView = headerImage;
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] init];
[backBarButtonItem setTintColor: [UIColor colorWithRed:0 green:69.0/255 blue:118.0/255 alpha:1]];
[backBarButtonItem setStyle: UIBarButtonItemStylePlain];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
Thanks in advance,
Create the UIBarButtonItem using a custom view. This custom view will be a UIView with the actual UIButton (as a subview) placed x pixels from the top (x=the number of pixels you want to move it down).
sorry
UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton1 setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal]; myButton1.showsTouchWhenHighlighted = YES;
myButton1.frame = CGRectMake(0.0, 3.0, 50,30);
[myButton1 addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1];
self.navigationItem.leftBarButtonItem = leftButton;