I have programatically created a toolbar to which I have added a UIBarButtonItem.
This is what is currently looks like:
I want the the button to have a border and a corner radius(curved corners). How will the same be implemented?
UIBarButtonItem Implementation code:
let okBarBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "donePressed:")
There's no built-in automatic way to do this, unfortunately. You have two choices:
You could make this a bar button item with a custom view, make that view a UIButton, and then do the usual stuff to that UIButton (which you can do because it's a view), by way of its layer (give it a border and corner radius).
You could just draw (in code) a background image with a curved border and use that as the bar button item's image.
If you can find the UIView associated with the UIBarButtonItem, you can modify the
UIView.layer
. But, finding theUIView
is not made easy. I used the technique from Figure out UIBarButtonItem frame in window? . Starting with thenavigationController.navigationBar
, which itself is aUIView
, I recursed through the subviews, one of which will be the button I wanted. Which one? Pick your own criteria! Here's my code:then in
viewDidAppear
(or similar)which is a bit of a hack but does the job: