可视格式语言:“Swift”图像约束不起作用

时间:2014-12-11 17:06:49

标签: xcode swift visual-format-language

我遇到了构建约束的问题。我是使用Visual Format Language的新手。我需要帮助。我为两个图像设置了约束,高度可以,但水平位置关闭。它们似乎与不同的设备放置在不同的长度。我试图将它们保持在一个按钮的中心位置,但这两个图像只是服从左侧和右侧的-70-,尽管手机的宽度也是如此。如何判断它是否适应手机的宽度?哦,请考虑我有图像setTranslatesAutoresizingMaskIntoConstraints(false)

// Constraint Views
        let views = ["view": view,
                     "topHeader": topHeader,
                     "topBannerView": topBannerView,
                     "backGroundView": backGroundView,
                     "wolLogoView":wolLogoView,
                     "backGround2View": backGround2View,
                     "pictureView": pictureView,
                     "wolMottoView": wolMottoView,
                     "menuButton": menuButton,
                     "buttonMenuView": buttonMenuView,
                     "featListings": featListings,
                     "scrollView": scrollView,
                     "button1": button1,
                     "busLogoView": busLogoView,
                     "button2": button2,
                     "classLogoView": classLogoView,
                     "button3": button3,
                     "forwardLogoView": forwardLogoView,
            ]

        // CONSTRAINTS -----------------------------------------------------

        // backGround
        var constHBackground = NSLayoutConstraint.constraintsWithVisualFormat("H:|[backGround2View]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHBackground)
        var constVBackground = NSLayoutConstraint.constraintsWithVisualFormat("V:|-17-[backGround2View(>=100)]-0-[backGroundView(==backGround2View)]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVBackground)


        // topHeader
        var constHHeader = NSLayoutConstraint.constraintsWithVisualFormat("H:[view]-(<=0)-[topHeader(==view)]", options: .AlignAllTop, metrics: nil, views: views)
        view.addConstraints(constHHeader)
        var constVHeader = NSLayoutConstraint.constraintsWithVisualFormat("V:[view]-(<=0)-[topHeader(17)]", options: .AlignAllCenterX, metrics: nil, views: views)
        view.addConstraints(constVHeader)


        // topBannerView
        var constHBanner = NSLayoutConstraint.constraintsWithVisualFormat("H:|[topBannerView(==view)]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHBanner)
        var constVBanner = NSLayoutConstraint.constraintsWithVisualFormat("V:|-17-[topBannerView(50)]|", options: .AlignAllCenterX, metrics: nil, views: views)
        view.addConstraints(constVBanner)

        // wolLogo
        var constHWolLogo = NSLayoutConstraint.constraintsWithVisualFormat("H:|-55-[wolLogoView(>=0)]-60-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHWolLogo)
        var constVWolLogo = NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[wolLogoView(28)]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVWolLogo)

        // picture view
        var constHPic = NSLayoutConstraint.constraintsWithVisualFormat("H:|[pictureView]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHPic)
        var constVPic = NSLayoutConstraint.constraintsWithVisualFormat("V:|-85-[pictureView(>=0)]-0-[backGroundView]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVPic)

        // wolMotto
        var constHWolMotto = NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[wolMottoView(>=0)]-20-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHWolMotto)
        var constVWolMotto = NSLayoutConstraint.constraintsWithVisualFormat("V:|-69-[wolMottoView(35)]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVWolMotto)

        // menuButton
        var constHMenuButton = NSLayoutConstraint.constraintsWithVisualFormat("H:|[menuButton(40)]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHMenuButton)
        var constVMenuButton = NSLayoutConstraint.constraintsWithVisualFormat("V:|-22-[menuButton(40)]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVMenuButton)

        // buttonMenuView
        var constHButtonMenu = NSLayoutConstraint.constraintsWithVisualFormat("H:|[buttonMenuView(==menuButton)]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHButtonMenu)
        var constVButtonMenu = NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[buttonMenuView(==menuButton)]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVButtonMenu)

        // featListings
        var constHFeatListing = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[featListings]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHFeatListing)
        var constVFeatListing = NSLayoutConstraint.constraintsWithVisualFormat("V:|-85-[pictureView][featListings][scrollView]-130-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVFeatListing)

        // button3
        var constHButton3 = NSLayoutConstraint.constraintsWithVisualFormat("H:|-(>=0)-[button3(100)]-(<=0)-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHButton3)
        var constVButton3 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-85-[pictureView][button3][scrollView]-130-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVButton3)

        // forwardLogoView
        var constHForwardLogoView = NSLayoutConstraint.constraintsWithVisualFormat("H:|-(>=0)-[forwardLogoView(23)]-(<=0)-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHForwardLogoView)
        var constVForwardLogoView = NSLayoutConstraint.constraintsWithVisualFormat("V:|-85-[pictureView][forwardLogoView(>=0)][scrollView]-130-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVForwardLogoView)

        // scrollView
        var constHScrollView = NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHScrollView)
        var constVScrollView = NSLayoutConstraint.constraintsWithVisualFormat("V:|-85-[pictureView]-30-[scrollView(>=0)]-130-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVScrollView)

        // button1 and button2
        var constHButton1 = NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[button1(<=220)]-20-[button2(==button1)]-15-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHButton1)
        var constVButton1 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-85-[pictureView]-30-[scrollView(>=0)]-20-[button1(80)]-25-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVButton1)
        var constVButton2 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-85-[pictureView]-30-[scrollView(>=0)]-20-[button2(80)]-25-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVButton2)

        // busLogoView and classLogoView
        var constHBusLogoView = NSLayoutConstraint.constraintsWithVisualFormat("H:|-50-[busLogoView]-100-[classLogoView(==busLogoView)]-50-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constHBusLogoView)
        var constVBusLogoView = NSLayoutConstraint.constraintsWithVisualFormat("V:|-85-[pictureView]-30-[scrollView(>=0)]-20-[busLogoView(<=50)]-45-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVBusLogoView)
        var constVClassLogoView = NSLayoutConstraint.constraintsWithVisualFormat("V:|-85-[pictureView]-30-[scrollView(>=0)]-20-[classLogoView(==busLogoView)]-45-|", options: nil, metrics: nil, views: views)
        view.addConstraints(constVClassLogoView)

我知道这很难看哈哈。就像我说这是我第一次做约束。遗憾!

1 个答案:

答案 0 :(得分:2)

好的,只需编辑约束以删除过度约束的视图,我就可以将其归结为......

// busLogoView and classLogoView
var constHBusLogoView = NSLayoutConstraint.constraintsWithVisualFormat("|-70-[busLogoView][classLogoView(==busLogoView)]-70-|", 
                                                                       options: nil,
                                                                       metrics: nil,
                                                                       views: views)
view.addConstraints(constHBusLogoView)
var constVBusLogoView = NSLayoutConstraint.constraintsWithVisualFormat("V:|-85-[pictureView]-30-[scrollView]-20-[busLogoView(<=50)]-45-|",
                                                                       options: nil,
                                                                       metrics: nil,
                                                                       views: views)
view.addConstraints(constVBusLogoView)
var constVClassLogoView = NSLayoutConstraint.constraintsWithVisualFormat("V:[scrollView]-20-[classLogoView(==busLogoView)]-45-|",
                                                                         options: nil,
                                                                         metrics: nil,
                                                                         views: views)
view.addConstraints(constVClassLogoView)

关于这些变化的一些注释。

  1. 向宽度或高度添加>=0约束是多余的,因为所有视图在AutoLayout中都必须具有正大小。所以你根本不能添加约束。
  2. 在第二组约束中,您已经定位了图片视图和滚动视图。您无需再次将它们放在第三组中。
  3. 在第一组中添加了]-0-[空格。您可以使用][
  4. 执行此操作
  5. 在第一组约束中,您在总线和类视图之间添加了相等的宽度约束。但是你也说公共汽车应该是<=50,你也给出了从超级视图的左边到右边一直有的约束。这是危险的,因为如果超级视图更加棕褐色240(70 + 70 + 50 + 50)那么这些约束将相互冲突(类和总线不能是相同的宽度,总线小于或等于50宽度,因为这不能添加高达240以上。
  6. 我怀疑它可能是导致你的问题的第4号(可能不是),但应用这些修正程序让我知道会发生什么。

    修改

    事实上,再次看起来你已经将图片视图和滚动视图限制在其他地方,所以你根本不需要图片视图......

    // busLogoView and classLogoView
    var constHBusLogoView = NSLayoutConstraint.constraintsWithVisualFormat("|-70-[busLogoView][classLogoView(==busLogoView)]-70-|", 
                                                                           options: nil,
                                                                           metrics: nil,
                                                                           views: views)
    view.addConstraints(constHBusLogoView)
    var constVBusLogoView = NSLayoutConstraint.constraintsWithVisualFormat("V:[scrollView]-20-[busLogoView(<=50)]-45-|",
                                                                           options: nil,
                                                                           metrics: nil,
                                                                           views: views)
    view.addConstraints(constVBusLogoView)
    var constVClassLogoView = NSLayoutConstraint.constraintsWithVisualFormat("V:[scrollView]-20-[classLogoView(==busLogoView)]-45-|",
                                                                             options: nil,
                                                                             metrics: nil,
                                                                             views: views)
    view.addConstraints(constVClassLogoView)