Ios 为swift 2中的按钮创建可缩放的三行菜单导航图标
如何使我的按钮可伸缩?通过编程,使用向量还是其他什么?它只是一个典型的三线导航图标。有没有一种简单的方法可以在不使用图像的情况下创建清晰、可缩放的图标?是的,您可以使用核心图形创建图标。请按照以下4个简单步骤绘制3条按钮图标 1) 将Ios 为swift 2中的按钮创建可缩放的三行菜单导航图标,ios,swift,vector,uibutton,icons,Ios,Swift,Vector,Uibutton,Icons,如何使我的按钮可伸缩?通过编程,使用向量还是其他什么?它只是一个典型的三线导航图标。有没有一种简单的方法可以在不使用图像的情况下创建清晰、可缩放的图标?是的,您可以使用核心图形创建图标。请按照以下4个简单步骤绘制3条按钮图标 1) 将ui按钮添加到情节提要并放置它。 2) 创建基类UIButton的Cocoa类,将其命名为“NavButton” 并粘贴以下代码 3) 将NavButton类从身份检查器>自定义类>类字段分配到您的UIButton 4) 从属性检查器>默认标题(第四个)中删除
ui按钮
添加到情节提要并放置它。
2) 创建基类UIButton的Cocoa类,将其命名为“NavButton”
并粘贴以下代码
3) 将NavButton
类从身份检查器>自定义类>类字段分配到您的UIButton
4) 从属性检查器>默认标题(第四个)中删除按钮的默认标题
完成后,现在构建并运行您可以看到带条的按钮
Swift 3的更新:
import UIKit
class NavButton: UIButton {
override func draw(_ rect: CGRect) {
// thickness of your line
let lineThick:CGFloat = 1.0
// length of your line relative to your button
let lineLength:CGFloat = min(bounds.width, bounds.height) * 0.8
// color of your line
let lineColor: UIColor = UIColor.black
// 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.move(to: CGPoint(
x: bounds.width/2 - lineLength/2,
y: 6.0 * CGFloat(line) + marginGap
))
//end point of line
linePath.addLine(to: CGPoint(
x: bounds.width/2 + lineLength/2,
y: 6.0 * CGFloat(line) + marginGap
))
//set line color
lineColor.setStroke()
//draw the line
linePath.stroke()
}
}
欢迎来到SO。你的问题太宽泛了,你能告诉我们到目前为止你做了什么吗?请看,您可以通过设置其背景色使包含UILable的UIView变得可扩展,并使其类似于导航栏按钮。当然,它是可伸缩的,它使用核心图形路径和填充,它是矢量。
import UIKit
class NavButton: UIButton {
override func draw(_ rect: CGRect) {
// thickness of your line
let lineThick:CGFloat = 1.0
// length of your line relative to your button
let lineLength:CGFloat = min(bounds.width, bounds.height) * 0.8
// color of your line
let lineColor: UIColor = UIColor.black
// 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.move(to: CGPoint(
x: bounds.width/2 - lineLength/2,
y: 6.0 * CGFloat(line) + marginGap
))
//end point of line
linePath.addLine(to: CGPoint(
x: bounds.width/2 + lineLength/2,
y: 6.0 * CGFloat(line) + marginGap
))
//set line color
lineColor.setStroke()
//draw the line
linePath.stroke()
}
}