2016-03-29 13 views
2

我正在嘗試創建可用作進度欄或類似的自定義UIView@IBDesignableIBInspectable s)。我想在這個視圖中居中(主要是X軸,但現在也是Y)UILabelUILabel將不會在UIView中心

private func addLabel() 
{ 
    let label = UILabel(frame: CGRectMake(0, 0, self.frame.width/4, self.frame.height/4)) 
    label.center = self.center 
    label.textAlignment = .Center 
    label.text = "5" 
    //Color just to see the whole label 
    label.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5) 
    self.addSubview(label) 
} 

InterfaceBuilder下的標籤中心就好了:

Interface Builder centered label

然而,我的設備上運行時,它(iPhone 5S)標籤稍微向右排列(如下圖所示)。我嘗試過不同的方法(如製作標籤框架self.frame),但它仍然沒有正確居中。我錯過了什麼?

UILabel on device

override func drawRect(rect: CGRect) { 
    // Drawing code 
    let center = CGPoint(x:bounds.width/2, y: bounds.height) 
    let radius: CGFloat = max(bounds.width, bounds.height) 
    let startAngle: CGFloat = π 
    let endAngle: CGFloat = 2 * π 

    path = UIBezierPath(arcCenter: center, radius: radius/2 - arcWidth/2, startAngle: startAngle, endAngle: endAngle, clockwise: true) 
    path.lineWidth = arcWidth 

    arcBackgroundColor.setStroke() 
    path.stroke() 

    self.addLayer() 
} 

private func addLayer() 
{ 
    progressLayer = CAShapeLayer() 
    progressLayer.path = self.path.CGPath 
    progressLayer.strokeColor = self.progressColor.CGColor 
    progressLayer.fillColor = UIColor.clearColor().CGColor 
    progressLayer.lineWidth = self.arcWidth 

    if animatesOnDraw && progress > 0 { 
     self.layer.addSublayer(progressLayer) 
     self.animateProgress(0) 

    } else { 
     progressLayer.strokeEnd = CGFloat(self.progress/self.maxValue) 
     progressLayer.opacity = Float(self.progressStrokeEndValue) 
     self.layer.addSublayer(progressLayer) 
    } 


     addLabel() //as shown above 

} 
+0

通過自動版式對齊的UILabel中心在筆尖/故事板文件。 –

回答

2

問題是label.center = self.center,因爲標籤和上海華中心的中心是在不同的座標系所以他們可能是不一樣的。改用

label.center = GCPoint(x:self.bounds.size.width/2.0, y:self.bounds.size.height/2.0) 
+0

感謝您的解釋和解決方案! – Mattias

0

試試這個:

self.label.center = view.center 
0

添加的UILabel編程和

let label = UILabel(frame: CGRectMake(0, 0, self.frame.width/4, self.frame.height/4)) 

修改的UILabel幀

let label = UILabel(frame: CGRectMake(self.frame.width/2 - (self.frame.height/4)/2, 0, self.frame.width/4, self.frame.height/4)) 

ELSE

實現自動佈局

0

一種可能的解決方案可以是: -

@IBOutlet customView : UIView! 
//Let customView be the outlet of your custom view 

func addLabel() { 
    let CenterY = customView.center.y 
    let CenterX = customView.center.x 
    let heightOfCustom = customView.frame.size.height 
    let widthOfCustom = customView.frame.size.width 
    // If you want a label with height and width 1/4th of the width of the custom view 
    let label = UILabel(frame : CGRectMake(CenterX - widthOfCustom/8, CenterY - heightOfCustom/8, widthOfCustom/4, heightOfCustom/4)) 
    label.text = "5" 
    label.textAlignment = .Center 
    label.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5) 
    self.addSubview(label) 
}