2014-10-04 49 views
0

视图以324像素的高度加载。然后,当我打开设备横向时,视图将其高度更新为250像素,但在将设备从横向转换为纵向后,它不会将视图的高度重置为324像素。如何在方向更改时更新uiview大小?

import UIKit 

class KeyboardViewController: UIInputViewController { 
var calcBoard = CalcControl() 
var disNum:Double = 0.0 
var expandedHeight:CGFloat = 250 


override func updateViewConstraints() { 
    super.updateViewConstraints() 

      // Add custom view sizing constraints here 
} 
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { 

    //self.view.addConstraint(heightconstraints2) 
    var heightconstraints3:NSLayoutConstraint = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0.0, constant: self.expandedHeight) 
    //self.view.addConstraint(heightconstraints2) 
    if toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft{ 

      self.expandedHeight = 250 


     self.view.addConstraint(heightconstraints3)  } 
    if toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight{ 

      self.expandedHeight = 250 


     self.view.addConstraint(heightconstraints3) 
    } 
    if toInterfaceOrientation == UIInterfaceOrientation.Portrait{ 
     println("In portrait") 
         println("Transforming") 
      self.expandedHeight = 324 


     self.view.addConstraint(heightconstraints3) 
    } 






} 
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) { 
    if fromInterfaceOrientation == UIInterfaceOrientation.Portrait{ 
     println("Adjusting...") 
     self.expandedHeight = 324 
    } 
} 
override func viewDidLoad() { 
    super.viewDidLoad() 
    println("Loaded") 


    NSNotificationCenter.defaultCenter().addObserver(self, selector: "LoadNumber:", name: "load", object: nil) 
    self.deleteBackward() 
    // Perform custom UI setup here 
    //self.calcBoard.frame = CGRect(x: 123, y: 0, width: 320, height: 324) 

    var xibView1 = NSBundle.mainBundle().loadNibNamed("CalcView", owner: nil, options: nil) 

    self.calcBoard = xibView1[0] as CalcControl 
    self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 324) 

    self.view.addSubview(calcBoard) 

} 

func LoadNumber(notification:NSNotification){ 
    println("got it") 
    var num = (calcBoard.number as NSNumber).stringValue 
    println(num) 
    var proxy = textDocumentProxy as UITextDocumentProxy 
    proxy.insertText(num) 
} 
func recieveNumberfromSource()->Double{ 

    return self.disNum 
} 
    func deleteBackward(){ 

} 
func hasText() -> Bool{ 
    return true 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated 
} 

override func textWillChange(textInput: UITextInput) { 
    // The app is about to change the document's contents. Perform any preparation here. 
} 

override func textDidChange(textInput: UITextInput) { 
    // The app has just changed the document's contents, the document context has been updated. 

    var textColor: UIColor 
    var proxy = self.textDocumentProxy as UITextDocumentProxy 
    if proxy.keyboardAppearance == UIKeyboardAppearance.Dark { 
     textColor = UIColor.whiteColor() 
    } else { 
     textColor = UIColor.blackColor() 
    } 

} 
override func viewDidAppear(animated: Bool) { 
    //self.updateViewConstraints() 
    var expandedHeight:CGFloat = 324 
    var heightconstraint:NSLayoutConstraint = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0.0, constant: expandedHeight) 
    self.view.addConstraint(heightconstraint) 

} 
} 

回答

0

当您第一次添加并在方向更改时更新它的常量时,请保留对heightConstraint的引用。

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { 
    if UIInterfaceOrientationIsLandscape(toInterfaceOrientation) { 
     heightConstraint.constant = 100 
    } else { 
     heightConstraint.constant = 300 
    } 
} 

对self.expandedHeight的赋值不会在其创建后更改约束。

相关问题