2016-02-15 82 views
0

我正在將自己介紹給VFL,我嘗試了一些我需要幫助的東西。使用VFL添加約束 - iOS Swift

我有圖像視圖像在縱向模式下的圖像對齊。 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

+0

你在哪裏調用此代碼? – tktsubota

+0

我已更新代碼 – slysid

回答

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) 

} 
+0

我嘗試了您的建議(更正了一些語法錯誤)看起來它正在工作,但圖像視圖看起來比代碼中定義的大小要大得多。 – slysid

+0

@slysid奇怪。你能鏈接新的更大尺寸的圖像嗎? – tktsubota

+0

@slysid我編輯了我的帖子(修正了語法),不知道這個改變了什麼。 – tktsubota