我一直试图在我的视图和按钮上添加一个图层,但不知何故该图层根本没有出现。我错过了什么?
class ViewController: UIViewController {
var button1: CustomButton!
override func viewDidLoad() {
super.viewDidLoad()
button1 = CustomButton(frame: CGRectZero)
let views1 = ["button1": button1]
button1.backgroundColor = .clearColor()
view.addSubview(button1)
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-50-[button1(50)]", options: [], metrics: nil, views: views1))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[button1(50)]-50-|", options: [], metrics: nil, views: views1))
}
}
class CustomButton: UIButton {
let shape = CAShapeLayer()
override init(frame: CGRect) {
super.init(frame: frame)
translatesAutoresizingMaskIntoConstraints = false
shape.fillColor = UIColor.redColor().CGColor
shape.strokeColor = UIColor.blackColor().CGColor
shape.lineWidth = 0.5
shape.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
layer.insertSublayer(shape, atIndex: 0)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
let padding: CGFloat = 10
let x = frame.origin.x - padding
let y = frame.origin.y - padding
let widths = bounds.width + 2 * padding
let height = bounds.height + 2 * padding
let rect = CGRect(x: x, y: y, width: widths, height: height)
shape.frame = rect
shape.path = UIBezierPath(rect: rect).CGPath
}
}
按钮被添加到视图中没有任何问题但是图层看起来没有任何可见(在点击等时也试图改变颜色,设置位置或只是编辑图层本身而不添加新图层。)
答案 0 :(得分:0)
你错过了'|'在约束字符串中。请更新为,
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'name',
['class' => 'yii\grid\ActionColumn',
'header' => 'Action',
'template' => '{view} {edit} {delete}',
'buttons' => [
'view' => function ($url, $model) {
return Html::a('View', $url);
},
'edit' => function ($url, $model) {
return Html::a('Edit', $url);
},
'delete' => function ($url, $model) {
return Html::a('Delete', $url);
},
],
],
],
]);