简体   繁体   English

Swift 4 NSLayoutConstraint无法正常工作

[英]Swift 4 NSLayoutConstraint not works properly

have a problem with NSLayoutConstraint and .scaleAspectFill after applying logoView (positioning and scaling ) and titleLabel constraints, titleLabel's Y position not sets correctly 有一个问题NSLayoutConstraint.scaleAspectFill正确运用logoView(定位和缩放 )和titleLabel限制,titleLabel的Y位置不设置后

titleLabel.topAnchor.constraint(equalTo: logoView.bottomAnchor, constant: 20]) titleLabel.topAnchor.constraint(equalTo:logoView.bottomAnchor,常数:20])

here my sample: 这是我的样本:

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])

Your logoView does not have an intrinsicSize, so it has no height . 您的logoView没有internalSize,因此没有height

If you change your initialization as follows, you'll see what's happening: 如果按以下方式更改初始化,则会看到发生了什么:

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
}()

You can either set an explicit height constraint, or set it proportional to its width. 您可以设置一个明确的高度约束,也可以与其宽度成比例设置。

This will make it "square": 这将使其成为“正方形”:

    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)
        ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM