如何使我的按钮可扩展?以编程方式,使用vector或其他东西?它只是一个带三线的典型导航图标.是否有一种简单的方法可以在不使用图像的情况下创建清晰,可扩展的图
是的,您可以使用Core Graphics创建图标.请按照这些简单的4个步骤绘制3条按钮图标.
1)添加UIButton
到您的故事板并放置它.
2)创建基类UIButton的Cocoa类,将其命名为'NavButton'并粘贴以下代码
import UIKit class NavButton: UIButton { override func drawRect(rect: CGRect) { // thickness of your line let lineThick:CGFloat = 1.0 // length of your line relative to your button let lineLenght:CGFloat = min(bounds.width, bounds.height) * 0.8 // color of your line let lineColor: UIColor = UIColor.whiteColor() // this will add small padding from button border to your first line and other lines let marginGap: CGFloat = 5.0 // we need three line for line in 0...2 { // create path let linePath = UIBezierPath() linePath.lineWidth = lineThick //start point of line linePath.moveToPoint(CGPoint( x: bounds.width/2 - lineLenght/2, y: 6.0 * CGFloat(line) + marginGap )) //end point of line linePath.addLineToPoint(CGPoint( x: bounds.width/2 + lineLenght/2, y: 6.0 * CGFloat(line) + marginGap )) //set line color lineColor.setStroke() //draw the line linePath.stroke() } } }
3)通过Identity Inspector> Custom Class> Class字段NavButton
为您UIButton
提供课程
4)从"属性检查器">"默认标题"(第四个)中删除按钮的默认标题
完成,现在构建并运行您可以看到带有条形的按钮