如何将边框和圆角半径应用于 UIBarButtonItem? [英] How to apply borders and corner radius to UIBarButtonItem?
问题描述
我以编程方式创建了一个工具栏,并在其中添加了 UIBarButtonItem.
这是目前的样子:
我希望按钮具有边框和圆角半径(弯角).将如何实施?
UIBarButtonItem 实现代码:
let okBarBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "donePressed:")
如果你能找到与 UIBarButtonItem 关联的 UIView,你可以修改 UIView.layer
.但是,找到 UIView
并不容易.我使用了
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:")
If you can find the UIView associated with the UIBarButtonItem, you can modify the UIView.layer
. But, finding the UIView
is not made easy. I used the technique from Figure out UIBarButtonItem frame in window? . Starting with the navigationController.navigationBar
, which itself is a UIView
, I recursed through the subviews, one of which will be the button I wanted. Which one? Pick your own criteria! Here's my code:
func recurseViews(view:UIView) {
print("recurseViews: \(view)") // helpful for sorting out which view is which
if view.frame.origin.x > 700 { // find _my_ button
view.layer.cornerRadius = 5
view.layer.borderColor = UIColor.redColor().CGColor
view.layer.borderWidth = 2
}
for v in view.subviews { recurseViews(v) }
}
then in viewDidAppear
(or similar)
recurseViews((navigationController?.navigationBar)!)
which is a bit of a hack but does the job:
这篇关于如何将边框和圆角半径应用于 UIBarButtonItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!