Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS中添加虚线内部视图_Ios_Swift_Uibezierpath_Drawrect - Fatal编程技术网

如何在iOS中添加虚线内部视图

如何在iOS中添加虚线内部视图,ios,swift,uibezierpath,drawrect,Ios,Swift,Uibezierpath,Drawrect,如何在UIView中添加虚线 我的代码: let path = UIBezierPath() let p0 = CGPointMake(CGRectGetMinX(view.bounds), CGRectGetMidY(view.bounds)) path.moveToPoint(p0) let p1 = CGPointMake(CGRectGetMaxX(view.bounds),

如何在UIView中添加虚线

我的代码:

let  path = UIBezierPath()

let  p0 = CGPointMake(CGRectGetMinX(view.bounds),
                              CGRectGetMidY(view.bounds))
path.moveToPoint(p0)

let  p1 = CGPointMake(CGRectGetMaxX(view.bounds),
                              CGRectGetMidY(view.bounds))
path.addLineToPoint(p1)

let  dashes: [ CGFloat ] = [ 16.0, 32.0 ]
path.setLineDash(dashes, count: dashes.count, phase: 0.0)

path.lineWidth = 8.0
path.lineCapStyle = .Butt
UIColor.magentaColor().set()
path.stroke()
view.setNeedsDisplay()
但它没有显示任何内容

我在日志中看到了这一点:

CGContextSetLineDash:无效的上下文0x0。如果要查看回溯,请设置CG_CONTEXT_SHOW_backtrace环境变量

试试这个

let rect = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 180, height: 180))//Set Height width as you want
let layer = CAShapeLayer.init()
let path = UIBezierPath(roundedRect: rect, cornerRadius: 8)
layer.path = path.cgPath;
layer.strokeColor = UIColor(red: 205/255, green: 207/255, blue: 211/255, alpha: 1.0).cgColor; // Set Dashed line Color
layer.lineDashPattern = [7,7]; // Here you set line length
layer.backgroundColor = UIColor.clear.cgColor;
layer.fillColor = UIColor.clear.cgColor;
self.newView.layer.addSublayer(layer);  
希望这会有帮助

试试这个

let rect = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 180, height: 180))//Set Height width as you want
let layer = CAShapeLayer.init()
let path = UIBezierPath(roundedRect: rect, cornerRadius: 8)
layer.path = path.cgPath;
layer.strokeColor = UIColor(red: 205/255, green: 207/255, blue: 211/255, alpha: 1.0).cgColor; // Set Dashed line Color
layer.lineDashPattern = [7,7]; // Here you set line length
layer.backgroundColor = UIColor.clear.cgColor;
layer.fillColor = UIColor.clear.cgColor;
self.newView.layer.addSublayer(layer);  

希望这会有帮助

使用下面的代码将虚线添加到父视图中

for index in 0 ..< 10 {
  let frame : CGRect = CGRectMake(index*10,200,2,30)
  var subview : UIView = UIView(frame: frame)
  testView.backgroundColor = UIColor.gray
  testView.alpha=1.0
  self.view.addSubview(testView)
}
0中索引的
。<10{
let帧:CGRect=CGRectMake(索引*10200,2,30)
变量子视图:UIView=UIView(帧:帧)
testView.backgroundColor=UIColor.gray
testView.alpha=1.0
self.view.addSubview(testView)
}

使用下面的代码将虚线添加到父视图中

for index in 0 ..< 10 {
  let frame : CGRect = CGRectMake(index*10,200,2,30)
  var subview : UIView = UIView(frame: frame)
  testView.backgroundColor = UIColor.gray
  testView.alpha=1.0
  self.view.addSubview(testView)
}
0中索引的
。<10{
let帧:CGRect=CGRectMake(索引*10200,2,30)
变量子视图:UIView=UIView(帧:帧)
testView.backgroundColor=UIColor.gray
testView.alpha=1.0
self.view.addSubview(testView)
}

这对我很有效,我创建了一个框,给出了一个高度,例如1,并添加了一个虚线CAShapeLayer

let line = CAShapeLayer()

let rect = CGRect(x: /*X position*/, y: /*Y position */, width: /*X width of line*/, height: /*height of line*/)

line.path = UIBezierPath(roundedRect: view.bounds, cornerRadius:0).cgPath
line.frame = self.bounds
line.strokeColor = UIColor.lightGray.cgColor
line.fillColor = UIColor.lightGray.cgColor
line.lineDashPattern = [4, 4]
view.layer.addSublayer(line)

希望有帮助

这对我很有效,我创建了一个框,给出了一个高度,例如1,并添加了一个虚线CAShapeLayer

let line = CAShapeLayer()

let rect = CGRect(x: /*X position*/, y: /*Y position */, width: /*X width of line*/, height: /*height of line*/)

line.path = UIBezierPath(roundedRect: view.bounds, cornerRadius:0).cgPath
line.frame = self.bounds
line.strokeColor = UIColor.lightGray.cgColor
line.fillColor = UIColor.lightGray.cgColor
line.lineDashPattern = [4, 4]
view.layer.addSublayer(line)

希望有帮助

您可以添加一个可视化视图来创建您想要创建的视图。可能重复:您是否在
drawRect:
函数中调用它?确保这样做,然后删除
view.setNeedsDisplay()
Swift 2。。。讨厌。。。它已过时。您是否可以添加一个可视化视图来创建您想要创建的视图。可能的重复:您是否在
drawRect:
函数中调用它?确保这样做,然后删除
view.setNeedsDisplay()
Swift 2。。。讨厌。。。它已经过时了。省略
.init
s以获得更简洁的代码,例如:
let rect=CGRect(原点:CGPoint(x:0,y:0),size:CGSize(width:180,height:180))
省略
.init
s以获得更简洁的代码,例如:
let rect=CGRect(原点:CGPoint(x:0,y:0),size:CGSize(width:180,height:180))