I need to create a navBar back button in accordance to the designer's plan. The back button has a pattern image and stitched leather on the perimeter.
Here it is:
My question is it possible to create this without a great amount of hassle and headache? Or if it's possible at all, since the back button has varying width?
Thanks!
UPDATE
Alright, with the help of PartiallyFinite turns out this is very easy. If you set the UIEdgeInsets correctly it will keep the left side fixed, the right side fixed, and then duplicate the middle of the image considering the back button's width.
This is the image I used for my back Button:
And these are my inset settings. You can try them yourself:
backButtonImage = [backButtonImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 17, 5, 12)];
Hope this helps someone in the future.
You will need to provide a stretchable image for the button, so it knows how to display it correctly:
UIImage *buttonImage = [[UIImage imageNamed:#"backButtonImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 6)]
You don't need to do anything special to the image itself, but you do need to specify appropriate edge insets for the resizable image to indicate the area around the edges of the image that should not be stretched, as shown above (the example shows an inset of 15 pixels from the left and 6 from the right). This area should cover the arrow head, and the curved right edge, so that the middle area can be stretched out as needed. Read the documentation for more information on this method.
UPDATE: By default, the resizable area of the image will be tiled to the new size, however if you want to have it stretch instead, you can use resizableImageWithCapInsets:resizingMode: and pass UIImageResizingModeStretch to achieve that behaviour. For your case obviously tiling is better as it preserves the stitching, but for some background images stretching is a better solution. Just putting this here to help anyone who sees this in the future.
Once you have the stretchable image, you can change the appearance your back button using this code:
[myBackButtonItem setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Alternatively, you can set this custom appearance for all back buttons in your app using this code:
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Run this when your app launches, and it will affect all back buttons in your app.
Note that, contrary to what some of the other answers suggest, you will not need to manually create any back buttons. If you create a UINavigationController and use it in the recommended way (read about that in the documentation, a navigation bar and back button will be created for you as you push view controllers using pushViewController:animated:. If you use the global UIAppearance code snippet to apply the custom button style, it will automatically be applied to all the back buttons that you have.
You can read more about setBackButtonBackgroundImage:forState:barMetrics: in the official documentation.
There are also numerous tutorials available online for a more in-depth explanation of how this works and how to do it, here are a few good ones:
http://useyourloaf.com/blog/2012/08/24/using-appearance-proxy-to-style-apps.html
http://nshipster.com/uiappearance/
You need to create your own BarButtonItem. You can't set image for system back button.
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:buttonWithYourImage] autorelease];
You can create your own UIBarButtonItem and set it as the leftButtonItem on the navigation bar on the current view controller:
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"your_image"] style:UIBarButtonItemStyleBordered target:self action:#selector(yourFunction:)];
self.navigationItem.leftBarButtonItem = btn;
I use this in my Appdelegate.m to customize button. ("buttonBack" is the name of your image). Hope it may help you
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIImage *barButtonImage = [[UIImage imageNamed:#"buttonBack"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
return YES;
}
First you need to make your own back button.. and set that image to back button.
UIBarButtonItem * btn=[[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(changed)];
self.navigationItem.leftBarButtonItem=btn;
[btn setImage:[UIImage imageNamed:#"yourimagename"]];
-(void)changed
{
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES];
}
try this one really helpful to you...
Related
UIBarButtonItem *adminBarButtonItem = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"779-users"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(adminButtonTouched)];
I tried to assign a new frame to adminBarButtonItem.customView.frame, it didn't work.
Expect for [[UIBarButtonItem alloc] initWithCustomView:aView], is there anyway to change the size of UIBarButtonItem?
I encountered a similar situation where I needed to resize a custom image to fit alongside another system bar button item. The key is to resize the image itself before I use it in UIBarButtonItem's initWithImage method.
For how to resize that UIImage, follow this link The simplest way to resize an UIImage?
Another pitfall is the tintColor of the custom button item. I used
myButtonItem.tintColor = [UIColor colorWithPatternImage:myImage];
to get around the blue tint that covers up my image (iOS8), but I suspect there exists a more kosher way. If anybody knows, please enlighten me.
I have used the following code in my appDelegate and customizing back bar button goes well. When i used segue for the controllers navigation backbarbutton appears twice.
UIImage *buttonPortait = [[UIImage imageNamed:#"back-icon"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0,0, 0,0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonPortait
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[UIBarButtonItem.appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -64) forBarMetrics:UIBarMetricsDefault];
I also used navigationBar setTranslucent:NO but yet the problem surviving. Any solution for this problem. Thanks in advance.
I got the solution by altering the image size for the back bar button.
I used image size with height 30x(non-retina)/60x(retina) and width 90x(non-retina)/180x(retina)
cheers!!!
I would like to make a back and forward navigation from the start. So I added a Button inside the navigation bar however I would like the exact iOS icon with the back text after it.
How can I get this thing myself without the need to navigate programmatically? I use the storyboard.
Ignore the background color I mean this icon :
I do not use a navigation controller I use a page view controller and that is why. I don't know how this icon work if the text is part of the icon or not so I hope anyone knows.
I get now what you are trying to do. I did this not too long ago. I actually had to make the button myself. You said you have a leftbutton, this will need an outlet in your .h file. If you want we will call it leftButton for now. You will need some images so I am going to include the ones that I made. I made them in Paint Code. It is $99 USD but one of the best investments I ever made as an App designer. Here they are. I called them back and whiteSpace..
The last two images are white spaces so make sure you get them too. Drage them from this page to your desktop, then drag them to your project, make sure to click destination, copy into folder when you do this. Once you have the icons, the rest is done programmatically. In your viewDidLoad method you will change the appearance of your leftButton.
//First get the images
UIImage *arrow = [UIImage imageNamed:#"back.png"];
UIImage *wordSpace = [UIImage imageNamed:#"whiteSpace.png"];
//Next make a size of the image or it will be distorted
CGSize size = CGSizeMake(arrow.size.width + wordSpace.size.width, arrow.size.height);
//Put the two images together
UIGraphicsBeginImageContext(size);
[arrow drawInRect:CGRectMake(0, 0, arrow.size.width, size.height)];
[wordSpace drawInRect:CGRectMake(arrow.size.width, 0, wordSpace.size.width, wordSpace.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Assign the icon to the button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked)];
[backButton setBackgroundImage:finalImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//Move title over a bit to make it look nice
[backButton setTitlePositionAdjustment:UIOffsetMake(-20, 0) forBarMetrics:UIBarMetricsDefault];
//Make your button this new backButton
_leftButton = backButton;
Now that you have a custom back button you can assign the #selector(backButtonClicked). I am not sure about the fact that you are using a page controller, but in mine, I used a popViewController. But I did have a Navigation Controller in the project. You might need to call the proper VC with another method. But this is backButtonClicked.
- (void)backButtonClicked
{
//NSLog(#"going back");
[self.navigationController popViewControllerAnimated:YES];
//change to call your VC
}
So other than calling your desired view controller that should do it. If you don't know how to call a view controller programmatically, here is a link.
I want to change the background image of my UIBarButtonItems. In the root view, I want them to be a certain background image (but still rounded-rect buttons) in the nav bar, but in the next view, I have a UIToolBar where I want it to have different backgrounds yet again.
I used [[UIBarButtonItem] appearance] in my app delegate to change all of them, but I now realize for some I want it one style and for others yet another.
More importantly, I want to change not only the background image, but the shape for some. For one of the UIToolBar's UIBarButtonItems I want it to be in the shape of a back button in the navigation bar. How would I achieve this look?
Can I achieve both of these with the method outlined here?
Basically: How do I make custom UIBarButtonItems all over the app, and have some with different shapes?
If you want the buttons to look different in different view controllers, you should set them within the view controller, not in the delegate. Add code in either the init method or the viewWillAppear: method to customise the buttons. If you are customising UIToolbar items then use code similar to the link you gave. If it's for the navigation bar, do something like this:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//Customise button with background etc that you want
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = item;
//Release item if you're not using ARC
You can use the 'appearanceWhenContainedIn' method from the UIAppearance Protocol http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html
So for the Toolbar, you'd use:
[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil]
And for the NavBar:
[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]
When I try to use UIBarButtonItem's "initWithImage" to initialize a navigation bar custom image, it comes out washed-up and stretched against a black navigation bar. This is how I create it:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"gear.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(showSetting:)];
Here is what it looks like:
Any idea if it's a problem with the image? I got it from a set of icons I bought.
The best way to do this is to create a button, set its background image, and set its action. Then a UIBarButtonItem can be created using this button as the custom view. Here's my example code:
UIButton *settingsView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 61, 30)];
[settingsView addTarget:self action:#selector(SettingsClicked) forControlEvents:UIControlEventTouchUpInside];
[settingsView setBackgroundImage:[UIImage imageNamed:#"settings"] forState:UIControlStateNormal];
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithCustomView:settingsView];
[self.navigationItem setRightBarButtonItem:settingsButton];
The displayed images on bar button items are 'derived' from the source image (it uses only the alpha channel values in rendering but that all looks ok in your image). Its possibly just not the right size - you might need to open the image file and crop it to the right size.
You could also try looking at whether setting the imageInsets property (inherited by UIBarButtonItem from UIBarItem) can be used to adjust the size in a way to stop it getting stretched.
Doco on the bar item images says the following:
The images displayed on the bar are derived from this image. If this image is too large to fit on the bar, it is scaled to fit. Typically, the size of a toolbar and navigation bar image is 20 x 20 points.
I know this question already has a checkmarked anser. But I ran in to this today and thought I would offer up my answer anyway. The check marked answer above did help me, but it also took some extra experimenting to figure out what was really happening.
The button image is being shrunk only in the x axis and not the y. this is because it is too tall for the button and it shrinks it down to fit. But it doesn't shrink it proportionally. Only on the vertical. So it appears stretched. It's not actually stretched - which implies a widening of it. Instead the height is shrunk. Knowing the difference I think is important to understanding why it's happening and how to fix it.
I did the same thing the OP did. Thinking that I am supporting retina I made my icon 40x40. Mine was a green checkmark with an alpha channel. It was padded with blank pixels to be 40x40. The app resized it to fit within the button's available height. But the width stayed the same. So it became somewhere in the range of 40x30 or 40x20. I think the button can handle an icon 30 high, but then it's a little too big for the box IMHO.
The OP reduced the button to 30x30 and that made it not squish any more. But that's not the best solution. Because it isn't actually a retina button when you do that. It's shrunk and then blown back up on the retina.
The correct answer is to name your 40 pixel tall version with the #2x and then make a half size (20 pixel tall) version and save it without the #2x. The width can be whatever. Then load with imageNamed: without specifying the #2x. It will use the appropriate png for a retina or non-retina device.
The next thing that happened to me was then the button frame was too small. So I upped my canvas size in psd to pad the png to 80 wide to make the button slightly wider and more tappable.
I got the same stretched issue for my 40x40 image when I set the background image for leftBarButtonItem
UIBarButtonItem *backButton = [UIBarButtonItem new];
[backButton setBackButtonBackgroundImage:[UIImage imageNamed:#"back_icon"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationItem.leftBarButtonItem = backButton;
But my issue got resolved with the following code
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back_icon"] style:UIBarButtonItemStylePlain target:self action:#selector(handleBack:)];
self.navigationItem.leftBarButtonItem = backButton;
and same result if UIBarButtonItemStyleBordered is used.
Set correct image size: #1x = 22px, #2x = 44px #3x = 88px firstly.
Then
let leftBarButtonItem = UIBarButtonItem(image: yourUIImage, style: .plain, target: self, action: #selector(action))
leftBarButtonItem.tintColor = UIColor.red
navigationItem.leftBarButtonItem = leftBarButtonItem
or
let btn = UIButton(type: .custom)
btn.addTarget(self, action: #selector(contactMe), for: .touchUpInside)
btn.setImage(#imageLiteral(resourceName: "open"), for: .normal)
For those who have come across this toolbar item stretching issue in iOS 11 specifically, it appears that the #2x version of your image is now required to render its frame and or bounds.
So if you have code like this where you're adding a custom image UIBarButtonItem like this:
UIButton *tagsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
tagsBtn.bounds = CGRectMake( 0, 0, 40, 40);
[tagsBtn setImage:[UIImage imageNamed:#"tags.png"] forState:UIControlStateNormal];
tags = [[UIBarButtonItem alloc] initWithCustomView:tagsBtn];
[tagsBtn addTarget:self action:#selector(tags:) forControlEvents:UIControlEventTouchUpInside];
[bottomToolbar setItems:[NSArray arrayWithObjects:flexibleSpace,tags,flexibleSpace,nil]];
Then you will need to have a tags#2x.png that's 80x80, even if your tags.png image is 80x80. Simply renaming tags.png to tags#2x.png would resize the image to 40x40 as it did pre iOS 11 without changing code, or just add tags#2x.png to your project.