2017-05-16 1 views
0

Hier ist mein Code:Etikett oben auf dem Bildschirm zeigt stattdessen auf die inputAccessoryView des Seins

var messageView : UITextView = { 
     var textView = UITextView() 
     textView.text = " Add your message here" 
     textView.textColor = UIColor.lightGrayColor() 
     textView.translatesAutoresizingMaskIntoConstraints = false 
     textView.backgroundColor = UIColor.lightGrayColor() 
     textView.layer.cornerRadius = 3 
     textView.clipsToBounds = true 
     textView.keyboardAppearance = .Dark 
     textView.layer.borderWidth = 1.0 
     textView.layer.borderColor = UIColor.lightGrayColor() 
     textView.autocorrectionType = .no 


     // MARK: Setup accesorryView 

     let label = UILabel() 
     label.text = "You have a 100 character limit" 
     label.translatesAutoresizingMaskIntoConstraints = false 

     let accessoryView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 44)) 
     accessoryView.backgroundColor = UIColor.redColor() 

     accessoryView.addSubview(label) 

     accessoryView.leadingAnchor.constraintEqualToAnchor(label.leadingAnchor, constant: 18) 
     accessoryView.centerYAnchor.constraintEqualToAnchor(label.centerYAnchor) 

     textView.inputAccessoryView = accessoryView 

     return textView 
    }() 

Ich versuche, eine inputAccessoryView meiner Textview-Tastatur hinzuzufügen. Mein inputAccessoryView muss ein Etikett haben sagen: „Sie haben eine 100-Zeichen-Grenze“ ...

Aber mein aktuelles Ergebnis ist wie:

enter image description here

Der Text in den blauen ... genau ist die Etikett ich möchte in der inputAccessoryView sein, aber es ist auf der Oberseite meines Bildschirms ...

+0

benötigen Sie nur einen Text zB „Sie haben eine 100 Zeichenbegrenzung "wird in' inputAccessoryView' angezeigt. – Maddy

+0

@Maddy Ich muss auch eine weitere Beschriftung auf der rechten Seite der inputAccessoryView hinzufügen, um als Zähler zu fungieren, dh die Anzahl der verbleibenden Zeichen zu geben ... – Honey

+0

Sie sind clea Roly fehlt Frame für das Label –

Antwort

1

Sie benötigen translatesAutoresizingMaskIntoConstraints auf dem Etikett false und isActive zu true auf die Einschränkungen einzustellen. Grundsätzlich werden Ihre Constraints Code sollte wie folgt aussehen:

accessoryView.leadingAnchor.constraintEqualToAnchor(label.leadingAnchor, constant: 18).isActive = true 
accessoryView.centerYAnchor.constraintEqualToAnchor(label.centerYAnchor).isActive = true 
+0

das Ergebnis ist das gleiche ... keine Verbesserung. Hast du sonst noch was? Ich werde meine Frage aktualisieren, um Ihre Antwort zu reflektieren, aber ... – Honey

+0

Ich habe eine Bearbeitung ... muss ich das für die AccessoryView selbst tun, oder das ist nicht notwendig? (Ich habe das auch versucht, aber ich will nur wissen, ob das auch nötig ist?) – Honey

+0

Warum muss ich 'isActive' auf" true "setzen? Ich musste es fast nie irgendwo einstellen ... – Honey

0

Wie pro mein Verständnis, versuchen Sie dies:

Swift 3

let accessoryView = UIView() 
let label   = UILabel() 
let counterLabel = UILabel()//This is the counter label 

label.text  = "You have a 100 character limit" 
counterLabel.text = "100" 

accessoryView.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44) 

accessoryView.backgroundColor = UIColor.red 

accessoryView.addSubview(label) 
accessoryView.addSubview(counterLabel) 

// to setup contraint set below property to false. 
label.translatesAutoresizingMaskIntoConstraints = false 
counterLabel.translatesAutoresizingMaskIntoConstraints = false 

//label constrint with 0 padding from left side. To change padding from left and right side, change the constant value. 
accessoryView.leadingAnchor.constraint(equalTo: label.leadingAnchor, constant: 0).isActive = true 

accessoryView.centerYAnchor.constraint(equalTo: label.centerYAnchor).isActive = true 

//counterl=Label constrint with 0 padding from right side 
     accessoryView.trailingAnchor.constraint(equalTo:counterLabel.trailingAnchor, constant: 0).isActive = true 

accessoryView.centerYAnchor.constraint(equalTo: counterLabel.centerYAnchor).isActive = true 

textView.inputAccessoryView = accessoryView 
+0

Während dies nicht falsch ist, verwendet dies nicht AutoLayout als OP wollte. Siehe meine aktualisierte Antwort. –

+0

@MihaiFratu dies ist das komplette ans wer das OP will – Maddy

+0

True :) Aber das ist nicht, was ich kommentiert habe;) http://StackOverflow.com/Revisions/44009957/1 –

Verwandte Themen