Ios – Add border in UIBarButtonItem

iosios7uibarbuttonitemuitoolbar

In my app, I have designed a view in the interface builder.
This view has a toolbar, with some UIBarButtonItem in.

My buttons can be custom images, or default button like share, add,…
Now with iOS7, buttons don't have anymore borders. So I'd like to add some.
enter image description here

Here is what i want to do : add borders like the white lines on my screenshot.
What I've tried is to add a UIButton to the toolbar. In my example, I've set my back button size (12×44). I add this button as a IBOutlet property of my view controller, and try to draw a border to it :

CALayer *cancelBorder = [CALayer layer];
[cancelBorder setFrame:CGRectMake(12, 0, 1, 44)];
[backBorder setBackgroundColor:[[UIColor whiteColor] CGColor]];
[backButton.layer addSublayer:cancelBorder];

But it doesn't work. Anyone has a solution?

Best Answer

Adding UIBarButtonItem to toolbar programmatically may solve your problem.

First, create custom button with images, borders, etc. As HereTrix said, you can add border to this button.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10, 0, 30, 30);
button.layer.borderColor = [UIColor blueColor].CGColor;
button.layer.borderWidth = 1.0f;
/* any other button customization */

Then, initialize UIBarButtonItem with that custom button and add this item to your toolbar.

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.toolBar.items = @[backButton];
Related Topic