So when I try to set a custom color to a UIBarButton, it causes the displayed button to be too big and it extends down past the NavigationBar and into the main view of the app. Is there an easy way to fix this? Here is my code:
UIImage *buttonColor = [[UIImage imageNamed:#"blue-background.jpg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[[UIBarButtonItem appearance] setBackgroundImage:buttonColor forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
try playing with the values of the UIEdgeInsetsMake function.
in one of my apps it looks like this;
UIImage *backbuttonimage = [[UIImage imageNamed:#"backbutton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 10)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backbuttonimage
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
it made it as i wanted... if you have any problems i would like you to tell me and if you can add a photo of how it looks for me to understand better the problem.
these are my values for my app:
UIEdgeInsetsMake(0, 15, 0, 10)
have a nice day :)
This is how i make custom UIBarButtonItem and it works perfect for me. Try this:
UIButton *postView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 61, 30)];
[postView setTitle:#"Post" forState:UIControlStateNormal];
[postView addTarget:self action:#selector(showAddPost) forControlEvents:UIControlEventTouchUpInside];
[postView setBackgroundImage:[UIImage imageNamed:#"bbi_texture.png"] forState:UIControlStateNormal];
[postView setBackgroundImage:[UIImage imageNamed:#"bbi_texture.png"] forState:UIControlStateHighlighted];
UIBarButtonItem *postButton = [[UIBarButtonItem alloc] initWithCustomView:postView];
[self.navigationItem setRightBarButtonItem:postButton];
Related
I want to custom iOS back button throughout the application. So I add these lines in my file AppDelegate.m :
UIImage *backButton = [UIImage imageNamed:#"btn_return"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[backButton resizableImageWithCapInsets:UIEdgeInsetsMake(0, backButton.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Here is the result :
As you can see, there is a white mark on the right.... Does somebody know why ? How can I remove it ?
The previous view controller has a white space as back button title (in his navigation item) because I don't want any label.
Maybe it's because of that ?! Is there an other solution to not see the default "Back" label ?
Thanks a lot for your help
[EDIT]
When I try the answer of Cy-4AH, I get :
Use in this way for
UIImage *image1 = [[UIImage imageNamed:#"btn_return"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self.menuBarBtn setImage:image1];
You need use
[[UINavigationBar appearance] setBackIndicatorImage: backButton];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage: backButton];
if yours deployment target IOS 7+
Otherwise ask yours designer to add one pixel empty column in the right.
Use this code:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
UIButton *backButton;
backButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0,35 ,35)];
backButton.imageEdgeInsets = UIEdgeInsetsMake(7, 7, 7, 7);
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(btnBackAction:) forControlEvents:UIControlEventTouchUpInside];
[backButton setImage:[UIImage imageNamed:#"your back button image name"] forState:UIControlStateNormal];
UIBarButtonItem *Rightbarbutton=[[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem=Rightbarbutton;
Use this code for hide default backbutton
self.navigationItem.hidesBackButton = YES;
Thanks to #DipenPanchasara, I changed my code by :
UIImage *backButtonImage = [[UIImage imageNamed:#"btn_return"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
GFloat leftInset = SYSTEM_VERSION_GREATER_THAN(#"9.0") ? -2.5 : backButtonImage.size.width -12;
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[backButtonImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, leftInset, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
(values in UIEdgeInsetsMake changed)
Here is the result :
The back button image is slightly flattened but it doesn't matter.
Thank you all for your suggestions ! :-)
I am customizing my nag bar button in iOS7 and I am using the following code:
// Customizing the NavBar Buttons
UIImage * btHome = [[UIImage imageNamed:#"Home.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 3, 0, 3)];
[[UIBarButtonItem appearance] setBackgroundImage:btHome forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
It is working, but the view shows my home button with the button text over it, I do not want the text to show.
What else do I need to do to remove the text?
What I have is shown here:
Try to set
1) text color on a bar button item through appearance
2) use setImage: instead of setBackgroundImage: and do not use resizable image
Try with this code. May be it will help you.
UIImage *faceImage = [UIImage imageNamed:#"back_arrow.png"];
UIButton *yourbutton = [UIButton buttonWithType:UIButtonTypeCustom];
yourbutton.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
[yourbutton addTarget:self action:#selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
[yourbutton setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:yourbutton];
self.navigationItem.leftBarButtonItem = backButton;
Sets the back button title offset for given bar metrics
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
I am wanting to use an image for my backButton in my navigationController. I just want the image used, with no "back" text shown at all, so I did:
UIImage *backButtonImage = [[UIImage imageNamed:#"ZSSBackButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 25, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, backButtonImage.size.height*2) forBarMetrics:UIBarMetricsDefault];
This works well, but seems to have the side effect to making long navigationItem titles appear to be off center, even though it really is just leaving white space where the backbutton text should be:
Is there a way that I can bring the title a little more to the left?
You can manage position of UINavigationbar title using titleView property.
UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];// Here you can set View width and height as per your requirement for displaying titleLbl position in navigationbar
[backView setBackgroundColor:[UIColor clearColor]];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
[backView addSubview:titleLbl];
titleLbl.text = #"YOUR TITLE";
titleLbl.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = backView;
EDIT : you can create custom button and assign it to leftbutton as :
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0,0,51,31);
[btnBack setBackgroundImage:[UIImage imageNamed:#"ZSSBackButton"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(btnBackProfile_TouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
[self.navigationItem setLeftBarButtonItem:leftBtn];
Hope it helps you.
I have created view with barbutton as follows
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"settingsicon.png"] style:UIBarButtonItemStylePlain target:self action:#selector(settingsButtonClicked:)];
It is comming like this
but I want it as
how can I remove the border of the button?
Use this :
UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:customButton];
You can initialize a button with whatever view you would like. You can make a button with any (acceptable) view inside it.
eg.
// GRAPHICAL BUTTON FRAMEWORK
UIButton* btton = [UIButton buttonWithType:UIButtonTypeCustom];
[btton setFrame:CGRectMake(0, 0, 30, 30)];
[btton addTarget:self action:#selector(SOMEROUTINE) forControlEvents:UIControlEventTouchUpInside];
[btton setImage:[UIImage imageNamed:#"SOME IMAGE"] forState:UIControlStateNormal];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:btton];
Hope it helps you.
Another way is to remove button's background image:
// portrait
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// landscape
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsLandscapePhone];
I have a background image for a button, with the dimensions of 80x30 pixels.
I use the code below to set the background in my view-controller, and the result is this:
As you can see, the corners and the top of the right button is messed up.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIImage *favoriteBtnImgNormal = [[UIImage imageNamed:#"favorite-btn-normal"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 30, 0)];
UIImage *favoriteBtnImgTouch = [[UIImage imageNamed:#"favorite-btn-touch"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 30, 0)];
[self.navigationItem.rightBarButtonItem setBackgroundImage:favoriteBtnImgNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:favoriteBtnImgTouch forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:favoriteBtnImgTouch forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setTitlePositionAdjustment:UIOffsetMake(-10.0, 0.0) forBarMetrics:UIBarMetricsDefault];
}
Note that I do this in a single view-controller, I use the appearance API to set general styles. But here I want to override the general appearance.
If I just get the images without the resizable-stuff, it looks like this:
Now the edges and corners are fine, but the button is too large.
I'm sure I'm doing this all wrong, so I need someone to point out what I could do to scale buttons correctly?
UIImage *image = [UIImage imageNamed:#"YourImage"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(YourAction) forControlEvents:UIControlEventTouchUpInside];
button.contentEdgeInsets = (UIEdgeInsets){.right=-10};
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];