更改方向时如何更新uiview大小?

时间:2014-10-04 17:42:59

标签: ios xcode uiview swift

视图加载高度为324像素。当我转动设备横向时,视图将其高度更新为250像素,但在我将设备从横向变为纵向后,它不会将视图的高度重置为324像素。

  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)

}
}

1 个答案:

答案 0 :(得分:0)

首次添加时保持对heightConstraint的引用,并在方向更改时更新它的常量。

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

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