私は、3つのUIButtons
のビューコントローラーを持ち、それぞれ同じY位置を持ち、水平方向に20px離れています。ボタンが選択されているとき、ボタンの下に線を描きたいと思います。私はアンダーライン属性を追加しようとしましたが、ラインは実際にはカスタマイズできません。 SectionViewという名前のUIViewのサブクラスに含まれるUIImageViewの上にボタンがあります。私のビューコントローラでUIButtonの下線はビューの内側に隠されています
私はボタンが押されたときに呼び出される次の関数ています、私は次のように私のUIViewサブクラスで
@IBOutlet weak var sectionView: SectionView!
func sectionButtonPressed(sender: UIButton) {
self.sectionView.selectedButton = sender
self.sectionView.setNeedsDisplay()
}
を持っている:
class SectionView: UIView {
var selectedButton: UIButton?
override func drawRect(rect: CGRect) {
let linePath = UIBezierPath()
linePath.moveToPoint(CGPoint(x:(self.selectedButton?.frame.origin.x)! - 3, y: (self.selectedButton?.frame.origin.y)! + 35))
linePath.addLineToPoint(CGPoint(x: (self.selectedButton?.frame.origin.x)! + (self.selectedButton?.frame.width)! + 3, y: (self.selectedButton?.frame.origin.y)! + 35))
linePath.closePath()
UIColor.purpleColor().set()
linePath.stroke()
linePath.fill()
}
}
は私が得ることができますUIButtonとUIImageViewの下にある、私が望む位置と長さを描画する線。上に表示するにはどうすればいいですか?
'のCALayerを使用するメリットは何ですか'vs a' UIView' – Brosef
UIViewとはちょっと変わり、簡単な方法です – Gokul