2015-01-07 115 views
0

你好我試圖改變一個按鈕的高度設置爲inputAccessoryView一旦我點擊它。動畫inputAccessoryView的高度Swift

不幸的是,它似乎只能改變位置,而不是高度。

任何人都可以幫我嗎?在我的代碼下面。

謝謝!

import UIKit 

class ViewController: UIViewController, UITextFieldDelegate { 



@IBOutlet var textField: UITextField! 

    var SendButton: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     SendButton.frame = CGRectMake(0, 0, UIScreen.mainScreen().applicationFrame.width, 30) 
     SendButton.backgroundColor = UIColor(red: 184/255.0, green: 56/255.0, blue: 56/255.0, alpha: 1) 
     SendButton.setTitle("Send", forState: .Normal) 
     SendButton.addTarget(self, action: "sendNotification", forControlEvents: UIControlEvents.AllEvents) 
    } 

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

    func sendNotification(){ 

     UIView.animateWithDuration(1, animations: { 
      self.SendButton.frame = CGRectMake(0, -340, UIScreen.mainScreen().applicationFrame.width, 30) 
      self.SendButton.backgroundColor = UIColor(red: 300/255.0, green: 56/255.0, blue: 56/255.0, alpha: 1) 
      self.SendButton.setTitle("Hello", forState: .Normal) 

     }) 
    } 


    func textFieldDidBeginEditing(textField: UITextField) { 
     textField.inputAccessoryView = self.SendButton 
    } 

} 

回答

1

在你的動畫塊中,你正在改變CGRectMake框架的y位置而不是高度。這就是爲什麼這個位置正在變化,而不是高度。

UIView.animateWithDuration(1, animations: { 
// Your problem is the line below 
     self.SendButton.frame = CGRectMake(0, -340, UIScreen.mainScreen().applicationFrame.width, 30) 
    }) 

功能CGRectMake的功能參數爲
CGRectMake(xPosition, yPosition, width, height)

更改下面到你想要的高度的代碼。

UIView.animateWithDuration(1, animations: { 
     self.SendButton.frame = CGRectMake(0, 0, UIScreen.mainScreen().applicationFrame.width, yourDesiredHeight) 
    }) 

但是,一旦它被設置,這不會幫助改變輸入附件的高度。

首先添加一個單獨的UIView作爲輸入附件(添加高度)並將按鈕添加到此UIView。之後在內部動畫按鈕。這工作。 您可以查看代碼中的要點
https://gist.github.com/rakeshbs/539827925df923e41afe

+0

您好,感謝您的回覆。但不幸的是,即使改變高度也無濟於事。高度仍然保持不變。你有什麼想法,爲什麼?謝謝 –

+0

你可以在 – rakeshbs

+0

這個問題上更新你當前的代碼我已經通過使用另一個視圖添加了高度作爲inputaccessoryview並添加了按鈕來解決問題。檢查代碼。 https://gist.github.com/rakeshbs/539827925df923e41afe – rakeshbs