使用VFL添加约束 - iOS Swift

时间:2016-02-15 20:53:29

标签: swift autolayout

我正在向VFL介绍我正在尝试一些我需要帮助的东西。

我在纵向模式下将图像视图对齐,如下图所示。 enter image description here

在横向视图中,视图如下所示

enter image description here

在横向中,我希望图像沿Y轴居中,即比图像中显示的位置稍微高一些。

我有以下VFL代码

override func viewDidLoad() {

    googleSignButton = UIImageView()
    googleSignButton!.translatesAutoresizingMaskIntoConstraints = false
    googleSignButton!.image = UIImage(named: "google.png")
    self.view!.addSubview(googleSignButton!)

    let buttonWidth:CGFloat = 50
    let views:[String:UIImageView] = Dictionary(dictionaryLiteral: ("google",googleSignButton!))
    let metrics = ["dim" : buttonWidth,
                       "horizontalPadding" : (UIScreen.mainScreen().bounds.width - (3 * buttonWidth)) / 3,
            "verticalPadding" : (UIScreen.mainScreen().bounds.height * 0.5) - (buttonWidth * 0.5)]

        let horizontalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|-horizontalPadding-[google(dim)]", options:[] , metrics: metrics, views: views)
        let verticalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-verticalPadding-[google(dim)]", options:[] , metrics: metrics, views: views)
        self.view.addConstraints(verticalConstraint)
        self.view.addConstraints(horizontalConstraint)
}

我怎样才能做到这一点。 Potrait模式看起来很好,在横向上,图像必须垂直居中。

谢谢。

修改

更新代码:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

        coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in



            }) { (UIViewControllerTransitionCoordinatorContext) -> Void in

                self.calculateConstraints()

        }
    }

    func calculateConstraints() {

        let buttonWidth:CGFloat = Common.sharedCommon.calculateDimensionForDevice(50)

        let views:[String:UIImageView] = Dictionary(dictionaryLiteral: ("google",googleSignButton!))
        let metrics = ["dim" : 50.0,
            "horizontalPadding" : (UIScreen.mainScreen().bounds.width - (3 * buttonWidth)) / 3,
            "verticalPadding" : (UIScreen.mainScreen().bounds.height * 0.5) - (buttonWidth * 0.5)]


        if let horizontal = horizontalConstraint, let vertical = verticalConstraint {

            NSLayoutConstraint.deactivateConstraints([horizontal])
            NSLayoutConstraint.deactivateConstraints([vertical])
        }

        horizontalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|-horizontalPadding-[google(dim)]", options:[] , metrics: metrics, views: views)[0]
        verticalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-verticalPadding-[google(dim)]", options:[] , metrics: metrics, views: views)[0]

        NSLayoutConstraint.activateConstraints([horizontalConstraint!,verticalConstraint!])

}

图片: enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

所以你的问题是约束不会在旋转时得到更新,因此它对横向使用相同的垂直约束。

首先,您实际上希望在属性中保存约束以用于停用目的。在视图控制器中:

var verticalConstraints: [NSLayoutConstraint]?
var horizontalConstraints: [NSLayoutConstraint]?

然后,将约束代码包装在一个函数中(注意我稍微修改了一下):

function calculateButtonConstraints() {
    let buttonWidth:CGFloat = 50
    let views:[String:UIImageView] = Dictionary(dictionaryLiteral: ("google",googleSignButton!))
    let metrics = ["dim" : buttonWidth,
                   "horizontalPadding" : (UIScreen.mainScreen().bounds.width - (3 * buttonWidth)) / 3,
        "verticalPadding" : (UIScreen.mainScreen().bounds.height * 0.5) - (buttonWidth * 0.5)]

    if let horizontal = horizontalConstraints, let vertical = verticalConstraints {
        NSLayoutConstraint.deactivateConstraints(horizontal)
        NSLayoutConstraint.deactivateConstraints(vertical)
    }

    horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-horizontalPadding-[google(dim)]", options:[] , metrics: metrics, views: views)
    verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-verticalPadding-[google(dim)]", options:[] , metrics: metrics, views: views)

    NSLayoutConstraint.activateConstraints(horizontalConstraints!)
    NSLayoutConstraint.activateConstraints(verticalConstraints!)
}

请注意,我使用的是NSLayoutConstraint.activateConstraints:,因为这是首选方式。此外,这部分与deactivateConstraints:

然后,代替viewDidLoad:中的代码,输入:

calculateButtonConstraints()

接下来,将其添加到视图控制器:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    coordinator.animateAlongsideTransition(nil, completion: { [unowned self]
        self.calculateButtonConstraints()
    })

    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

}