我想制作一个带圆边框的UIBarButtonItem。
let postButton = UIButton(type: .Custom)
postButton.frame = CGRectMake(0, 0, 90, 30)
postButton.layer.borderColor = logoColor(0).CGColor
postButton.layer.borderWidth = CGFloat(3.0)
postButton.layer.cornerRadius = 4.0
postButton.clipsToBounds = true
postButton.titleLabel?.font = UIFont(name: "Lato-Bold", size: 10.0)
postButton.titleLabel?.textAlignment = .Center
postButton.titleLabel?.textColor = UIColor.blackColor()
postButton.setTitle("post", forState: .Normal)
postButtonBar = UIBarButtonItem(customView: postButton)
navigationItem.rightBarButtonItems = [ postButtonBar ]
我尝试了这个,但结果是一个没有标题的红色轮廓按钮。
答案 0 :(得分:2)
如果正确构建按钮,您的代码将正常工作:
let postButton = UIButton(type: .Custom)
postButton.frame = CGRectMake(0, 0, 90, 30)
postButton.layer.borderColor = UIColor.redColor().CGColor
postButton.layer.borderWidth = CGFloat(3.0)
postButton.layer.cornerRadius = 4.0
postButton.clipsToBounds = true
postButton.setTitle("post", forState: .Normal)
postButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
postButton.titleLabel?.font = UIFont(name: "GillSans", size: 10.0)
postButton.setTitleColor(UIColor.grayColor(), forState: .Highlighted)
let postButtonBar = UIBarButtonItem(customView: postButton)
我替换了另一种字体,因为我没有你的字体...我也在突出显示时添加了一种灰色,这样按钮在点击时就会响应。
答案 1 :(得分:0)
我认为您需要使用:
postButton.setTitle("Post", forState: .Normal)
postButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
我不确定,因为我没有在你面前提供完整的代码,但我认为你需要在按钮上添加一个动作才能点击它:
postButton.addTarget(self, action: "functionName", forControlEvents: .TouchUpInside)