2017-12-06 2 views
0

haben ein Problem mit NSLayoutConstraint und .scaleAspectFill nach logoView Anwendung (Positionierung und Skalierung) und Titlelabel Einschränkungen, die Titlelabel Y-Position nicht richtig SätzeSwift 4 NSLayoutConstraint nicht funktioniert

titleLabel.topAnchor.constraint (EqualTo: logoView.bottomAnchor, konstante: 20])

hier mein Beispiel:

let logoView:UIImageView = { 
     let img = UIImage(named: "big_logo") 
     let im = UIImageView(image: img) 
     im.translatesAutoresizingMaskIntoConstraints = false 
     im.contentMode = .scaleAspectFill 
     return im 
}() 

    lazy var titleLabel:UILabel = { 
     let title:UILabel = UILabel() 
     title.translatesAutoresizingMaskIntoConstraints = false 
     title.text = MuiPack.getMuiString(key: "splash_greeting") 
     title.font = UIFont.boldSystemFont(ofSize: 18) 
     title.textColor = .black 
     return title 
    }() 

NSLayoutConstraint.activate([logoView.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.6), 
            logoView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), 
            logoView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -20)]) 

NSLayoutConstraint.activate([titleLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), 
            titleLabel.topAnchor.constraint(equalTo: logoView.bottomAnchor, constant: 20]) 

Antwort

1

Ihre logoView hat keine intrinsicSize, also keine Höhe.

Wenn Sie Ihre Initialisierung wie folgt ändern, werden Sie sehen, was passiert:

let logoView:UIImageView = { 
    let img = UIImage(named: "cross") 
    let im = UIImageView(image: img) 
    im.translatesAutoresizingMaskIntoConstraints = false 
    im.contentMode = .scaleAspectFill 
    // set a background color so you can see the image view's frame 
    im.backgroundColor = .blue 
    // clip the image to the bounds of the view's frame 
    im.clipsToBounds = true 
    return im 
}() 

können Sie entweder eine explizite Höhe Zwang gesetzt, oder es proportional zu seiner Breite festgelegt.

Dies wird es „Platz“ machen:

NSLayoutConstraint.activate([ 
     logoView.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.6), 
     logoView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),    
     logoView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -20), 
     // add proportional height anchor 
     logoView.heightAnchor.constraint(equalTo: logoView.widthAnchor, multiplier: 1.0) 
     ]) 
+0

dank @DonMag wurde Ihre Antwort sehr helfen voll –

Verwandte Themen