Wie kann ich programmgesteuert hinzufügen einer Ansicht rechts unter der Navigationsleiste?

Ich versuche Sie zum hinzufügen einer Ansicht zu einem UINavigationController mit seiner Oberseite bündig mit der Navigationsleiste unten.

Ich habe versucht, mit Einschränkungen, indem Sie den folgenden zu meinem UINavigationController Unterklasse:

override func viewDidAppear(_ animated: Bool) {
           self.label = UILabel()
    self.label?.translatesAutoresizingMaskIntoConstraints = false
    self.label?.backgroundColor = UIColor.red
    self.label?.text = "label text"
    self.view.addSubview(self.label!)
    let horConstraint = NSLayoutConstraint(item: label!, attribute: .top, relatedBy: .equal,
                                           toItem: topLayoutGuide, attribute: .bottom,
                                           multiplier: 1.0, constant: 0.0)
    let widthConstr = NSLayoutConstraint(item: label!, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)

    let heightConstr = NSLayoutConstraint(item: label!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)
    view.addConstraints([horConstraint, widthConstr, heightConstr])
}

Mit diesem Ergebnis:

Wie kann ich programmgesteuert hinzufügen einer Ansicht rechts unter der Navigationsleiste?

Und ich habe versucht, durch die Einstellung der frame meines Untersicht:

override func viewDidAppear(_ animated: Bool) {

    self.label = UILabel(frame: CGRect(x: 0, y: navigationBar.frame.height, width: 300, height: 100))
    self.label?.translatesAutoresizingMaskIntoConstraints = false
    self.label?.backgroundColor = UIColor.red
    self.label?.text = "label text"
    self.view.addSubview(self.label!)

}

Und das kam heraus:

Wie kann ich programmgesteuert hinzufügen einer Ansicht rechts unter der Navigationsleiste?

In beiden Fällen mein label deckt einen Teil der Navigationsleiste. Wie kann ich dieses Problem beheben?

  • versuchen Sie, geben Sie die y-position 64
InformationsquelleAutor Spaci Tron | 2017-07-07
Schreibe einen Kommentar