简体   繁体   English

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

[英]How to update uiview size when orientation is changed?

The view loads with a height of 324 pixels. 视图加载的高度为324像素。 Then view updates its height to 250 pixels when I turn the device landscape, but after I turn the device from landscape to portrait it doesn't reset the height of the view back to 324 pixels. 然后,当我将设备横向旋转时,视图会将其高度更新为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)

}
}

Keep a reference to the heightConstraint when you first add it and update the constant of it when the orientation changes. 第一次添加heightConstraint时,请保持引用,并在方向更改时更新其常数。

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

An assignment to self.expandedHeight will not change the constraint after its creation. self.expandedHeight的分配在创建约束后不会更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM