我正在將自己介紹給VFL,我嘗試了一些我需要幫助的東西。使用VFL添加約束 - iOS Swift
在橫向視圖看起來如下
在景觀欲沿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!])
}
你在哪裏調用此代碼? – tktsubota
我已更新代碼 – slysid