この質問を投稿したのは 9 日前です。
私はそれを理解していないので、コードとスクリーンショットで再投稿しています。
ホスト アプリでは正常に機能するカスタム ボタンがありますが、Today 拡張機能では完全には描画されません。
ストーリーボードでどのように表示され、プレビューがどのように歪んで見えるかを次に示します。
iPhone 5s で実際に表示すると、次のようになります。
これは、Today Extension のロード方法と関係があると思います。これは、どういうわけか、ボタンが完全に描画されていないためです。ただし、これは歪んだプレビューを説明するものではありません (少なくとも私の初心者にとっては)。
ボタンのコードは次のとおりです。
@IBDesignable class TakeButton: UIButton {
@IBInspectable var fillColor: UIColor = UIColor.greenColor()
@IBInspectable var isAddButton: Bool = true
@IBInspectable var isTodayExtensionButton: Bool = false
var plusHeight: CGFloat = 0.0
var plusWidth: CGFloat = 0.0
//
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
var path = UIBezierPath(ovalInRect: rect)
fillColor.setFill()
path.fill()
//set up width and height variables for horizontal stroke
if isTodayExtensionButton {
plusHeight = 2.0
plusWidth = 25.0
}
else {
plusHeight = 3.0
plusWidth = 45.0
}
//create the path
var plusPath = UIBezierPath()
//set the path's line width to the height of the stroke
plusPath.lineWidth = plusHeight
//move the initial point of the path
//to the start of the horizontal stroke
plusPath.moveToPoint(CGPoint(
x: bounds.width/2 - plusWidth/2 + 0.5,
y: bounds.height/2))
//add a point to the path at the end of the stroke
plusPath.addLineToPoint(CGPoint(
x: bounds.width/2 + plusWidth/2 + 0.5,
y: bounds.height/2))
//for vertical line - take button
if isAddButton {
plusPath.moveToPoint(CGPoint(
x: bounds.width/2,
y: bounds.height/2 - plusWidth/2 + 0.5))
plusPath.addLineToPoint(CGPoint(
x: bounds.width/2,
y: bounds.height/2 + plusWidth/2 + 0.5))
}
//set the stroke color
UIColor.whiteColor().setStroke()
//draw the stroke
plusPath.stroke()
}
}