gpt4 book ai didi

swift - 使用 VFL 添加约束 - iOS Swift

转载 作者:行者123 更新时间:2023-11-30 13:42:16 25 4
gpt4 key购买 nike

我正在了解 VFL,并且正在尝试一些我需要帮助的东西。

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

横向 View 如下所示

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

我怎样才能做到这一点。肖像模式看起来不错,在横向模式下,图像必须垂直居中。

谢谢。

编辑

更新的代码:

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

最佳答案

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

首先,您实际上需要将约束保存在属性中以用于停用目的。在你的 View Controller 中:

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

接下来,将其添加到您的 View Controller :

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

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

super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

}

关于swift - 使用 VFL 添加约束 - iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35418869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com