2016-03-29 15 views
2

Ich versuche, eine benutzerdefinierte UIView erstellen, die als Fortschrittsbalken oder ähnliches (@IBDesignable mit IBInspectable s) verwendet werden kann. Ich möchte eine zentrierte (hauptsächlich X-Achse, aber jetzt auch Y) UILabel in dieser Ansicht.UILabel wird nicht zentriert in 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) 
} 

In Interface die Label-Zentren gerade fein:

Interface Builder centered label

Allerdings, wenn es auf meinem Gerät (iPhone 5S) läuft das Etikett leicht nach rechts ausgerichtet ist (Bild unten). Ich habe verschiedene Ansätze ausprobiert (z. B. das Erstellen des Etikettenrahmens self.frame), aber es ist immer noch nicht richtig zentriert. Was vermisse ich?

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

Align UILabel Zentrum durch Autolayouts in Nib/Storyboard-Datei. –

Antwort

2

Das Problem ist label.center = self.center, weil das Zentrum des Etiketts und der Mitte des Superview in verschiedenen Systemen koordinieren sind, so dass sie nicht die gleiche sein könnte. Verwenden Sie stattdessen

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

Vielen Dank für die Erklärung und die Lösung! – Mattias

0

Versuchen Sie folgendes:

self.label.center = view.center 
0

Fügen Sie die UILabel programmatisch und Ändern der UILabel Rahmen von

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

Um

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

ELSE

Implementieren Automatische Anordnung

0

Eine mögliche Lösung sein kann: -

@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) 
} 
Verwandte Themen