2016-03-29 14 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) 
}