2014-10-04 6 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 не изменит ограничение после его создания.

Смежные вопросы